  function [xnew,fnew,gnew] = SDaskel(xc,fc,gc,Lmax,auto)
% [xnew,fnew,gnew] = SDStep(xc,f,g,Lmax)
% Generates a steepest descent step.
%
%  Arguments:
%    input:
%     xc     a column-vector, an approximate minimizer
%     fc     f(xc)
%     gc     g(xc)
%   Lmax     max step length
%    output:
%   xnew     a column n-vector, an improved minimizer
%   fnew     f(xnew)
%   gnew     g(new)
%

nplotvals = 20;

% Get the Steepest Descent Search Direction
sc = -gc;
sc = -gc;
% Line Search
% Manual search:
if auto==0
  % Sample across [0,L]
  L=Lmax;
  lambdavals = linspace(0,L,nplotvals);
  fvals = zeros(1,nplotvals);
  for k=1:nplotvals
    fvals(k) = objf(xc+lambdavals(k)*sc);
  end
  figure
  plot(lambdavals,fvals)

else
  % Automatic search by interval halving.
  % Try to get L<=Lmax so xc+L*sc is at least as good as xc.
  L = Lmax;
  Halvings = 0;
  fL = objf(xc+L*sc);
  while (fL>=fc) & Halvings<=10
    L = L/2;
    Halvings = Halvings+1;
    fL = objf(xc+L*sc);
   end  
% Sample across [0,L]
   lambdavals = linspace(0,L,nplotvals);
   fvals = zeros(1,nplotvals);
   for k=1:nplotvals
      fvals(k) = objf(xc+lambdavals(k)*sc);
   end
end

   % Line search.
[fnew,i] = min(fvals(1:nplotvals));
xnew = xc + lambdavals(i(1))*sc;
gnew = objg(xnew);


