Error when implementing Newton's method

조회 수: 1 (최근 30일)
tammy green
tammy green 2022년 3월 9일
답변: Torsten 2022년 3월 9일
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에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by