How to exit a for loop if the conditions within it are already met?
2 views (last 30 days)
Show older comments
I am using an iterative Newton- Raphson method to calculate the square root of an input number from an initial guess of the square root. The exit condition of the loop is that the absolute relative percentage error condition (=(abs((x-xprevious)/x))*100) is met however if the input guess is already close enough to the square root of the input number how can I stop the loop running an iteration of the for loop?
0 Comments
Answers (1)
James Tursa
on 29 Nov 2015
Sounds like you have two conditions, your relative error calculation and your guess being close enough. If you want to forgo the iteration as you suggest, then you will need to code up the guess part as well. E.g., maybe wrap your iteration code in an if-test for the guess calculation, or maybe make the guess calculation part of the loop test. If you post your code we could suggest ways to do it.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!