%
% Regula Falsi for f(x) = x^2 - 2;
%


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

%%%%%

x0  = 2;
x1  = 1;

tol      = 10^(-5);
MAX_ITER = 25;
i        = 1;

fprintf('---------------------------------\n');
fprintf('  i              x           f(x)\n');
fprintf('---------------------------------\n');
while( i < MAX_ITER )
  dx = f(x1)*(x0-x1)/(f(x0)-f(x1));
  i = i+1;
  p = x1 - dx;
  fprintf('%3d   %8.6f   % 12.5e  \n',i, p, f(p));
  if( f(x0)*f(p) > 0 )
    x0 = p;
  else
    x1 = p;
  end
  if( abs(dx)<tol )
    x1 = p;
    return;
  end
end
fprintf('WARNING: Regulafalsi iteration did not converge after %d steps.\n',i);



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%---------------------------------
%%%  i              x           f(x)
%%%---------------------------------
%%%  2   1.333333   -2.22222e-001
%%%  3   1.400000   -4.00000e-002
%%%  4   1.411765   -6.92042e-003
%%%  5   1.413793   -1.18906e-003
%%%  6   1.414141   -2.04061e-004
%%%  7   1.414201   -3.50128e-005
%%%  8   1.414211   -6.00729e-006
%%%  9   1.414213   -1.03069e-006
