Code covered by the BSD License

# Derivative-based Optimization

### Nassim Khaled (view profile)

Derivative-based Optimization using The Gradient Descent

% %%%%%Optimization Project-Derivative Based Optimization%%%%%%%%%
%%This script finds the local maximum by using the gradient of the function% %%%
clc
clear all
close all
fxy=inline('3*(1-x)^2*exp(-x^2-(y+1)^2)-10*(x/5-x^3-y^5)*exp(-x^2-y^2)-1/3*exp(-(x+1)^2-y^2)')
syms x y
f=3*(1-x)^2*exp(-x^2-(y+1)^2)-10*(x/5-x^3-y^5)*exp(-x^2-y^2)-1/3*exp(-(x+1)^2-y^2);
x=1;
y=-.9;
difference=10
neu=0.01;
Vold=[x ;y]
f_old=fxy(x,y)
E=f_old;

axes1 = axes(...
'FontName','Times New Roman',...
'FontSize',20,...
'FontWeight','bold','Parent',figure);
title(strcat('This is the function ascending with time'))
hold on
ind=1;
i=0;
X=x;
Y=y;
f_new=f_old-0.00001;
diff_new_old=1;

while   (diff_new_old>=0 ) & (difference>10^-9)
i=i+1;
ind=[ind i];
difference=sum((Vnew-Vold).^2);
Vold=Vnew;
f_new=fxy(Vnew(1),Vnew(2));
diff_new_old=f_new-f_old;
f_old=f_new;
F=eval(f);
E=[E F];
plot(ind,E,'-')
pause(.001)
x=Vold(1);
y=Vold(2);
X(i)=x;
Y(i)=y;
Z(i)=fxy(x,y);

end

x_max=X(i)
y_max=Y(i)
fxy(x_max,y_max)

hold off

[x,y] = meshgrid(-3:0.05:3,-3:0.05:3);
f1=3.*(1-x).^2.*exp(-x.^2-(y+1).^2)-10.*(x./5-x.^3-y.^5).*exp(-x.^2-y.^2)-1/3.*exp(-(x+1).^2-y.^2);
axes1 = axes(...
'FontName','Times New Roman',...
'FontSize',20,...
'FontWeight','bold','Parent',figure);
title(strcat('This is the path followed by the optimizer to reach the local maximum[ ',num2str(num2str([x_max y_max])),']'))
view(axes1,[-37.5 30]);
grid(axes1,'on');
hold(axes1,'all');
mesh(x,y,f1)
hold on
plot3(X,Y,Z,'-*r','LineWidth',2) %,...
hold off