%gradient descent demo

clf
f = @(x,y) x.^2 + y.^2 + 1;
dfx = @(x,y) 2*x;
dfy = @(x,y) 2*y;
delta = 0.25;

xx = [-6:0.25:6];
yy = [-6:0.25:6];


[X,Y] = meshgrid(xx,yy);

M = 7;

x = zeros(1,M+1);
y = zeros(1,M+1);

x0 = 5;
y0 = 5;

x(1) = x0;
y(1) = y0;

for i=1:M
    x(i+1) = x(i) - delta*dfx(x(i),y(i));
    y(i+1) = y(i) - delta*dfy(x(i),y(i));
    
end 

v = f(x,y);
figure(1);
contour(X,Y,f(X,Y),v)
hold on;
for i=1:M
plot(x(1:i),y(1:i),'r-o')
pause(1.0)
end
hold off
title(['Gradient Descent on z = x^2 + y^2'])
hFig = figure(2);
set(hFig, 'Position', [1500 1000 700 1100])   %[x y width height])
figure(2)
contour3(X,Y,f(X,Y),v)
surface(X,Y,f(X,Y),'EdgeColor',[.8 .8 .8],'FaceColor','none')
set(gca,'DataAspectRatio',[1 1 3.5])
grid off
view(-15,50)
hold on;
for i=1:M
plot3(x(1:i),y(1:i),f(x(1:i),y(1:i)),'r-o')
pause(1.0)
end
hold off
title(['Gradient Descent on ' 'z = x^2 + y^2 '])


