MatLab code runs indefinitely since adding while loop
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Cat
am 26 Okt. 2014
Kommentiert: Geoff Hayes
am 27 Okt. 2014
I had a code which ran but wasn't giving me the right output so I changed an if lop to use a while loop instead and now when I run the code it runs indefinitely and I'm not sure why or what I can do to fix it. Have tried closing & reopening, removing while loop and then running without, using a different computer but all to no avail! Any help greatly appreciated! Cheers P.s, have tried to use the {} brackets but not sure it's all coming out right :/
Code {
m=4000; %kg
g=9.81;
L=4; %m
r_min=1.2; %m
r_max=2.2; %m
r=linspace(1.2,2.2,37); %m
theta_min=-20; %degrees
theta_max=80; %degrees
theta=linspace(-20,80,37); %degrees
phi=linspace(0,180,37); %degrees
gamma=phi+20; %degrees
%-----------------------------------
F_max=0;
for i=1:37
c_2(i)=((r_max^2)-(r_min^2))./(cosd(gamma(i)+theta_min)-cosd(gamma(i)+theta_max));
c_1(i)=(r_min^2)+c_2(i).*cosd(gamma(i)+theta_max);
a(i)=real(0.5*(((c_1(i)+c_2(i)).^(1/2))+((c_1(i)-c_2(i)).^(1/2))));
b(i)=real(c_2(i)/(2*a(i)));
for j=1:37
r(i,j)=sqrt((a(i)^2)+(b(i)^2)-(2*a(i)*b(i)*cosd(gamma(i)+theta(j))));
F(i,j)=(r(i,j)*m*g*L*cosd(theta(j)))/(b(i)*a(i)*sind(gamma(i)+theta(j)));
plot(theta,F(i,j));
end
while F(i,j)>F_max
F_max_new=F(i,j);
end
F_max=F_max_new
end
}
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 26 Okt. 2014
Cat - you should format your code so that it is readable. Just highlight the code portion and press the {} Code button.
It looks like your while loop is just the code
while F(i,j)>F_max
F_max_new=F(i,j);
end
So if the condition F(i,j)>F_max is true, then the body of the loop is executed which just sets F_max_new to a new value, then the condition is checked again. But since nothing has changed with respect to either F_max or F(i,j), then the condition is once again satisfied/true, so we execute the body of the loop. This continues without change and so the code becomes "stuck" in this loop.
If you are just trying to find the maximum value of F, then you could do this after you have finished the for loops. Just do
F_max = max(F(:));
and that will return the maximum value of F, and so you can avoid using the while loop.
2 Kommentare
Geoff Hayes
am 27 Okt. 2014
Cat - you are getting Inf and NaN values due to division by zero errors at the calculations for c_2(i), c_1(i) (since it depends upon c_2), and F(i,j). You may want to add some logic to handle these events.
As for the horzcat error, it is because theta_opt is a 37x1 array. Whenever you determine the minimum force, you will want to save the corresponding boom angle as well. This is true for the other attribute too (phi, length, etc.).
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!