Gradient Descent for function with multiple variables
조회 수: 21 (최근 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
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!