%
% Secant Method Method for f(x) = x^2 - 2;
%

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

%%%%%


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

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



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

%%% ---------------------------------
%%%  i              x           f(x)
%%%---------------------------------
%%%  1   2.000000    2.00000e+000
%%%  2   1.333333   -2.22222e-001
%%%  3   1.400000   -4.00000e-002
%%%  4   1.414634    1.18977e-003
%%%  5   1.414211   -6.00729e-006
%%%  6   1.414214   -8.93146e-010
