I had an error when implementing the Newton's method.
The 2x2 Hessian matrix of the function (1-x)^2 + 100(y-x^2)^2 is
(1200x^2 -400y+2 -400x ; -400x 200)
Where's the error of the implementation? I couldn't plot out the trajectory such that the function converges to 0.
x(i) =rand;
y(i) =rand;
f(i) = (1-x(i))^2 + 100*(y(i)-x(i)^2)^2;
while f(i) > 1e-6 || i >20000
fx(i) = 2*x(i)-2+400*(x(i)^3-x(i)*y(i));
fy(i) = 200*(y(i)-x(i)^2);
H{i} = [1200*x(i)^2-400*y(i)+2 -400*x(i); -400*x(i) 200];
i = i + 1;
tmp = [x(i-1);y(i-1)]-inv(H{i-1})*[fx(i-1);fy(i1)];
x(i) = tmp(1);
y(i) = tmp(2);
f(i) = (1-x(i))^2 + 100*(y(i)-x(i)^2)^2;
end plot(x,y)
xlabel('x'),ylabel('y'),
title('X & Y trajectory ')
figure(2),
plot(f,1:i)
ylabel('No.of Iteration'),
xlabel('f')

 Réponse acceptée

Torsten
Torsten le 9 Mar 2022
i=1;
x(i) =rand;
y(i) =rand;
f(i) = (1-x(i))^2 + 100*(y(i)-x(i)^2)^2;
while f(i) > 1e-6 && i <20000
fx(i) = 2*x(i)-2+400*(x(i)^3-x(i)*y(i));
fy(i) = 200*(y(i)-x(i)^2);
H{i} = [1200*x(i)^2-400*y(i)+2 -400*x(i); -400*x(i) 200];
i = i + 1;
tmp = [x(i-1);y(i-1)]-inv(H{i-1})*[fx(i-1);fy(i-1)];
x(i) = tmp(1);
y(i) = tmp(2);
f(i) = (1-x(i))^2 + 100*(y(i)-x(i)^2)^2;
end
plot(x,y)
xlabel('x'),ylabel('y'),
title('X & Y trajectory ')
figure(2),
plot(f,1:i)
ylabel('No.of Iteration'),
xlabel('f')

Plus de réponses (0)

Catégories

En savoir plus sur Partial Differential Equation Toolbox dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by