I have a secant method in for loop trying to change it to while loop but problem is just showing one iteration and I want to make it like for loop but in while loop.
The code of For loop:
clc;clear;
x0=1;
x1=0;
f=@(x) 3*x+sin(x)-exp(x)
if abs(x0) <abs(x1)
t=x0;
x0=x1;
x1=t;
end
for i=1:5
x2=x1-(f(x1)*((x0-x1)/(f(x0)-f(x1))))
disp([x0, x1, x2, f(x2)]);
x0=x1;
x1=x2;
end
and this is my while loop code:
clc; clear;
x0=1;
x1=0;
error=1
tol=0.00001;
f=@(x) 3*x+sin(x)-exp(x)
if abs(f(x0)) < abs(f(x1))
t=x0;
x0=x1;
x1=t;
end
x2=x1-(f(x1)*((x0-x1)/(f(x0)-f(x1))))
while(abs(f(x2))<tol)
disp(x1);
x0=x1;
x1=x2;
end