Newton's method for two variable functions

8 views (last 30 days)
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

Accepted Answer

Alan Stevens
Alan Stevens on 13 Feb 2021
Edited: Alan Stevens on 13 Feb 2021
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 Comments
Alan Stevens
Alan Stevens on 13 Feb 2021
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 on 13 Feb 2021
Thank you! I feel so stupid now haha...

Sign in to comment.

More Answers (0)

Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!