% Unit 3 (MATLAB Course @ UAM)
% (c) Alexander Knebe

function [x0, n] = bisec(f, a, b)

% define accuracy
eps = 1e-8;

% count number of iterations
n  = 0;
x0 = (a+b)/2;

while abs(a-b)>eps
   n  = n + 1;
   if f(a)*f(x0) > 0
       a = x0;
   else
       b = x0;
   end
   x0 = (a+b)/2;
end

end