Matlab issue: Nested while loop paradox

I have an issue where I need to calculate x1, and keep calculating x1 until either the iterations reaches the desired number of iterations OR the approximate error in %(epsilon) is less than E(user defined tolerance in %).
My problem is that in order to calculate epsilon I must have the x1 value, but in order for the x1 value equation to be performed it must have the epsilon value to know when to end the while loop, so it's like there is a paradox. Any suggestions on how to solve this?

 Accepted Answer

Fraser - just initialize the error epsilon to some value that is greater then R so that the condition
epsilon > E
is true and the body of the while loop will be evaluated. I think something like the following should work
no_iter = 0;
epsilonError = 2*E; % renamed to avoid confusion with built-in eps
while no_iter < max_iter && epsilonError > E
% calculate xinew
% calculate epsilonError
no_iter = no_iter + 1;
end
Note that I've combined your two while loops into one because you need the two conditions together (as the calculated epsilon may never be less than E and so you will get stuck without the iteration count check).
Also, I am unclear on your calculation for xinew (what is xi?) and the purpose of the line
if ((abs(xinew-xi)/xinew)*100)
which seems to have something to do with epsilonError.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!