terminating the while if loop

1 vue (au cours des 30 derniers jours)
sermet
sermet le 14 Juil 2017
Modifié(e) : Jan le 14 Juil 2017
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?

Réponse acceptée

Star Strider
Star Strider le 14 Juil 2017
Modifié(e) : Star Strider le 14 Juil 2017
I would add a break or return in your if block:
if iteration>=1000
warndlg('iteration cannot be converged ', 'Error!', 'modal')
return
end
  2 commentaires
sermet
sermet le 14 Juil 2017
it doesn't terminate while loop because abs(dE) is always higher than 1e-12.
Star Strider
Star Strider le 14 Juil 2017
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 14 Juil 2017
Modifié(e) : Jan le 14 Juil 2017
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

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by