% Chapter 6: Numerical Techniques
%            For Unconstrained Optimization
%--------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Applied Optimization with Matlab Programming
% Dr. P.Venkataraman
% Second Edition,  John Wiley
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Conjugate Gradient Method  for unconstrained minimum
%  two dimension version
%  First Order Method
%  6.5.3  Conjuagte Gradient
%  Side constraints are not checked
%  P. Venkataraman - March 2008
%--------------------------------------------------
% A Wrapper Script  for the Conjugate Gradient Method
% applied to Example 6.4
%************************************
% requires:     	       ConjugateGradient.m
%                          UpperBound_nVar.m
%						   GoldSection_nVar.m
% and the problem m-file:  Example6_1.m
% AND
%   ConjugateGradient('Example6_4',[0.5 0.5],20, 1e-08,0,1,20)
%************************************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% 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 xcg fcg acg convgcg lenXcg delx1 delx2  % information returned from 
%               ConjugateGradient is used for printing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% monitor cpu time
starttime = cputime;
delx1 = 10;  delx2 = 10;
%%%%   call Conjugate Gradient   --------------------------
ConjugateGradient('Example6_4',[10 2],30, 1e-08, 0,1 ,20);
%%%%---------------------------------------------------

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

%%%  plot iterations here
fprintf('\nExample 6.4 (Conjugate Gradient)\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(fcg)
    fprintf('\n %3i  %7.5f   %7.5f   %7.5f   %7.3e   %7.3e   %7.3e', ...
        i,xcg(i,:),fcg(i),convgcg(i),acg(i),lenXcg(i))
end
    

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



