terminating the while if loop

1 次查看(过去 30 天)
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 中查找有关 Particle & Nuclear Physics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by