Minimize a function using gradient descent

65 Ansichten (letzte 30 Tage)
Shikhar Singh
Shikhar Singh am 11 Apr. 2022
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);

Akzeptierte Antwort

Torsten
Torsten am 11 Apr. 2022
Bearbeitet: Torsten am 11 Apr. 2022
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

Weitere Antworten (1)

Sam Chak
Sam Chak am 11 Apr. 2022
Bearbeitet: Sam Chak am 11 Apr. 2022
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

Kategorien

Mehr zu Networks finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by