Clear Filters
Clear Filters

MatLab code runs indefinitely since adding while loop

3 views (last 30 days)
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
}

Accepted Answer

Geoff Hayes
Geoff Hayes on 26 Oct 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 Comments
Cat
Cat on 26 Oct 2014
Thank you very much. I understand why it was stuck now! I'm trying to make it store the value of F if it is larger than the previous value in the iteration so that I end up with a selection of the largest values. I also however want to then find the minimum force and the corresponding values for theta, a, b, r and phi. I had previously used an if loop but wasn't sure how to store the values. This is what I have changed the code to if you have any thoughts! It now runs but unfortunately gives me a max Force of Infinity and a minimum of -Infinity.
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)));
end
if F(i,j)>F_max
F_max_new=F(i,j);
end
F_max=max(F_max_new);
F_min=min(F(:));
theta_opt=theta(:);
a_opt=a(:);
b_opt=b(:);
r_opt=r(:);
phi_opt=gamma(:)-20;
end
disp(['Minimum Force possible on ram ' num2str(F_min) 'N' ])
disp(['Corresponding Boom Angle for Minimum force ' num2str(theta_opt) 'degrees' ])
disp(['Corresponding Phi for Minimum force ' num2str(phi_opt) 'degrees' ])
disp(['Corresponding Length from origin to A (base of ram) for Minimum force ' num2str(a_opt) 'm' ])
disp(['Corresponding Length from origin to B (position of applied force on crane) for Minimum force ' num2str(b_opt) 'm' ])
disp(['Corresponding Ram length for Minimum force ' num2str(r_opt) 'm' ])
I am getting out a couple of errors too which I think are to do with trying to get theta etc which are from the same iteration as the minimum force:
Minimum Force possible on ram -InfN
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in MatLab_Code_main_2 (line 68)
disp(['Corresponding Boom Angle for Minimum force ' num2str(theta_opt) 'degrees' ])
Geoff Hayes
Geoff Hayes on 27 Oct 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.).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!