Newton's method for two variable functions
8 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Systems of Nonlinear Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!