image thumbnail

Derivative-based Optimization

by

 

Derivative-based Optimization using The Gradient Descent

Gradient_Descent_minimum.m
% %%%%%Optimization Project-Derivative Based Optimization%%%%%%%%%
%%This script finds the local minimum 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);
Grad_F=[diff(f,'x');diff(f,'y')];
eval(Grad_F)
x=1;
y=-.7;
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 descending 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];
    GRAD_F=eval(Grad_F);
    Vnew=Vold-neu.*GRAD_F;
    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(.00001)
    x=Vold(1);
    y=Vold(2);
    X(i)=x;
    Y(i)=y;
    Z(i)=fxy(x,y);
  
end

x_min=X(i-1)
y_min=Y(i-1)
fxy(x_min,y_min)



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 first local minimum [',num2str(num2str([x_min y_min])),']'))
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


Contact us