For loop that runs until value reaches a certain point.
4 次查看(过去 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
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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!