function P3()
% Demos the method of Steepest Descent (with exact line search) and
% the method of Newton
clc
SD(-1,1)
Newton(-1,1)
SD(0,1)
Newton(0,1)
SD(2,1)
Newton(2,1)

  function SD(x0,y0)
% Applies Steepest Descent with starting value (x0,y0)
fprintf('\n\nSteepest Descent with (x0,y0) = (%5.2f,%5.2f)\n\n',x0,y0)
x = x0; y = y0;
its = 0;
while f(x,y)>.0001 && its<=20
    g = Grad(x,y);
    tStar = fminbnd(@(t) phi(t,x,y,g),0,1);
    x = x - tStar*g(1);
    y = y - tStar*g(2);
    fprintf('%12.8f  %12.8f   %12.8f\n',x,y,f(x,y))
    its = its+1;
end

  function Newton(x0,y0)
% Applies Newton's method with starting value (x0,y0).
fprintf('\n\nNewton with (x0,y0) = (%5.2f,%5.2f)\n\n',x0,y0)
x = x0; y = y0;
its = 0;
while f(x,y)>.0001 && its<=20
   s = Hess(x,y)\Grad(x,y);
   x = x-s(1);
   y = y-s(2);
   fprintf('%12.8f  %12.8f   %12.8f\n',x,y,f(x,y))
   its = its+1;
end

  function z = phi(t,x0,y0,g)
% The line search function
z= f(x0-t*g(1),y0-t*g(2));

  function z = f(x,y) 
% The object ive function that is minimized at (x,y) = (1,1)
z = 100*(y-x^2)^2 + (1-x)^2;

  function g = Grad(x,y)
% The gradient of f at (x,y)
fx = 400*x*(x^2-y)+2*(x-1);
fy = 200*(y-x^2);
g = [fx;fy];

  function H = Hess(x,y)
% The Hessian of f at (x,y)
fxx = 1200*x^2+2-400*y;
fxy = -400*x;
fyy = 200;
H = [fxx fxy;fxy fyy];

% -1 for incorrect gradient
% -1 for incorrect Hessian
% up to -2 for lack of useful output
% up to -2 for incorrect implementation of Newton method
% up to -2 for incorrect implementation of steepest descent
% up to -1 for loop termination conditions not as specified
