function root = secant(fname, x, tol);

% SECANT  Secant method
%
% Input       fname  : Name of function.
%             x      : Vector with two approximations of the root.
%             tol    : Relative error of the found root.
% Output      root   : Position of the root.
%
% Remarks  1) There are functions for which this method fails. So be very 
%             careful using this algorithm!
%          2) Disadvantage of this method is that you need two start values.
%             With a wrong choice of these start values the algorithm will
%             not work properly.

if nargin < 3, tol = 1e-7; end

x1 = x(1);
x2 = x(2);
while 1,

% Calculate the new approximation based on the secant method

  f1 = feval(fname,x1);
  f2 = feval(fname,x2);
  root = x2 - f2*(x2-x1)/(f2-f1);

  err = abs((root-x2)/root);
  x1 = x2;
  x2 = root;
  if err < tol,
    break;
  end
end
