Writing a 'while loop'

조회 수: 5 (최근 30일)
Taylor
Taylor 2013년 1월 29일
Hello, I am trying to write a while loop and I want it to keep iterating until the answer converges to within 1.e-4 of the previous iteration. Every time I run this code, it only iterates it once. How would I go about fixing my first line so that it keeps running until the difference between the last two iterations is less than or equal to 1.3-4?
while (error>=1.e-4);%%This is where I am getting confused
iteration=iteration+1;
phi(:,iteration)=D\(rho-T*phi(:,iteration-1));
error=norm(phi(:,iteration)-phi(:,iteration-1),Inf);
end
Thank you in advance for you help.

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 29일
편집: Azzi Abdelmalek 2013년 1월 29일
Initialize error to 1 for example
error=1
while (error>=1.e-4)
%your code
end
  댓글 수: 1
Jan
Jan 2013년 1월 30일
No, do not use "error" as a variable, because this shadows a very important Matlab function. I assume that the OP does have problems with this already, therefore avoiding such shadowing is strongly recommended.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by