Non working while loop
3 views (last 30 days)
Show older comments
When running the code below it seems the while loop is run through once and then executes the commands after the last end statement and ends the program.
I do not understand while the loop doesn’t actually loop until the while loop statement isn't true.
Thank you in advance
function P=fixpointiteration(val)
switch nargin
case 0
disp('0 Chosen input')
x0 = rand;
case 1
disp('1 Chosen input')
x0 = val;
otherwise
error('Unexpected input')
end
disp(x0)
f = @(x0) cos(x0);
i=0;
t = 0.00001;
x1=1;
while abs (x1-x0)>t
i=i+1
x1 = f(x0)
fprintf('x%d = %.8f\n',i,x1)
x0 = x1
end
disp('Number of iterations: ')
disp(i)
0 Comments
See Also
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!