  function xStar = Bisection(f,L,R)   
% f is a handle to a continuous function  f(x) of a single variable.
% L and R define an interval [L,R] and f(L)f(R) < 0
%
% xStar is an approximate root of f with the property that it
% is within |R-L|/10^6 of a true root.


fL = f(L); 
fR = f(R);
tol = 10^-6*(R-L);
while abs(R-L) > tol
   mid = (L+R)/2;
   fmid = f(mid);    
   if fL*fmid<=0
      % There is a root in [L,mid].
      R  = mid; 
      fR = fmid;
   else
      % There is a root in [mid,R].
      L  = mid; 
      fL = fmid;
   end  
   disp(sprintf('%10.6f  %10.6f',L,R))
end
xStar = (L+R)/2;