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

5 次查看(过去 30 天)
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

采纳的回答

Matt Fig
Matt Fig 2011-5-12
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 个评论
Matt Fig
Matt Fig 2011-5-12
To see a4, simply type:
a4
at the end of the loop. This will dump the vector to the screen.

请先登录,再进行评论。

更多回答(1 个)

Ahmad
Ahmad 2011-5-12
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 个评论
Ahmad
Ahmad 2011-5-12
I would completely go for the answer Mr. Matt Fig Suggested.
Thanks for your help Sir.
bassam  imam
bassam imam 2011-5-13
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by