Error when implementing Newton's method

19 ビュー (過去 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 Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by