% STEEP  applies the steepest descent algorithm
%
%   [x,it,intx] = steep(x0,f,plot,tol,maxit,maxstep)
%   Outputs the minimum of the function f, by applying the steepest 
%   descent method starting from the point x0.
%   Call sequence: steep(x0,@f,plot,tol,maxit,maxstep), with f like xyfunc.
%
%
%   INPUTS
%   f -> the function see xyfunc for an example.
%   x0 -> the starting point (this is a column vector).
%   plot -> if ==1, plot the figures
%           if ==0, do not plot the figures 
%           standard set to 0.
%   tol -> the accuracy, standard 10^(-4).
%   maxit -> maximum number of iterations, standard 100
%   maxstep -> maximum size of lambda, lambda between [0,maxstep]
%              standard maxstep=2   
%   OUTPUTS
%   x -> The minimum
%   it -> The number of performed iterations
%   intx -> Shows all the intermediate values of x
%
%
%   Software written by Raf Vandebril
%   Raf.Vandebril-at-cs.kuleuven.be
%   Revision Date: 10/05/2010


function [x,it,intx] = steep7(x0,f,plot,tol,maxit,maxstep)

  % These lines of code assign the default values
  % for maxit, tol, plot if no different input values are given
    if nargin < 6
      maxstep=2; 
    end
    if nargin < 5
      maxit=100; 
    end
    if nargin < 4
      tol=10^(-4);
    end
    if nargin < 3
      plot=0;
    end
    

    
  % Assign the starting variables
  it=1; % This variable will count the number of iterations 
  xk=x0; % The improvement in each step stored in xk

 
  % Store every next iterates value (needed for plotting)
  intx=[x0]; 
  
  % Plot the mesh if plot is equal to 1
  if (plot==1)
     steepplotsurf(f); % Plot the surface
     steepplotcont(f,intx); % Plot the initial value of the steepest descent
     disp('Push a key to continue')
     pause
  end
  
  % Evaluate the function in the point xk and retrieve the 
  % function value and the gradient in this point
  [func,grad]=feval(f,xk);


  % the while loop determines when to stop iterating
  % the first condition (norm(grad)), checks how close the gradient
  % equals zero, the second condition poses a constraint on the maximum
  % number of iterations
  while(norm(grad) > tol & it <= maxit)
    
    % COMPLETE (Number of iterations)
    % Count the total number of iterations, change the variable it, which
    % counts the number of iterations to it+1
    it=it+1;
    
    % COMPLETE (Line minimum)
    % The next line computes the minimum lambda in this direction
    % Use the function linesearchmin
    [alpha]=linesearchmin(f,xk,maxstep);
    
    
    % COMPLETE (Define the new iterate)
    % You have now the function value and the gradient in xk, 
    % You also have the value of lambda, update your iterate,
    % This means compute the new xk
    xk=xk-alpha*grad/norm(grad);
        
    % COMPLETE (Store the new value)
    % Store the intermediate value of xk, add the value of xk at the back
    % of the variable intx
    % This is only for plotting purposes
    intx=[intx,xk];
        
    % If plot=1 plot the contours and the approximation
    if (plot==1)
      steepplotcont(f,intx) % Plot the figure showing how the search evolves
      disp('Push a key to continue')
      pause
    end
    
   
    
    
    % COMPLETE (Compute the new gradient and function value)
    % You have now the new iterate xk, compute the function value
    % of f in xk and the gradient (see how this was done before the loop).
    % This is necessary for the next step in the while loop
    [func,grad]=feval(f,xk);
    
  end
  
  % COMPLETE (Assign your solution to x)
  % Put the solution xk into the variable x, which is sent to the output
  x=xk;

