function [lower, upper, z] = seplam_bisect(A, B, lower, upper, z, abstol, prtlevel)
% bisection for seplambda
% returns lower and upper bounds, the latter with certifying z, 
% and with lower bound within abstol
iter = 0;
while upper - lower > abstol 
   iter = iter + 1;
   if iter > 100
      error('max iterations exceeded, try bigger abstol')
   end
   if lower > upper
      error('bisect: something wrong with the bounds')
   end
   mid = (lower + upper)/2;
   znew = realimagcheck(A, B, mid);
   if isnan(znew)
      lower = mid;
      if prtlevel > 0
          fprintf('\nnew lower bound is %e', lower)
      end
   else
      upper = mid;
      z = znew;  
      if prtlevel > 0
          fprintf('\nnew upper bound is %e', upper)
      end
   end
end
