function newtonex
% example for Newton method
%    find x1,x2 such that
%       2*x1 + x1*x2   = 2
%       2*x2 - x1*x2^2 = 2

x = [0;0]            % initial guess

while 1
  b = f(x);          % evaluate f
  A = fp(x);         % evaluate Jacobian
  d = -A\b;          % solve A*d = -b
  x = x + d
  if norm(d)<=1e-12 
    break
  end
end



function y = f(x)
% function for which we want f(x) = zero vector
y = [ 2*x(1) + x(1)*x(2) - 2 ; ...
      2*x(2) - x(1)*x(2)^2 - 2 ];

function A = fp(x)
% Jacobian f'(x)
A = [ 2+x(2) , x(1) ; ...
     -x(2)^2 , 2 - 2*x(1)*x(2) ];

