How to use an array as an input in a while loop

2 次查看(过去 30 天)
I need to make the input of t2 vary from 1 to 360 degrees so I can plot it on a graph with R4. t2 as the x input and R4 as the y output. Thank you.
R2=2.5; R3=7; R1=1; t1=90*pi/180; t4=0;
t3=330*pi/180;
R4=8;
n=0;
error=1;
for t2=(0*pi/180:1*pi/180:360*pi/180)
while error>0.001
n=n+1;
F=R2*cos(t2)+R3*cos(t3)+R1*cos(t1)-R4*cos(t4);
G=R2*sin(t2)+R3*sin(t3)+R1*sin(t1)-R4*sin(t4);
A=[-R3*sin(t3),-cos(t4);
+R3*cos(t3),-sin(t4)];
B=[-F;-G];
Delta=inv(A)*B;
t3=t3+Delta(1);
R4=R4+Delta(2);
error=abs(Delta);
t2=t2+1*pi/180;
end
end
plot(t2,R4)
t2
t3*180/pi
R4

回答(1 个)

Walter Roberson
Walter Roberson 2020-9-20
Your question requires that you plot with t2 as the independent variable and R4 as the dependent variable.
You are currently looping through one t2 value at a time.
You are not saving the values of t2 or R4 within your loop: you just always have the "current" t2 and R4.
To continue on with that code structure, you would need to plot one t2, R4 pair at a time, counting on the overall plot to make sense, that the result of adding one point at a time, multiple times, would be the graph you wanted.
However, when you plot one point at a time, MATLAB never automatically joins points into lines.
Your options then become:
  1. plot one point at a time using markers, ending up with a plot that is only markers with no lines; or
  2. create an empty line() handle before the loop. Then, each iteration, add the new x and y to the end of the line's XData and YData properties, extending the line object; or
  3. Use animatedline() before the loop. Then, each iteration, use addpoints() to add the new x and y to the loop, allowing animatedline to take care of whatever details it needs to make that work; or
  4. restructure your code so that it records each t2 value and R4 value as you go through the loop, not plotting inside the loop, and then afterwards make a single plot() call that will be able to join the points because there will be more than one point available to it to join together.
Your for t2 loop is iterating by pi/180 each time, so you cannot use t2 as an index to know where to store the current R4 value relative to the beginning of your iterations. Mathematically, you could calculate R4_index = t2*180/pi + 1, but because of floating point round-off, the calculate value is mostly not going to turn out to be an exact integer.
What can you do that will reliably create a relative index to know where to store the current t2 and R4 values?
Answer: create a list of all your t2 values in advance, and then iterate over indices into the t2 list. The "current" t2 value can be obtained by indexing the list of all t2 values by the current index, and the relative offset to store the current R4 value at would be the same as the current index being iterated over.
An example of how to do this is shown at the link I previously gave, https://www.mathworks.com/matlabcentral/answers/504285-for-loop-for-non-consecutive-values-in-an-array . I show there the general pattern that can be used for any for loop in which the values to iterate over are known in advance, even if the values are not consecutive integers; even if the values are not integers at all; even if the values are scrambled order; even if the values have duplicates.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by