%%%
%%% Bisection for f(x) = x^2 - 2;
%%%

a = 0.2;
b = 1.6;

f = inline('x^2 - 2');

k=0;
MAX_ITER = 50;
tol = 10^(-5);
m=(a+b)/2;

fprintf('---------------------------------------------\n');
fprintf(' k         a         b         m         f(m)\n');
fprintf('---------------------------------------------\n');
while( k < MAX_ITER )
  k = k + 1;
  fprintf('%2d  %8.6f  %8.6f  %8.6f  % 10.8f\n',k,a,b,m,f(m));
  if( f(a)*f(m) > 0 )
    a = m;
  else
    b = m;
  end
  if( f(m)== 0 ) | abs(b-a)/2<tol 
    fprintf('%2d  %8.6f  %8.6f  %8.6f  % 10.8f\n',k,a,b,m,f(m));
    return;
  end
  m = (a+b)/2;
end
fprintf('WARNING: Bisection iteration did not converge after %d steps.\n',k);



%%% ---------------------------------------------
%%% k         a         b         m         f(m)
%%%---------------------------------------------
%%% 1  0.200000  1.600000  0.900000  -1.19000000
%%% 2  0.900000  1.600000  1.250000  -0.43750000
%%% 3  1.250000  1.600000  1.425000   0.03062500
%%% 4  1.250000  1.425000  1.337500  -0.21109375
%%% 5  1.337500  1.425000  1.381250  -0.09214844
%%% 6  1.381250  1.425000  1.403125  -0.03124023
%%% 7  1.403125  1.425000  1.414063  -0.00042725
%%% 8  1.414063  1.425000  1.419531   0.01506897
%%% 9  1.414063  1.419531  1.416797   0.00731339
%%%10  1.414063  1.416797  1.415430   0.00344120
%%%11  1.414063  1.415430  1.414746   0.00150651
%%%12  1.414063  1.414746  1.414404   0.00053952
%%%13  1.414063  1.414404  1.414233   0.00005611
%%%14  1.414063  1.414233  1.414148  -0.00018558
%%%15  1.414148  1.414233  1.414191  -0.00006474
