For loop that runs until value reaches a certain point.
조회 수: 5 (최근 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!