Gradient Descent for function with multiple variables

6 views (last 30 days)
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

Accepted Answer

Matt J
Matt J on 12 Apr 2022
Edited: Matt J on 12 Apr 2022
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;

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!