terminating the while if loop

조회 수: 1 (최근 30일)
sermet
sermet 2017년 7월 14일
편집: Jan 2017년 7월 14일
while abs(dE) > 1e-12
iteration=iteration+1;
E_old = E;
E = M+(ecc(1)*sin(E)); %eccentric anomaly
dE=E-E_old;
if iteration==1000
warndlg('iteration cannot be converged ', 'Error!', 'modal')
end
end
How can I modify above code to terminate the while loop when iteration exceeds 1000?

채택된 답변

Star Strider
Star Strider 2017년 7월 14일
편집: Star Strider 2017년 7월 14일
I would add a break or return in your if block:
if iteration>=1000
warndlg('iteration cannot be converged ', 'Error!', 'modal')
return
end
  댓글 수: 2
sermet
sermet 2017년 7월 14일
it doesn't terminate while loop because abs(dE) is always higher than 1e-12.
Star Strider
Star Strider 2017년 7월 14일
change the if condition to:
if (iteration>=1000) || (abs(dE) < 1e-12)
warndlg('iteration cannot be converged ', 'Error!', 'modal')
return
end
That should work as you want it to.

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

추가 답변 (1개)

Jan
Jan 2017년 7월 14일
편집: Jan 2017년 7월 14일
limit = 1000;
iter = 0;
dE = 1;
while abs(dE) > 1e-12 && iter < limit
iter = iter + 1;
E_old = E;
E = M+(ecc(1)*sin(E)); %eccentric anomaly
dE = E-E_old;
end
if iter == limit
warndlg('iteration cannot be converged ', 'Error!', 'modal')
end

카테고리

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