Gradient Descent for function with multiple variables

7 ビュー (過去 30 日間)
Shikhar Singh
Shikhar Singh 2022 年 4 月 12 日
編集済み: Matt J 2022 年 4 月 12 日
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 Center および File ExchangeMathematics についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by