Gradient Descent for function with multiple variables
이전 댓글 표시
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
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

