# Is this code good enough for illustrate gradient descent

2 views (last 30 days)
Dinh Le Dung on 6 Aug 2022
Edited: Adam Danz on 7 Aug 2022
This is my code for 15 iterative steps
clf
X = -3:0.1:3;
[X,Y] = meshgrid(X);
Z = 4*X.^2-4*X.*Y+2*Y.^2;
surf(X,Y,Z,'FaceColor','c','FaceAlpha',0.3,'EdgeColor','none');
hold on
x(1) = 2; % initial value of x
y(1) = 3; % initial value of y
z(1) = 4*(x(1)).^2-4*(x(1)).*(y(1))+2*(y(1)).^2;
stepsize = 0.1;
for i = 1:15
zx = 8*x(i)-4*y(i);
zy =-4*x(i)+4*y(i);
x(i+1) = x(i) - stepsize*zx; %gradient descent
y(i+1) = y(i) - stepsize*zy;
z(i+1) = 4*(x(i+1)).^2-4*(x(i+1)).*(y(i+1))+2*(y(i+1)).^2;
end
plot3(x,y,z,'o','Markersize',3,'Color','red')
hold off
axis([min(x),max(x), min(y),max(y), min(z), max(z)]);
rotate3d on;
xlabel x; ylabel y; zlabel z;
I get this figure
I dont know if this illustration good enough
##### 2 CommentsShowHide 1 older comment
Adam Danz on 6 Aug 2022
Edited: Adam Danz on 7 Aug 2022
In my opinion, instead of setting EdgeColor to None, it would look better if you set EdgeAlpha to something like 0.4. It will help to show the 2D structure of the surface. You may want to set the viewing angle (view) if you need a specific vantage point.