i want to keep calculating better values of root iteratively until either the max number of iteration is used or the error is less than the tolerance and then display the values using the switch statement.i think somethin is wrong in my while loop.

1 view (last 30 days)
k=1;
e=-1*((x(k+1)-x(k))/x(k+1))*100;
while e > tol && k==x_max;
e=-1*((x(k+1)-x(k))/x(k+1))*100;
k=k+1;
end
switch e
case e< tol
disp('Success!The result is %.4f',e)
case e<0
disp('The number was negative.')
otherwise
disp('Maximum number of iteration used.')
end

Accepted Answer

Thorsten
Thorsten on 2 Dec 2015
k_max = 100; % sample value, maximum number of iterations
:
while e > tol & k <= k_max
:
  3 Comments
Thorsten
Thorsten on 2 Dec 2015
The switch does not work because you compare e to (e<tol) or (e<0), both are logical values (0 or 1). Instead, use
if e < tol
disp('Success!The result is %.4f',e)
elseif e < 0
disp('The number was negative.')
else
disp('Maximum number of iteration used.')
end
Walter Roberson
Walter Roberson on 2 Dec 2015
switch true
case e < tol
disp('Success!The result is %.4f',e);
case e < 0
disp('The number was negative.')
otherwise
disp('Maximum number of iteration used.')
end

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!