function c = bisection(a,b,f,Tol, Max_it)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function c = bisection(a,b,f,Tol, Max_it)
%
% Bisection Method
% finds root of function handle f
% a: left endpoint of search interval
% b: right endpoint of search interval
% f: function handle
% Tol: how close to root you want usually Tol = 1e-10
% Max_it: max # of iterations
% c: root
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%home

%testing
%Max_it = 100;
%Tol = 1e-6; %how close to zero
%define function 
%f = @(x) 2*x^3+7*x^2+5;
%f = @(x) exp(x) -x^3 -2;
%f = @(r) 100000*exp(20*r)- 500000
%f = @(x) x^3 +2*x -5
%a = -10;
%b = 10;

if (a>=b) 
    disp('Error: a is greater than or equal to b on interval [a,b]')
    c = 'error';
    return
end

i = 1; %initialize counter

while i < Max_it
    c = a + (b-a)/2;%(a+b)/2;
    disp('***********************************')
    disp(['current iteration = ' num2str(i)])
    disp(['f(root) = ' num2str(f(c))])
    disp(['root = ' num2str(c)])
    if abs(f(c)) < Tol
        disp('***********************************')
        disp(['final root = ' num2str(c,16)])
        disp('***********************************')
        break
    elseif sign(f(a))*sign(f(c))<0 %avoid underflow or overflow %f(a)*f(c)<0
        b = c;
    elseif sign(f(b))*sign(f(c))<0 %avoid underflow or overflow %f(b)*f(c)<0
        a = c;
    else
        disp('No root in interval')
        break
    end
    i = i + 1;
end
    






