clc
clear all
cla
close all
global gval
global fval
global xduration
x0=[2.1,-2]; %start point
mu=0.0001; % mu constant of SD
e=.001; % max error lenght
maxiter=2*10^5;
gval=0;
fval=0;
xopt=SD(x0,mu,@grad,maxiter,e)
% xopt=SD(x0,mu,@(x1,x2) [2*x1 - 400*x1*(- x1.^2 + x2) - 2,- 200*x1.^2 + 200*x2],maxiter,e)
fopt=func(xopt(1,1),xopt(1,2)) %optimum cost of function
fval %number of function used
gval %number of gradian function used
funcduration=func(xduration(:,1),xduration(:,2)); %seyre taghirate tabe
% ##################### Figures ###########################
v=-3:.1:3; % mesh size of ploted contour
[x1,x2]=meshgrid(v);
cost=func(x1,x2);
figure
meshz(x1,x2,cost);
figure
V=[0,.0001,.001,.01,0.1,0.2,1,10,20,50,200,500,10^3:10^3:10^4];
% rasm contour haye tabe be ezaye maghadir bordar V
%# you can use n ,instead of V to plot n contour of function
contour(x1,x2,cost,V);
hold on
plot(1,1,'m.','markersize',20); % optimum point of function is [1,1]
plot(xduration(:,1),xduration(:,2),'r') %rasme seyre taghirate x1 va x2