% Chapter 6: Numerical Techniques
%            For Unconstrained Optimization
%--------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Applied Optimization with Matlab Programming
% Dr. P.Venkataraman
% Second Edition,  John Wiley
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Random Walk Method  for unconstrained minimum
%  two dimension version
%  Zero Order Method
%  6.5.1   Random Walk
%  Side constraints are not checked
%  P. Venkataraman - March 2008
%--------------------------------------------------
% A Wrapper Script for the Random Walk Method
% applied to Example 6.2
%************************************
% requires:     	       RandomWalk.m
%                          UpperBound_nVar.m
%						   GoldSection_nVar.m
% and the problem m-file:  Example6_2.m
% AND
% RandomWalk('Example6_2',[0.5 0.5],30, 0.001, 0,1 ,10)
%--------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% management functions
clear  % clear all variable/information in the workspace - use CAUTION
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  % don't report any warnings like divide by zero etc.

%%%   Data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global xs fs delx1 delx2  % information returned from random walk 
%               is used for printing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% monitor cpu time
starttime = cputime;
% delx1 = 2; delx2 = 2;
%%%%   call Random walk   --------------------------
%RandomWalk('Example6_1a',[0 0],1000, 1e-08, 0,1 ,20);
RandomWalk('Example6_1a',[0.5 0.5], 20, 0.001, 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.2\n')
fprintf('**************\n')

fprintf('\niterations    x(1)        x(2)        f    \n')
fprintf('-----------------------------------------------')

for i = 1:length(fs)
    fprintf('\n %3i       %8.5f     %8.5f     %8.5f',i,xs(i,:),fs(i))
end
    

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


