%%NEWTON2.M  Newton's method for nonlinear systems root-finding. 
% NH 2019

f = @(x) [x(1)^5 + x(2)^5 - x(1)*x(2)  ;           % Function 
          cos(x(1)-x(2)) - x(1) - x(2)];
J = @(x) [5*x(1)^4 - x(2),    5*x(2)^4 - x(1)    ; % Jacobian
         -sin(x(1)-x(2)) - 1, sin(x(1)-x(2)) - 1];
x = [0.2 ; 0.7];                                   % Guess
for k = 1:5
    dx = -J(x)\f(x);                               % Update
    x = x + dx;                                    % Step
    disp(x')
end
