%NEWTONS DESCENT METHOD FOR COMPUTING MINS/MAXES of (CORRECTED)
%f(x,y) = x^2+y^2/2-xy+x^3+x^4/2


clear all

%SAMPLE RUNS for different initial conditions

%These initial conditions converge to a max at (-.5,-.5)
%x(1) = -0.5
%x(2) = -.3

%These initial conditions converge to a min at (-1,-1)
x(1) = -1.5
x(2) = -1.3

%These initial conditions converge to a min at (0,0)
%x(1) = .2
%x(2) = 1

%These initial conditions converge to a min at (0,0)
%x(1) = 0.1
%x(2) = 1
x = x'

for k = 1:10
   
   H(1,1) = 2+6*x(1)+6*x(1)^2;
   H(1,2) = -1;
   H(2,1) = -1;
   H(2,2) = 1;
   
   eig(H);
   
   gf(1) = 2*x(1)-x(2)+3*(x(1))^2+2*(x(1))^3;
   gf(2) = x(2)-x(1);
  
   gf
   x = x -inv(H)*gf'

end

extreme1 = x(1)^2+x(2)^2/2-x(1)*x(2)+x(1)^3+x(1)^4/2
x(1) = x(1)+.01;
x(2) = x(2) + .01;
extreme2 = x(1)^2+x(2)^2/2-x(1)*x(2)+x(1)^3+x(1)^4/2