Error when implementing Newton's method

1 次查看(过去 30 天)
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')

采纳的回答

Torsten
Torsten 2022-3-9
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')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by