Step-wise algorithm
% Program Secant
% A program for computing root of a single
% equation using the secant method.
% Define the following given values:
% x0 = first initial guess of root x
% x1 = second initial guess of root x
% es = stopping criterion tolerance (%)
x0 = 3.; x1 = 2.; es = 0.001;
% Define the function of the problem:
func = @(x)(exp(-x/4)*(2-x) - 1); % This function..
%should be changed according to your question
fprintf('\n Iteration No. x \n');
for iter = 1:500
f0 = func(x0); f1 = func(x1);
df = (f0-f1)/(x0-x1);
dx = -f1/df; x0 = x1; x1 = x1 + dx;
fprintf(' %8d %22.6e\n', iter, x1);
tol = abs(dx*100./x1);
if tol < es
fprintf('\n The root is %14.6e\n', x1)
break
end
end
while tol > es
fprintf(' Root cannot be reached for\n');
fprintf(' the given range');
break
end