For loop that runs until value reaches a certain point.

5 ビュー (過去 30 日間)
Tristan Blackledge
Tristan Blackledge 2019 年 10 月 30 日
コメント済み: Image Analyst 2019 年 10 月 30 日
I am trying to work this program to produce the k_eff and phi_eff that meet the conditions below but don't know what is wrong with the code.
A =
0.1139 -0.0139 0 0 0 0
-0.0069 0.1139 -0.0069 0 0 0
0 -0.0069 0.1139 -0.0069 0 0
0 0 -0.0069 0.1139 -0.0069 0
0 0 0 -0.0069 0.1139 -0.0069
0 0 0 0 -0.0139 0.2806
F =
0.1240 0 0 0 0 0
0 0.1240 0 0 0 0
0 0 0.1240 0 0 0
0 0 0 0.1240 0 0
0 0 0 0 0.1240 0
0 0 0 0 0 0.1240
Both F and A are 6x6 matrices
k initial or k_o = 1
phi initial or phi_o = ones(N+1,1)
N=5
crit_k=10^-9;
crit_phi=10^-8;
N=5;
for i=1:1:inf
k_o=1;
phi_o=ones(N+1,1);
if (i-1)==0
k=k_o;
phi=phi_o;
else
phi(i)=A\((1/k(i-1))*F*phi(i-1));
k(i)=k(i-1)*sum(phi(i))/sum(phi(i-1));
if abs(k(i)-k(i-1))/k(i)<=crit_k && abs(phi(i)-phi(i-1))/phi(i)<=crit_phi
k_eff=k(i);
phi_eff=phi(i);
break;
end
end
end
  1 件のコメント
Image Analyst
Image Analyst 2019 年 10 月 30 日
Evidently your criteria is never met. Why don't you plot the ratio at each iteration and see what it does?
Don't use a for loop with an infinite number of iterations. Use a while loop instead, and don't forget to have one condition be a check on the number of iterations so that it doesn't get unreasonably large.

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by