Running multiple for loops

1 次查看(过去 30 天)
John
John 2014-12-5
编辑: Stephen23 2014-12-5
I am extremely new to MATLAB and I am trying to develop a code that will give me an output for a series of equations that are affected by one another. This is extremely simple in Excel; however, I am required to do this in MATLAB. The first for loop runs and works perfectly but I can not get the second for loop to give me an output. I am positive that this extremely simple, I am just not familiar with this program. Thanks.
%%First 4-Bar%%
O2O4=2; O2A=5.38517; O4B=4; AB=1:1.74457;
%%Second 4-Bar%%
AC=1.41422; AG=6.96923; CG=5.87276; GE=1.82186; CD=4; DE=3:.967108; DE=3:.001:.967108;
for i=1:.001:1.74457
thetaO2A=180-acosd((O2A^2+O2O4^2-(O4B+(i))^2)/(2*(O2A*O2O4)));
thetaO4B=acosd((O2O4^2+(O4B+(i))^2-O2A^2)/(2*(O2O4*(O4B+(i)))));
thetaAC=(180-156.8014)+thetaO2A;
thetaAG=(180-121.46776)+thetaO2A;
thetaCG=(180-136.66081)+thetaAC;
pointA=[O2A*cosd(thetaO2A);+O2A*sind(thetaO2A)];
end
for ii=3:0.001:.967108
thetaGE=(acosd((CG^2+GE^2-(CD+(ii))^2)/(2*CG*GE)))-(180-CG)
end
for iii=3:0.001:1.19133
%another theta equation I have not derived yet that will reference the
%previous two for loops and the third variable iii
end

回答(2 个)

Andrew Newell
Andrew Newell 2014-12-5
编辑:Andrew Newell 2014-12-5
In the second and third loops, the starting value is higher than the end. Maybe you want
for ii=3:-0.001:.967108
(this counts down from 3 to 0.968).
  2 个评论
John
John 2014-12-5
I do actually need to count down instead of up on the second two for loops. Would this matter for the output?
Andrew Newell
Andrew Newell 2014-12-5
If you mean, will it change the output for a given value of ii? No. Note that, if you want to keep all the values of thetaGE, you could replace the loop by a vector expression:
x = .968:.001:3;
thetaGE=(acosd((CG^2+GE^2-(CD+x).^2)/(2*CG*GE)))-(180-CG);
The .^2 represents element-by-element taking of the power.

请先登录,再进行评论。


Stephen23
Stephen23 2014-12-5
编辑:Stephen23 2014-12-5
It seems like the basic problem is a lack of indexing to keep the results of each loop. Currently each loop simply redefines the variable pointA to be the result of the last line in the loop, but this is just a 2x1 array, and so you loose all of the results from the previous iterations.
However an even better solution is to vectorize your code, which is faster, neater and reads more like the maths it represents. Note the use of array operations , e.g. .^ instead of ^.
%%First 4-Bar%%
O2O4=2; O2A=5.38517; O4B=4; AB=1:1.74457;
%%Second 4-Bar%%
AC=1.41422; AG=6.96923; CG=5.87276; GE=1.82186; CD=4; DE=3:.967108; DE=3:.001:.967108;
%
vec = 1:.001:1.74457;
%
thetaO2A = 180-acosd((O2A^2+O2O4^2-(O4B+vec).^2)/(2*(O2A*O2O4)));
thetaO4B = acosd((O2O4^2+(O4B+vec).^2-O2A^2)/(2*(O2O4*(O4B+vec))));
thetaAC = (180-156.8014)+thetaO2A;
thetaAG = (180-121.46776)+thetaO2A;
thetaCG = (180-136.66081)+thetaAC;
pointA = [O2A*cosd(thetaO2A);+O2A*sind(thetaO2A)];
%
%for n=3:0.001:.967108
% thetaGE = acosd((CG^2+GE^2-(CD+n)^2)/(2*CG*GE)) - (180-CG)
%end
I leave the second loop as an exercise for the reader...
Also note that you should not use i for your loop variable, as this is the name of the inbuilt imaginary unit .

类别

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