Gradient Descent for function with multiple variables

10 次查看(过去 30 天)
I am trying to apply gradient descent for the following function, however it does not produce a surface plot showing the decent, and only displays a red line, what might be the error?
% Minimise the function z = (3-x)^2 + 30(y-(x^2))^2 using gradient descent,
% using an initial starting point of (0,0)
% My implementation:
X = -8:0.1:8;
Y = -8:0.1:8;
[X,Y] = meshgrid(X,Y);
Z =(3-X).^2 + 30*((Y-(X.^2)).^2);
surf(X,Y,Z)
hold on
x(1) = 0; % initial value of x
y(1) = 0; % initial value of y
z(1) = ((3-x(1)).^2) + (30.*(y(1)-(x(1).^2)).^2);
stepsize = 0.1;
for i = 1:30
zx = 120*x(i)^3 + 2*x(i) - 120*x(i)*y(i) - 6;
zy = 60*y(i) - 60*x(i)^2;
x(i+1) = x(i) - stepsize*zx; %gradient descent
y(i+1) = y(i) - stepsize*zy;
z(i+1) = ((3-x(i+1)).^2) + (30.*(y(i+1)-(x(i+1).^2)).^2);
end
plot3(x,y,z,'Markersize',10,'Color','red')
hold off

采纳的回答

Matt J
Matt J 2022-4-12
编辑:Matt J 2022-4-12
Mainly, your step size is too big and you are plotting over too large a range.
X = -3:0.01:3;
[X,Y] = meshgrid(X);
Z =(3-X).^2 + 30*((Y-(X.^2)).^2);
surf(X,Y,Z,'FaceColor','c','FaceAlpha',0.3,'EdgeColor','none');
hold on
x(1) = 0; % initial value of x
y(1) = 0; % initial value of y
z(1) = ((3-x(1)).^2) + (30.*(y(1)-(x(1).^2)).^2);
stepsize = 0.01;
for i = 1:60
zx = 120*x(i)^3 + 2*x(i) - 120*x(i)*y(i) - 6;
zy = 60*y(i) - 60*x(i)^2;
x(i+1) = x(i) - stepsize*zx; %gradient descent
y(i+1) = y(i) - stepsize*zy;
z(i+1) = ((3-x(i+1)).^2) + (30.*(y(i+1)-(x(i+1).^2)).^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)]);
view(154,34)
xlabel x; ylabel y; zlabel z;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by