%
% Newton's Method for f(x) = x^2 - 2;
%


f  = inline('x.^2 - 2');
fp = inline('2*x');
x0 = 1.6;

%%%%%

x  = x0;
tol      = 10^(-5);
MAX_ITER = 25;
i        = 0;

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



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

%%%---------------------------------
%%%  i              x           f(x)
%%%---------------------------------
%%%  0   1.600000    5.600000e-001
%%%  1   1.425000    3.062500e-002
%%%  2   1.414254    1.154682e-004
%%%  3   1.414214    1.666518e-009
%%%  4   1.414214    4.440892e-016 
