How to plot in a for loop?

3 views (last 30 days)
Nicole Evangelista
Nicole Evangelista on 23 Oct 2017
Commented: Rena Berman on 30 Oct 2017
my graph is showing up, but the plot is blank. Does anyone know why? Thank you.
TimeOut=input('Press Enter to view velocity and acceleration of the wiper process in increments of 5 degrees')
hold on
for i=[0:5:90]
L1=3; %in feet
L2=3; %in feet
r1=[-L1*sind(i),L1*cosd(i),0];
i2 = asind(-L1*sind(i)/(L2));
r2=[L2*sind(i2),L2*cosd(i2),0]; %array
r0 = r1(2)+r2(2); %r0=length of travel of mechanism
disp(sprintf('Length of travel of mechanism is %0.2f feet\n',r0(1)));
vb=cross(W, r1);
w2=[0, 0, L1*cosd(i)/(L2*cosd(i2))];
vc=[0,-W(3)*-L1*sind(i)-w2(3)*L2*sind(i2),0];
%Make sure the velocity of the mechanism is not to exceed 480mm/s
if abs(vc)>=1.5748; %480mm/s = 1.5748ft/s
disp('Velocity exceeds 480mm/s. Refine inputs.')
break
end
%Find Acceleration
anb=cross(W, cross(W, r1)); %Normal Acceleration of b
atb=cross(alpha1, r1); %Tangential Acceleration of b
ancb=cross(w2, cross(w2, r2)); %Normal Acceleration of c/b
alpha2=[0, 0, -(anb(2)+atb(2)+ancb(2))/r2(1)];%Angular Acceleration of link 2
atcb=cross(alpha2, r2);%Tangential Acceleration of b
ac=anb + atb + ancb + atcb;%Acceleration of wiper (point C)
disp(sprintf('Velocity of wiper is %0.2f ft/s\n',vc(2)));
disp(sprintf('Acceleration of wiper is %0.2f ft/s^2\n',ac(1)));
subplot(2,1,1)
plot(i,vc(2))
grid
xlabel('Angle in Degrees')
ylabel('Velocity')
subplot(2,1,2)
plot(i,ac(1))
grid
xlabel('Angle in Degrees')
ylabel('Acceleration')
end
  6 Comments
Cam Salzberger
Cam Salzberger on 24 Oct 2017
I had assumed you were cutting out the rest of your loop code in your post not literally cutting it out. Yes, "V" and "A" will contain the same value for every element if you loop through each element and assign the same value to it. When I suggested you follow a structure similar to this:
for i = iVals
...
velVals(i) = vc(2);
...
accelVals(i) = ac(1);
...
end
I meant:
for i = iVals
...PUT CALCULATIONS FOR vc HERE...
velVals(i) = vc(2);
...PUT CALCULATIONS FOR ac HERE...
accelVals(i) = ac(1);
...DO ANYTHING ELSE YOU NEED TO DO IN THE LOOP HERE...
end
I'm not here to write your code for you fully and completely. I'm just trying to give you suggestions on how you can resolve the issues you are facing and allow you to apply them to your own code.
Hopefully this helps clear up the misunderstanding.
-Cam
Rena Berman
Rena Berman on 30 Oct 2017
(Answers Dev) Restored edit

Sign in to comment.

Accepted Answer

Cam Salzberger
Cam Salzberger on 23 Oct 2017
Hello Nicole,
Assuming you have just opened MATLAB, your axes are first created when you do "hold on" (which isn't really the best way to create axes, since it's not obvious to people who read your code). Later, when you use subplot, it actually deletes these axes and replaces them. This is important, because the property that "hold on" affects ('NextPlot' property) is reset to its default value. So you are replacing the existing line every time you plot a new line.
The second issue is that you are plotting (as far as I can tell) one point at a time. However, you are using the default line specification, which is a simple line between points. If you have only one point, it won't draw the line. If you want to plot one point at a time, specify a line specification that includes a marker. For example:
plot(i, vc(2), 'o')
However, I would recommend computing all the points you would like to plot, saving them to an array, and then plot at once outside the loop. It's just cleaner and better for the graphics operations.
-Cam
  2 Comments
Cam Salzberger
Cam Salzberger on 23 Oct 2017
Preallocate outside the loop so you're not growing the variables inside of it:
iVals = 0:5:90;
velVals = zeros(size(iVals));
accelVals = zeros(size(iVals));
Then assign the values to the arrays as you compute them:
for i = iVals
...
velVals(i) = vc(2);
...
accelVals(i) = ac(1);
...
end
Then do your plotting at the end:
subplot(2,1,1)
plot(iVals, velVals)
grid
xlabel('Angle in Degrees')
ylabel('Velocity')
subplot(2,1,2)
plot(iVals, accelVals)
grid
xlabel('Angle in Degrees')
ylabel('Acceleration')
Cam Salzberger
Cam Salzberger on 23 Oct 2017
Edited: Cam Salzberger on 23 Oct 2017
Right, sorry, I didn't pay attention to the actual loop values. The error is because it's trying to use 0 as an index. A better way would be:
x=0:5:90;
V=zeros(size(x));
A=zeros(size(x))
for i=1:numel(x)
...(using x(i) in calculations)...
V(i)=vc(2);
A(i)=ac(1);
end
Of course, I technically wouldn't recommend using "i" or "j" as a variable name, since it has a built-in meaning as the imaginary unit. I prefer "k" for single loops, "r"/"c" for looping over a matrix, or the ever-popular "ii", "jj", etc.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!