function [xs, fs, n] = bisection(f, a, b, tol)
% This function is for finding a single isolated root of f over the interval [a,b]
% When do you get an error   
fa = feval(f, a);
fb = feval(f,b);
if sign(fa) * sign(fb) == 1 
    xs = nan;    
    fs = nan;
    n = nan;
    fprintf('error, check your inputss...');
    return
elseif a >= b; 
    xs = nan; 
    fs = nan; 
    n = nan; 
    fprintf('error, check your inputss...');
    return
end 
n=0; % initialize itteration
x0 = (a + b)/2;
f0 = feval(f, x0);
xf = x0;
while (abs(x0 - xf)) >= tol*(1+abs(x0))||abs(f0)>=tol;    
    xf = x0;
    x0 = (a + b)/2;
    n = n + 1;
    if (fa) * (f0) < 0
        a = x0;        
    else 
         b = x0;        
    end
end
xs = xo;
fs = f0;
% Printing final results
fprintf('\n Bisection Method For Functions W/ Multiple Roots \n Homework 3 Problem 1')
fprintf('\n\n xs = %f produces  \n fs = %f \n n = %i iterations\n', xs, fs, n-1);
end
%bisection(@(x)(cos(x)-x),0,1,1e-6)






