Newton's method for two variable functions

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

 Akzeptierte Antwort

Alan Stevens
Alan Stevens am 13 Feb. 2021
Bearbeitet: Alan Stevens am 13 Feb. 2021

0 Stimmen

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 Kommentare

If I could add, it is a really bad idea to hard code functions into your algorithms. So setting the matrix D here inside the loop, inside your function:
D = [4*x^3-2*y^5 4*y^3-10*x*y^4; 6*x^5+2*x 4*y^3];
Instead, learn to pass functions INTO your code. Think of the idea as a target, something you may want to learn for the future.
Yes, I know this is homework. But one day you might find it important. :)
I tried to do the changes you proposed but the function continues to give the same values of x and y it receives as an input.
x0 = 2
y0 = 3
f = x^4+y^4-2*x*y^5 ;
g = x^6+x^2+y^4-4 ;
function [x1, y1] = newton3(f,g,x0,y0)
for N = 1:30
D = [diff(f,x) diff(f,y); diff(g,x) diff(g,y)];
z = [x0 y0]' ;
z = z - D\[f g]' ;
x0 = z(1);
y0 = z(2);
end
x1 = x0
y1 = y0
end
Alan Stevens
Alan Stevens am 13 Feb. 2021
The code doesn't actually call the function newton3!
Alan Stevens
Alan Stevens am 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 am 13 Feb. 2021
Thank you! I feel so stupid now haha...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 13 Feb. 2021

Kommentiert:

am 13 Feb. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by