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!