% Chapter 6: Numerical Techniques
%            For Unconstrained Optimization
%--------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Applied Optimization with Matlab Programming
% Dr. P.Venkataraman
% Second Edition,  John Wiley
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Steepest Descent Method  for unconstrained minimum
%  two dimension version
%  First Order Method
%  6.3.1  Steepest Descent
%  Side constraints are not checked
%  P. Venkataraman - March 2008
%--------------------------------------------------
% A Wrapper Script  for the Random Walk Method
% applied to Example 6.1
% Example 6.1:
% f = 3*sin(0.5 + 0.25*x1*x2)*cosx1
%************************************
% requires:     	       RandomWalk.m
%                          UpperBound_nVar.m
%						   GoldSection_nVar.m
% and the problem m-file:  Example6_1b.m
% AND
%       SteepestDescent(functname, ...
%            dvar0,niter,tol,lowbound,intvl,ntrials)
%************************************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% management functions
clear  % clear all variable/information in the workspace - use CAUTION
clear global  %  again use caution - clears global information
clc    % position the cursor at the top of the screen
close   %  closes the figure window
format compact  % avoid skipping a line when writing to the command window
warning off  %#ok<WNOFF> % don't report any warnings like divide by zero etc.

%%%   Data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global xsd fsd asd convgsd lenXsd    % information returned from 
%               SteepestDescent is used for printing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% monitor cpu time
starttime = cputime;

%%%%   call Steepest Descent   --------------------------
SteepestDescent('Example6_1b',[0.5 0.5],30, 1e-08, 0,1 ,10);
%%%%---------------------------------------------------

%%%   the objective function contour and iterations are already 
%%% plotted in RandomWalk.   No figures are necessary here

%%%  plot iterations here
fprintf('\nExample 6.1b (Steepest Descent)\n')
fprintf('********************************\n')

%%% convergence is  S'*S
%%% design change is sum(delX'*delX)
fprintf('\n - The design vector,function value, KT condition etc. \n    during the iterations\n')

fprintf('\niter  x(1)       x(2)        f     KT Cond         alpha      DesignChange  \n')
fprintf('------------------------------------------------------------------------------')

for i = 1:length(fsd)
    fprintf('\n %3i  %7.5f   %7.5f   %7.5f   %7.3e   %7.3e   %7.3e', ...
        i,xsd(i,:),fsd(i),convgsd(i),asd(i),lenXsd(i))
end
    

%%% print iter
totaltime = cputime - starttime;
fprintf('\n\nTotal cpu time (s)= %7.4f \n\n',totaltime)



