function [fun, x, flg] = SecantMethod(Fun, x0, x1, N, tol,rho)
% On input: 
%   Fun is the name of function 
%   x0, x1 are initial guesses, and tol tolerance.
%
% On output
%   fun contains all function values 
%   computed by Secant
%   flg = 0 means success; otherwise method failed.
%   x(end) is the root if flg = 0.
%   
% Written by Ming Gu for Math 128B, Spring 2010
% 
[FunFcn,msg] = fcnchk(Fun,0);
if ~isempty(msg)
    error('InvalidFUN',msg);
end
flg = 1;
x(1)= x0;
x(2)= x1;
fun(1) = FunFcn(x(1),rho);
fun(2) = FunFcn(x(2),rho);
for k = 2:N
    if (fun(k) == fun(k-1))
       return;
    end
    x(k+1) = x(k) - fun(k)/(fun(k)-fun(k-1))*(x(k)-x(k-1));
    fun(k+1) = FunFcn(x(k+1),rho);
    if (abs(fun(k+1)) < tol)
       flg = 0;
       return;
    end
end


       

