% SteepestDescend.m Implemenation of Steepest Descend Method
%
% Usage: modify the starting point, and function pointer below.
%
% Author: Sven Leyffer, Argonne National Laboratory, 2016.
%-----------------------------------------------------------------------

% ... initialization: set initial point, and assing function pointer
n        = 2;
x        = [-1.5;0.5];
fun      = @rosenbrock;
%% x        = [1;1];
%% fun      = @powell;
%% PrntAlg  = 1;      % ... indicate whether or not to print the progress 

% ... set up print of progress ... CHOOSE FUNCTIO BELOW
if (PrntAlg == 1)
   figure(1); clf;
   % ... print rosenbrock
   xx = -2:0.04:2;
   [xx,yy] = meshgrid(xx,xx);
   ff = 100*(xx.^2 - yy).^2 + (xx - 1).^2;
   contour(xx,yy,ff,40); hold on;
   title('Progress of Steepest Descend on Rosenbrock Function');
   %% % ... print powell
   %% xx = -2:0.04:2;
   %% [xx,yy] = meshgrid(xx,xx);
   %% ff = xx.^4   + xx.*yy + (1+yy).^2;
   %% contour(xx,yy,ff,15); hold on;
   %% title('Progress of Steepest Descend on Powell Function');
end

% ... define the constants of the search
sigma  = 0.1;      % ... sufficient reduction parameter
OptTol = 1e-4;     % ... loose optimality tolerance
kmax   = 1000;     % ... maximum number of iterations
fail   = 0;        % ... failure code for Armijo search

% ... set up output format 
format1 = '%5i %12.6g %12.6g %12.6g %5i \n';
fprintf(' iter    objective      || g ||        alpha LiFail\n');

% ... initialization
k      = 0;
[f,g]  = feval( fun, x );
fstart = f;
xstart = x;
s      = zeros(n,1);

while ( (norm(g) > OptTol) & (k < kmax) )
   
  % ... steepest descend direction
  s = -g;     

  % ... Armijo line search
  [alpha, j, fail] = ArmijoLineSearch( x, s, g, fun );
  
  % ... exist, if line search failed (info <> 0)
  if (fail ~= 0) 
    break;
  end; % if

  % ... print progress of method
  if (PrntAlg == 1)
     plot([x(1),x(1)+alpha*s(1)], [x(2),x(2)+alpha*s(2)]);
  end % if
 
  % ... update iterate & re-evaluate function 
  x = x + alpha*s;
  k = k+1;
  [f,g] = feval( fun, x );

  % ... print progress
  fprintf(format1, k, f, norm(g), alpha, fail);

end; % while 

if (fail ~= 0) 
  fprintf(format1, k, f, norm(g), alpha, fail);
end; % if

% ... display the result
fail   = fail
x      = x
fmin   = f
fstart = fstart
