How to find the minimum using Newton method (matlab)

7 vues (au cours des 30 derniers jours)
Nikoo Azarm
Nikoo Azarm le 2 Déc 2017
I am given this function: f(x, y) = 1(x − 1)2 + 1(10(y − x2))2 + 1y2 and I tried to write the code below to find the minimum but it doesn't seem to work.
syms a;
syms b;
g=gradient(vrosenbrock(a,b));
x=-1;
y=-1;
z=0;
i=1;
H=hessian(vrosenbrock(a,b));
while norm(subs(g,[a,b],[x,y])) > 10e-3
z=[x,y]-((subs(inv(H),[a,b],[x,y]))*((subs(g,[a,b],[x,y])))).' ;
x=z(1); y=z(2);
i=i+1;
end
[x y]
And as function I've saved this already: function z = vrosenbrock(x,y)
FUN = 1;
if FUN == 1
z = 0.5*(10*(y - x.^2)).^2 + 0.5*(1-x).^2 + 0.5*y.^2;
elseif FUN == 2
z = (10*(y - x.^2)).^2 + (1-x).^2;
end
end

Réponses (1)

Ayush Modi
Ayush Modi le 27 Août 2024
Hi Nikoo,
I understand you are trying to implement the Newton's method for finding the minimum of the given function.
Here is the example code to get you started:
syms a b;
% Define the function - taking example function
vrosenbrock = @(x, y) 0.5*(10*(y - x.^2)).^2 + 0.5*(1-x).^2 + 0.5*y.^2;
% Compute the gradient and Hessian
g = gradient(vrosenbrock(a, b), [a, b]);
H = hessian(vrosenbrock(a, b), [a, b]);
% Initial guess
x = -1;
y = -1;
% Iteration counter
i = 1;
while norm(double(subs(g, [a, b], [x, y]))) > 1e-3
grad_val = double(subs(g, [a, b], [x, y]));
hess_val = double(subs(H, [a, b], [x, y]));
z = [x; y] - hess_val \ grad_val;;
x = z(1);
y = z(2);
i = i + 1;
end
disp(['Minimum found at x = ', num2str(x), ', y = ', num2str(y)]);
Minimum found at x = 0.59108, y = 0.34592
Refer to the following MathWorks documentation for more information on (\) i.e. backslash operator in MATLAB:

Community Treasure Hunt

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

Start Hunting!

Translated by