how to save outputs of a loop in vectors to use later ??? urgent please help

iam making a loop to calculate some values for theta from 0:360 but i cant plot correctly it always plots for me the last point out from the loop and i want to print the values corresponding the theta in the loop for my project and plot it here is the code
l1=0;%input('input l1: ');
l2=3;%input('input l2: ');
l3=8;%input('input l3: ');
w2=20.9;%;input('input w2: ');
for i=0:360
a=i*(pi/180);
b=l2*sin(a);
c=l2*cos(a);
d=-(asin((l1+a)/l3));
th3=d*(180/pi)*i;
e=l2*sin(d);
f=l2*cos(d);
g=l3*sin(d);
h=l3*cos(d);
l4=c+f;
w3=-w2*(c/f);
v4=-w2*(b)-w3*(g);
alp3=(b*(w2^2)+g*(w3^2))/h;
a4=[-(g*(alp3)+c*(w2^2)+h*(w3^2))];
plot (i,a4)
end
here i want to plot a4,th2 but plots only one point and i want to the 360 values of the a4? but how
thanks in advance

 Accepted Answer

I don't see a th2. Assuming you meant one point per loop iteration...
l1=0;%input('input l1: ');
l2=3;%input('input l2: ');
l3=8;%input('input l3: ');
w2=20.9;%;input('input w2: ');
a4 = zeros(1,361);
for ii=0:360
a = ii*(pi/180);
b = l2*sin(a);
c = l2*cos(a);
d = -(asin((l1+a)/l3));
th3 = d*(180/pi)*i;
e = l2*sin(d);
f = l2*cos(d);
g = l3*sin(d);
h = l3*cos(d);
l4 = c+f;
w3 = -w2*(c/f);
v4 = -w2*(b)-w3*(g);
alp3 = (b*(w2^2)+g*(w3^2))/h;
a4(ii+1) = -(g*(alp3)+c*(w2^2)+h*(w3^2));
end
plot(0:360,a4)

4 Comments

sorry i meant plot (i,a4);
and how can i see a4 as a vector to use later in some operation else?
really thank u very much............really appreciate it
To see a4, simply type:
a4
at the end of the loop. This will dump the vector to the screen.

Sign in to comment.

More Answers (1)

Hi, Where is th2. Anyways, I do not know what is this meant to do but try this :
th3_vect=zeros(0);
a4_vect=zeros(0);
for i=0:36
a=i*(pi/180);
b=l2*sin(a);
c=l2*cos(a);
d=-(asin((l1+a)/l3));
th3=d*(180/pi)*i;
e=l2*sin(d);
f=l2*cos(d);
g=l3*sin(d);
h=l3*cos(d);
l4=c+f;
w3=-w2*(c/f);
v4=-w2*(b)-w3*(g);
alp3=(b*(w2^2)+g*(w3^2))/h;
a4=[-(g*(alp3)+c*(w2^2)+h*(w3^2))];
th3_vect(length(th3_vect)+1)=th3;
a4_vect(length(a4_vect)+1)=a4;
end
plot (th3_vect,a4_vect)

3 Comments

I would not recommend growing arrays in the loop like this. The effect is to slow the code down. For such a small final length this may not matter a whole lot, but for larger lengths this will really slow things down...
I would completely go for the answer Mr. Matt Fig Suggested.
Thanks for your help Sir.
thanks for your help Mr. Matt fig and Mr.Ahmad
finished my kinematics project at last :D and plotted all figures required thanks agian:D

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!