function p = bisection(f,a,b, eps)
% function p = bisection(f,a,b, eps)
% Input arguments:
% f: inline format of the function we want to solve.
% a,b: Initial interval satisfying f(a)f(b)<0.
% eps: Tolerance 
% Example: 
% For finding the roots of x^6 -x -1 =0 with accuracy
% eps = 1e-3 use
% f =inline('x.^6 -x - 1')
% with initial a=0, b =2.
% p = bisection(f,a,b,eps)
fprintf('--------------------------------------------------------------------------------\n')

 fprintf('n \t a \t\t b \t\t c \t\t b-c \t\t f(c)\n')
  fprintf('--------------------------------------------------------------------------------\n')
n=1;
if f(a)*f(b)>0 | a==b
    disp('The root does not live in this interval try another!')
else
    c = (a + b)/2;
    err = abs(f(c));
   fprintf('%d  %10.4e      %10.4e       %10.4e     %10.4e     %10.4e \n',...
         n, a, b , c , b-c, f(c) )      
    while err > eps
        n=n+1;
        fprintf('%d  %10.4e      %10.4e       %10.4e     %10.4e     %10.4e \n',...
         n, a, b , c , b-c, f(c) ) 
   if f(a)*f(c)<0 
       b = c;
      % fprintf('%d  %10.4e      %10.4e       %10.4e     %10.4e     %10.4e \n',...
      %   n, a, b , c , b-c, f(c) ) 
   else
       a = c;    
   % fprintf('%d  %10.4e      %10.4e       %10.4e     %10.4e     %10.4e \n',...
   %      n, a, b , c , b-c, f(c) )   
   end
    c = (a + b)/2; 
   err = abs(f(c));
   
    end
     fprintf('--------------------------------------------------------------------------------\n')
end