[英]How to plot advance of gradient descent matlab on a 3D surface
I will try to keep as less code as possible. 我将尝试保留尽可能少的代码。 sorry in advance.
提前对不起。
I have implemented a gradient descent algorithm on the following grid: 我已经在以下网格上实现了梯度下降算法:
[x,y] = meshgrid(-3:.1:3,-3:.1:3);
f = 80.*(x.^4 )+0.01.*(y.^6 );
which I am plotting using: 我正在使用的绘图:
surf(x,y,f); xlabel('x'); ylabel('y'); zlabel('f(x,y)');
print('f.png','-dpng');
hold on;
After which comes the algorithm. 之后是算法。 The
hold on;
hold on;
is there because in each iteration when I have a new x,y
point I want to plot the convergence plot of the algorithm on the face of the original function. 之所以存在,是因为在每次迭代中都有一个新的
x,y
点时,我都希望在原始函数的表面上绘制算法的收敛图。
In other words, I wish each iteration to add a line on the existing plots, describing the descent of the function. 换句话说,我希望每次迭代都在现有图上添加一条线,以描述函数的下降。
How do I plot this? 我该如何绘制?
I am adding my gradient algorithm for reference. 我正在添加渐变算法以供参考。 the quiver method is my failed attempt and any other suggestion is more than welcome:
颤抖方法是我的失败尝试,任何其他建议都值得欢迎:
eta = 0.001;
x0 = 1;
y0 = 1;
eps = 1e-4;
dw = 10;
maxIter=5000000;
itr=1;
f_GD=zeros(maxIter,1);
x = x0;
y= y0;
while itr<maxIter && dw > eps
dx = 4.*80.*(x.^3 );
dy = 6.*0.01.*(y.^5 );
x = x - eta*dx;
y = y - eta*dy;
dw = sqrt(dx^2+dy^2);
f_GD(itr)=80.*(x.^4 )+0.01.*(y.^6 );
quiver(x,y,dx,dy,2,'linewidth',3);
itr=itr+1;
end
hold off;
I have found a solution by simply using the plot3(x,y,z)
function: 我已经找到了解决方案,只需使用
plot3(x,y,z)
函数:
eta = 0.001;
x0 = 1;
y0 = 1;
eps = 1e-4;
dw = 10;
maxIter=5000000;
itr=1;
f_GD=zeros(maxIter,1);
x = x0;
y= y0;
while itr<maxIter && dw > eps
dx = 4.*80.*(x.^3 );
dy = 6.*0.01.*(y.^5 );
x = x - eta*dx;
y = y - eta*dy;
dw = sqrt(dx^2+dy^2);
f_GD(itr)=80.*(x.^4 )+0.01.*(y.^6 );
plot3(x,y,f_GD);
itr=itr+1;
end
hold off;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.