% Chapter 6: Numerical Techniques
%            For Unconstrained Optimization
%--------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Applied Optimization with Matlab Programming
% Dr. P.Venkataraman
% Second Edition,  John Wiley
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%--------------------------------------------------
% Example 6.1: Powell's Method
%  6.2.3
%  Analytical computation of numbers in Section 6.2.3
%------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms x1 x2 alpha	% symbolic variables
f = 3*(sin(0.5 +0.25*x1*x2))*cos(x1)

% computing the derivative
dfdx1 = diff(f,x1)
dfdx2 = diff(f,x2)

%%%  You will have to enter start vector X0 and seacrh direction
%%% the code will determine analytical solution 
%%%  If alpha is negative - reverse the search direction

X0 = [3.1653 1.3532]'
S1 = [2.7395 0.8222]' %-[0.5 0.5]'
%S1 = [0 1]'
S1 = -S1
% S1 = .0533*[0 1]' + 0.0065*[2.7395 0.8222]'
X1 = X0 + alpha*S1
fa = subs(f,{x1,x2},{X1(1),X1(2)})
dfa = diff(fa,alpha)
astar = double(solve(dfa))

for i = 1:length(astar)
    if (astar(i) > 0) & (astar(i) < 3)
    fas = subs(fa,astar(i))
    as = astar(i)
    X1s = subs(X1,as)
    end
end

