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

1 回表示 (過去 30 日間)
bassam  imam
bassam imam 2011 年 5 月 12 日
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.
bassam  imam
bassam imam 2011 年 5 月 13 日
worked fine thanks allot :D

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by