% XEQTANX:   find 20 solutions to the equation  -x = tan x
%            using the "bisection" method

for n = 1:20
   % each x_n which solves  -x = tan x  is in interval [a_n, b_n]:
   a = ((2*n-1) * pi / 2) + 1.0e-5;  % just to right of asymptote
   b = n * pi;
   disp(['looking in interval [' num2str(a) ', ' num2str(b) ']'])
   for m = 1:25  % error will be less than (pi/2) / 2^25
      c = (a + b)/2;
      if abs(tan(c) + c) < 1.0e-12
         disp('solution found with  (tan x + x) < 10^{-12}')
         break
      end
      if (tan(c) + c) > 0
         b = c;
      else
         a = c;
      end
   end
   c = (a + b)/2;
   disp(['  n = ' int2str(n) ' soln ' num2str(c,10) ' found in ' ...
      'bracket ' num2str(a,10) ', ' num2str(b,10) ']'])
end
