function z = secant(fcn,x0,x1,tol)
% secant method
% fcn - string (in single quotes) containing name of the function
% x0 = an initial guess
% x1 = another initial guess, doesn't if x0 or x1 is larger
% There must be a root inside the interval [x0 x1]
% tol = tolerance
% Usage: z = secant('x2m4sinx',1,3,eps)
% See example 5.12 on p.232 of Heath
% K. Ming Leung, 02/03/03

if sign(x0) ~= sign(x1),
    error('Initial interval does not bracket a root');
end

dx=1;                         % change in
f0=feval(fcn,x0);
while abs(dx)> tol;
    f1=feval(fcn,x1);     % f(x1)
    dx=-f1*(x1-x0)/(f1-f0);     % change in x
    x0=x1;                    % replace x0 by x1
    f0=f1;                     % replace f0 by f1
    x1=x1+dx;             % update x1
end;
z=x1;