While Loop exiting before statement is met

2 ビュー (過去 30 日間)
Jana Stucke
Jana Stucke 2019 年 9 月 13 日
コメント済み: Aquatris 2019 年 9 月 17 日
Hi guys,
I have written a function which uses two vectors (221x1) . I now want to loop through each component until the residual is < 1e-4. However, the following code exits the while loop after the first iteration.
Could someone help out please?
function [alpha, PM]= alphacorrect(N,alpha_34,p)
K= 0.05315;
T= -0.00289;
c_deflect_vst=0.38;
dist_mrc=0.5;
PM=0;
alpha=0;
for i=1:length(N)
delta_alpha=1;
while delta_alpha> 1e-4
alpha_cos=(alpha*pi)/180;
dtheta=(N(i)*dist_mrc + PM).*K + N(i)*T -c_deflect_vst*cos(alpha_cos);
if N(i)<0
delta_alpha=alpha_34(i)-dtheta-alpha;
else
delta_alpha=alpha_34(i)+dtheta-alpha;
end
alpha=alpha_34(i)-dtheta;
%PM= polyval(p,alpha);
end
i=i+1;
% alpha(i)=alpha;
% PM(i)=PM;
end
end
  3 件のコメント
Jyothis Gireesh
Jyothis Gireesh 2019 年 9 月 16 日
I tried to replicate the condition that you mentioned in the question using some random inputs. But in most of the cases, the value of delta_alpha turned out to be negative after the first iteration of while loop. Even in the other cases the program was executing normally.
Can you provide the values for “N” and “alpha_34” so that I may able to replicate the error?
Aquatris
Aquatris 2019 年 9 月 17 日
Maybe you meant to use absolute value of delta_alpha as your while loop condition?

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

回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by