Is this code good enough for illustrate gradient descent

조회 수: 2 (최근 30일)
Dinh Le Dung
Dinh Le Dung 2022년 8월 6일
편집: Adam Danz 2022년 8월 7일
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
John D'Errico
John D'Errico 2022년 8월 6일
Is it good enough? Why not? Does it show the indicated behavior? It seems to do so. Did you write an explanation? (I don't see one, but I don't case as that is not a MATLAB question.) Is there any discussion of the behavior at the end? I don't see any, but in either case, such a discussion would be off-topic for Answers, as having nothing to do with MATLAB.
Surely the only one who can make an assessment of perfection is your teacher, who will grade your work? After all, suppose we said it was incredibly good, but your teacher disagrees? Who is right? (The one who will assign a grade.)
Adam Danz
Adam Danz 2022년 8월 6일
편집: Adam Danz 2022년 8월 7일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by