For loop that runs until value reaches a certain point.

5 visualizaciones (últimos 30 días)
Tristan Blackledge
Tristan Blackledge el 30 de Oct. de 2019
Comentada: Image Analyst el 30 de Oct. de 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 comentario
Image Analyst
Image Analyst el 30 de Oct. de 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.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by