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;
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.
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:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.