%% Example for Newton Method
% We want to find x=\left[\matrix{x_1 \cr x_2}\right] such that  \left[ \matrix{f_1(x_1,x_2) \cr f_2(x_1,x_2)}\right]
% =  \left[ \matrix{0 \cr 0}\right].
%% Define function f(x) = \left[ \matrix{f_1(x_1,x_2) \cr f_2(x_1,x_2)}\right]
% = \left[ \matrix{2x_1 + x_1 x_2 -2\cr 2x_2-x_1x_2^2-2}\right]

f = @(x) [2*x(1)+x(1)*x(2)-2 ; 2*x(2)-x(1)*x(2)^2-2 ];

%% Define Jacobian f^{\prime } \left(x\right)=\left\lbrack \begin{array}{cc}
% \frac{\partial \;f_1 }{\partial \;x_1 } & \frac{\partial \;f_1 }{\partial \;x_2 }\\
% \frac{\partial \;f_2 }{\partial \;x_1 } & \frac{\partial \;f_2 }{\partial \;x_2 }
% \end{array}\right\rbrack =\left\lbrack \begin{array}{cc}
% 2+x_2  & x_1 \\
% -x_2^2  & 2-2x_{1\;} x_2
% \end{array}\right\rbrack

fp = @(x) [2+x(2), x(1); -x(2)^2, 2-2*x(1)*x(2) ];

%
%% Perform Newton iteration
% We start with the initial guess x^{\left(0\right)} =\left\lbrack \begin{array}{c}
% 0\\
% 0
% \end{array}\right\rbrack.

x = [0;0];       % initial guess
xs = [.5; 2];    % solution (used to print errors below)
for j=1:9
  b = f(x);      % evaluate f
  A = fp(x);     % evaluate Jacobian matrix
  d = -A\b;      % solve linear system A d = -b
  x = x + d;
  error = norm(x-xs,Inf);
  fprintf('step %g:   x = [ %-17.15g; %-17.15g],   error = %8.3e\n',j,x,error)
end

% We see that the errors decrease like {10}^{-1} ,{10}^{-2} ,{10}^{-4} ,{10}^{-8} ,{10}^{-16}, i.e. we have convergence of order 2.