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

 채택된 답변

Alan Stevens
Alan Stevens 2021년 2월 13일
편집: Alan Stevens 2021년 2월 13일

0 개 추천

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

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 2021년 2월 13일
The code doesn't actually call the function newton3!
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개)

카테고리

도움말 센터File Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기

질문:

2021년 2월 13일

댓글:

2021년 2월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by