How to exit a for loop if the conditions within it are already met?

2 views (last 30 days)
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?

Answers (1)

James Tursa
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.
  1 Comment
anonymous
anonymous on 3 Dec 2015
the problem is my guess being close enough is determined by the relative error calculation and if the initial input is close enough it is not possible to do a relative error calculation with it

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!