Minimize a function using gradient descent

62 次查看(过去 30 天)
How can we minimise the following function using gradient descent (using a for loop for iterations and a surface plot to display a graph that shows the minimisation)
% initial values: x = y = 2
z = 2*(x^2) + 3*(y^2);

采纳的回答

Torsten
Torsten 2022-4-11
编辑:Torsten 2022-4-11
X = -2:0.1:2;
Y = -2:0.1:2;
[X,Y] = meshgrid(X,Y);
Z = 2*X.^2+3*Y.^2;
surf(X,Y,Z)
hold on
x(1) = 2; % initial value of x
y(1) = 2; % initial value of y
z(1) = 2.*x(1).^2 + 3.*y(1).^2;
stepsize = 0.1;
for i = 1:30
zx = 4*x(i);
zy = 6*y(i);
x(i+1) = x(i) - stepsize*zx; %gradient descent
y(i+1) = y(i) - stepsize*zy;
z(i+1) = 2.*x(i+1).^2 + 3.*y(i+1).^2
end
plot3(x,y,z,'Markersize',10,'Color','red')
hold off

更多回答(1 个)

Sam Chak
Sam Chak 2022-4-11
编辑:Sam Chak 2022-4-11
Let us visualize and formulate the minimization problem first. So you want to start descending from the point , circled in the image. The contour plot can give you an estimation where you are heading to from the starting point.
f = @(x,y) 2*(x.^2) + 3*(y.^2);
[x,y] = meshgrid(-2.5:0.25:2.5, -2.5:0.25:2.5);
z = f(x, y);
[fx, fy] = gradient(z, 0.25);
cs = contour(x, y, z);
axis square
clabel(cs);
hold on
plot(2, 2, 'ro', 'linewidth', 1.5)
quiver(x, y, -fx, -fy);
hold off
xlabel('x')
ylabel('y')
We try to first obtain the solution with the fminsearch() function. Then, we can write the gradient descent algorithm to compare with the result.
fun = @(x) 2*(x(1).^2) + 3*(x(2).^2);
[x, fval] = fminsearch(fun, [2, 2])
x =
1.0e-04 *
0.0707 -0.3490
fval =
3.7533e-09
Surface plot with the mesh() function:
[x, y] = meshgrid(-3:0.375:3);
z = 2*(x.^2) + 3*(y.^2);
[u, v] = gradient(z, 0.375);
w = 1;
magnitude = sqrt(u.*u + v.*v + w.*w);
u = u./magnitude;
v = v./magnitude;
w = w./magnitude;
mesh(x, y, z)
axis square
xlabel('x');
ylabel('y');
zlabel('z');
hold on
quiver3(x, y, z, -0.75*u, -0.75*v, w, 0)
hold off

类别

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