function [fun, x, flg] = BisectionMethod(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);
if (sign(fun(2)) * sign(fun(1)) > 0)
    return;
end
for k = 3:N
    x(k)   = (x(k-1)+x(k-2))/2;
    fun(k) = FunFcn(x(k),rho);    
    if (abs(fun(k)) < tol)
       flg = 0;
       return;
    end
    if (sign(fun(k))*sign(fun(k-1))>0)
        x(k-2:k-1) = [x(k-1),x(k-2)];
        fun(k-2:k-1) = [fun(k-1),fun(k-2)];
    end
end



       

