You are on page 1of 2

function [res] = secant(func, x0, x1, es, max_it)

ea = 1;
prev = x1;
fprintf('Iteration \t\t x0 \t\t\t x1 \t\t\t xr \t\t\t ea\n');
for its = 1:max_it
uval = double(subs(func,x1));
lval = double(subs(func,x0));
xr = x1 - double((uval*(x0-x1))/(lval-uval));
rval = double(subs(func,xr));
ea = abs(double((xr-prev)/xr));
if its == 1
fprintf('%5d \t\t %f \t\t %f \t\t %f\n', its, x0, x1, xr)
else
fprintf('%5d \t\t %f \t\t %f \t\t %f \t\t %f\n', its, x0, x1, xr, ea*100)
end
if rval == 0
res = xr;
break
elseif ea <= es
res = xr;
break
else
x0 = x1;
x1 = xr;
end
prev = xr;
end
end

You might also like