Newton's method for two variable functions

8 ビュー (過去 30 日間)
sanyer
sanyer 2021 年 2 月 13 日
コメント済み: sanyer 2021 年 2 月 13 日
I have a problem in which I'm supposed to solve a system using Newton's method, but my function gives the same x and y as an output as I give to it as an input. How do I fix this?
function [x, y] = newton3(x,y)
for N = 1:30
D = inv([4*x^3-2*y^5 4*y^3-10*x*y^4; 6*x^5+2*x 4*y^3]);
f = x^4+y^4-2*x*y^5 ;
g = x^6+x^2+y^4-4 ;
z = [x y]' ;
z = z - D*[f g]' ;
x = z(1)
y = z(2)
end
end

採用された回答

Alan Stevens
Alan Stevens 2021 年 2 月 13 日
編集済み: Alan Stevens 2021 年 2 月 13 日
It depends on your initial guesses. Some work, some don't (not unusual for Newton's method!): Also, better practice to set D to be the Jacobian, rather than its inverse, then use backslash division in the iteration see below:
x = 2; y = 2;
[x,y] = newton3(x,y);
disp([x y])
disp([x^4+y^4-2*x*y^5 x^6+x^2+y^4-4 ])
function [x, y] = newton3(x,y)
for N = 1:30
D = [4*x^3-2*y^5 4*y^3-10*x*y^4; 6*x^5+2*x 4*y^3]; %%%%%%%
f = x^4+y^4-2*x*y^5 ;
g = x^6+x^2+y^4-4 ;
z = [x y]' ;
z = z - D\[f g]' ; %%%%%%%%
x = z(1);
y = z(2);
end
end
  5 件のコメント
Alan Stevens
Alan Stevens 2021 年 2 月 13 日
Also, to define the functions you need
f = @(x,y) x^4 ...etc.
and you will need to define functions for dfdx, dfdy etc. if you are not using the Symbolic toolbox.
sanyer
sanyer 2021 年 2 月 13 日
Thank you! I feel so stupid now haha...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by