% BISECT5  Solve polynomial root problem by bisection.

f = @(x) 3 * x - exp(x);  % define the function

a = 1;  b = 2;            % set initial bracket
s = sign(f(a));           % either +1 or -1 according to sign of f(a)
for n = 0:19
  fprintf('  [a_%2d, b_%2d] = [%f, %f]\n', n, n, a, b)
  c = (a+b)/2;
  if sign(f(c)) == s      % now this works if  f(a)>0  *or* if  f(a)<0
    a = c;
  else
    b = c;
  end
end
fprintf('final estimate  c = %f  has maximum error %e\n',c,(b-a)/2)
