% CLASS6NOV  Solve the pair of equations
%            x^3 + xy - y^3 = 5
%           e^(xy) - sin(y) = arctan(x)
% using an initial guess generated from a graph
% and Newton's method (for systems)

% the two functions which we want to be zero; F(x) = 0:
F = @(x) [x(1).^3 + x(1).*x(2) - x(2).^3 - 5;
          exp(x(1).*x(2)) - sin(x(2)) - atan(x(1))]

% the Jacobian of F; J_ij = d F_i / d x_j
J = @(x) [3*x(1).^2 + x(2),                        x(1) - 3*x(2).^2;
          exp(x(1).*x(2)).*x(2) - 1./(1+x(1).^2),  exp(x(1).*x(2) ).*x(1) - cos(x(2))]

% see  curves.m  to graph the curves in [-3,3] x [-3,3]
% this initial guess from there:
x=1.69, y=0.04


% Newton's method is   0 = F(x_n) + J(x_n) (x_{n+1} - x_n):
format long
X = [x; y]              % initial guess as a vector
for n = 1:10
  dX = J(X) \ (-F(X));  % solve system using Gauss elimination
  X = X + dX
end
