Why does this code cause matlab to hang on busy?

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 件のコメント

Adam Danz
Adam Danz 2020 年 7 月 31 日
There's no way we can look into the problem without having values for all of the variables.
There are 5 undefined variables.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 7 月 31 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

タグ

質問済み:

2020 年 7 月 31 日

回答済み:

2020 年 7 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by