x = [-1.2; 1];
gdparams = struct('maxit',5000,'tol',1.0e-4);
nmparams = struct('maxit',100,'tol',1.0e-9);


fprintf('Gradient Descent\n');
fprintf('----------------\n');
fprintf('Problem a\n');
[inform,xnew] = GradientDescent(@obja,x,gdparams);
if inform.status == 0
  fprintf('CONVERGENCE FAILURE: %d steps were taken without\n', inform.iter);
  fprintf('gradient size decreasing below %8.4g.\n', gdparams.tol);
else
  fprintf('Success: %d steps taken\n', inform.iter);
end
fprintf('  Ending point: '); fprintf('%8.4g ',xnew);
fprintf('\n  Ending value: '); fprintf('%8.4g ',feval(@obja,xnew,1));
fprintf('\n  Ending gradient: '); fprintf('%8.4g ',feval(@obja,xnew,2));
fprintf('\n  Norm of ending gradient: %8.4g\n\n\n', norm(feval(@obja,xnew,2)));

fprintf('Problem b\n');
[inform,xnew] = GradientDescent(@objb,x,gdparams);
if inform.status == 0
  fprintf('CONVERGENCE FAILURE: %d steps were taken without\n', inform.iter);
  fprintf('gradient size decreasing below %8.4g.\n', gdparams.tol);
else
  fprintf('Success: %d steps taken\n', inform.iter);
end
fprintf('  Ending point: '); fprintf('%8.4g ',xnew);
fprintf('\n  Ending value: '); fprintf('%8.4g ',feval(@objb,xnew,1));
fprintf('\n  Ending gradient: '); fprintf('%8.4g ',feval(@objb,xnew,2));
fprintf('\n  Norm of ending gradient: %8.4g\n\n\n', norm(feval(@objb,xnew,2)));

fprintf('Problem c\n');
[inform,xnew] = GradientDescent(@objc,x,gdparams);
if inform.status == 0
  fprintf('CONVERGENCE FAILURE: %d steps were taken without\n', inform.iter);
  fprintf('gradient size decreasing below %8.4g.\n', gdparams.tol);
else
  fprintf('Success: %d steps taken\n', inform.iter);
end
fprintf('  Ending point: '); fprintf('%8.4g ',xnew);
fprintf('\n  Ending value: '); fprintf('%8.4g ',feval(@objc,xnew,1));
fprintf('\n  Ending gradient: '); fprintf('%8.4g ',feval(@objc,xnew,2));
fprintf('\n  Norm of ending gradient: %8.4g\n\n\n', norm(feval(@objc,xnew,2)));

fprintf('Problem d\n');
[inform,xnew] = GradientDescent(@objd,x,gdparams);

if inform.status == 0
  fprintf('CONVERGENCE FAILURE: %d steps were taken without\n', inform.iter);
  fprintf('gradient size decreasing below %8.4g.\n', gdparams.tol);
else
  fprintf('Success: %d steps taken\n', inform.iter);
end
fprintf('  Ending point: '); fprintf('%8.4g ',xnew);
fprintf('\n  Ending value: '); fprintf('%8.4g ',feval(@objd,xnew,1));
fprintf('\n  Ending gradient: '); fprintf('%8.4g ',feval(@objd,xnew,2));
fprintf('\n  Norm of ending gradient: %8.4g\n\n\n', norm(feval(@objd,xnew,2)));





fprintf('Newton Method\n');
fprintf('----------------\n');
fprintf('Problem a\n');
[inform,xnew] = NewtonMethod(@obja,x,nmparams);
if inform.status == 0
  fprintf('CONVERGENCE FAILURE: %d steps were taken without\n', inform.iter);
  fprintf('gradient size decreasing below %8.4g.\n', nmparams.tol);
else
  fprintf('Success: %d steps taken\n', inform.iter);
end
fprintf('  Ending point: '); fprintf('%8.4g ',xnew);
fprintf('\n  Ending value: '); fprintf('%8.4g ',feval(@obja,xnew,1));
fprintf('\n  Ending gradient: '); fprintf('%8.4g ',feval(@obja,xnew,2));
fprintf('\n  Norm of ending gradient: %8.4g\n\n\n', norm(feval(@obja,xnew,2)));

fprintf('Problem b\n');
[inform,xnew] = NewtonMethod(@objb,x,nmparams);
if inform.status == 0
  fprintf('CONVERGENCE FAILURE: %d steps were taken without\n', inform.iter);
  fprintf('gradient size decreasing below %8.4g.\n', nmparams.tol);
else
  fprintf('Success: %d steps taken\n', inform.iter);
end
fprintf('  Ending point: '); fprintf('%8.4g ',xnew);
fprintf('\n  Ending value: '); fprintf('%8.4g ',feval(@objb,xnew,1));
fprintf('\n  Ending gradient: '); fprintf('%8.4g ',feval(@objb,xnew,2));
fprintf('\n  Norm of ending gradient: %8.4g\n\n\n', norm(feval(@objb,xnew,2)));

fprintf('Problem c\n');
[inform,xnew] = NewtonMethod(@objc,x,nmparams);
if inform.status == 0
  fprintf('CONVERGENCE FAILURE: %d steps were taken without\n', inform.iter);
  fprintf('gradient size decreasing below %8.4g.\n', nmparams.tol);
else
  fprintf('Success: %d steps taken\n', inform.iter);
end
fprintf('  Ending point: '); fprintf('%8.4g ',xnew);
fprintf('\n  Ending value: '); fprintf('%8.4g ',feval(@objc,xnew,1));
fprintf('\n  Ending gradient: '); fprintf('%8.4g ',feval(@objc,xnew,2));
fprintf('\n  Norm of ending gradient: %8.4g\n\n\n', norm(feval(@objc,xnew,2)));

fprintf('Problem d\n');
[inform,xnew] = NewtonMethod(@objd,x,nmparams);
if inform.status == 0
  fprintf('CONVERGENCE FAILURE: %d steps were taken without\n', inform.iter);
  fprintf('gradient size decreasing below %8.4g.\n', nmparams.tol);
else
  fprintf('Success: %d steps taken\n', inform.iter);
end
fprintf('  Ending point: '); fprintf('%8.4g ',xnew);
fprintf('\n  Ending value: '); fprintf('%8.4g ',feval(@objd,xnew,1));
fprintf('\n  Ending gradient: '); fprintf('%8.4g ',feval(@objd,xnew,2));
fprintf('\n  Norm of ending gradient: %8.4g\n\n\n', norm(feval(@objd,xnew,2)));
