  function [xnew,fnew,gnew] = SDStep(fName,gName,xc,fc,gc,plist,Lmax,auto)
%
% Pre:
%     fName     the name of a function f(x,plist) that accepts column
%               n-vectors and returns a scalar.
%     gName     the name of a function g(x,plist) that returns the
%               gradient of f at x.
%        xc     a column-vector, an approximate minimizer
%        fc     f(xc,plist)
%        gc     g(xc,plist)
%     plist     orbit parameters for f and g
%      Lmax     max step length
%      auto     if auto is nonzero, then automated line search
%
% Post:
%      xnew     a column n-vector, an improved minimizer
%      fnew     f(xnew,plist)
%      gnew     g(new,plist)
%
   
   nplotvals = 20;
   
   % Get the Steepest Descent Search Direction
 
   sc = -gc;
   
   % Line Search
   
   % Try to get L<=Lmax so xc+L*sc is at least as good as xc.
   
   L = Lmax;
   Halvings = 0;
   fL = feval(fName,xc+L*sc,plist);
   while (fL>=fc) & Halvings<=10
      L = L/2;
	  Halvings = Halvings+1;
	  fL = feval(fName,xc+L*sc,plist);
   end  
   
   % Sample across [0.L]
   
   lambdavals = linspace(0,L,nplotvals);
   fvals = zeros(1,nplotvals);
   for k=1:nplotvals
       fvals(k) = feval(fName,xc+lambdavals(k)*sc,plist);
   end
   
  
   if auto==0
      % Manual line search.
      plot(lambdavals,fvals);
      xlabel('lambda');
      ylabel('f(xc+lambda*sc)');
      title('Enter the Best lambda value.');
      [lambda,y] = ginput(1);
      xnew = xc+lambda*sc;
      fnew = feval(fName,xnew,plist);
	  gnew = feval(gName,xnew,plist);
   else
      % Automated line search.
      [fnew,i] = min(fvals(1:nplotvals));
	  xnew = xc + lambdavals(i(1))*sc;
	  gnew = feval(gName,xnew,plist);
   end
	  

   
   
   
