Why does this code cause matlab to hang on busy?

9 views (last 30 days)
Im puzzled as to why this code causes matlab to hang and not generate my desired values. My human logic is clashing with computer logic so I am very upset as to why this doesn't work
estFS= 2.0; %I estimated a high factor of safety
Err=1;
while Err >0.001
FTOP=0;
FBOT=0;
for r=1:1:length(alpha)
m=(cosd(alpha(r))+ tand(phiprime)*sind(r))/estFS;
for k= 1:1:9
FTOP=FTOP+(cprime*bn+(Wn*tand(phiprime)));
FBOT=FBOT+(Wn*sind(alpha(r)));
end
Fs= FTOP/FBOT;
Err=abs(Fs-estFS);
Fs=estFS;
end
end
  1 Comment
Adam Danz
Adam Danz on 31 Jul 2020
There's no way we can look into the problem without having values for all of the variables.
There are 5 undefined variables.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 31 Jul 2020
You are currently calculation Err for each alpha value, but then you overwrite it with the Err for the next alpha value.
You should either be calculating Err(r ) -- separate Err for each different alpha -- or else you should be calculating a composite error from the contributions for each different alpha.
If you calculate separate Err values for each alpha, then you should be careful because you are currently using while Err > 0.001 and if Err becomes a vector then the while condition would only be considered true if all of the Err are > 0.001 -- so the while loop would stop as soon as one did better than 0.001 . If you are going to have separate Err values, you should consider while any(Err > 0.001) -- taking note that it will continue to try to refine for values that are already "good enough".
So, consider then
Err = 1 * ones(size(alpha));
while any(Err > 0.001)
for r = find(Err > 0.001) %only work on the ones out of range
FTOP = 0;
FBOT = 0;
for k = 1:1:9
FTOP=FTOP+(cprime*bn+(Wn*tand(phiprime)));
FBOT=FBOT+(Wn*sind(alpha(r)));
end
Fs = FTOP/FBOT;
Err(r) = abs(Fs-estFS);
end
end
This assumes that the error for each alpha is independent of what is happening for the other alpha values, which is not what you have programmed at the moment. The way you initialize FTOP aand FBOT at the moment accumulates error over all of the alpha values... and if that is what you want to do then you should reconsider why you are calculating Err inside the for r loop.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!