# For loop that runs until value reaches a certain point.

8 views (last 30 days)
Tristan Blackledge on 30 Oct 2019
Commented: Image Analyst on 30 Oct 2019
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 Comment

Image Analyst on 30 Oct 2019
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.