Newton's method returns NaN when solving

3 visualizzazioni (ultimi 30 giorni)
Editor
Editor il 21 Dic 2022
Commentato: Editor il 22 Dic 2022
I am trying to solve a 7D system of equations using the Newton's method. However, my code returns NaN after running. I have checked through my equations and I do not have the case of 0/0. I have tried to change initial guesses but to no success. I cannot figure out why I get such values. Can someone please give me a third eye or suggest a better method that I can use? Thanks in anticipation. Below is my code:
clc; close all; clear
syms x1 x2 x3 x4 x5 x6 x7 x8
f1(x1, x2, x3, x4, x5, x6, x7) = (1.*x2.*(x3+x6) + x1.*x7.*x4)./(2.*x1.*x7 + 2.*x2) + x5./2;
f2(x1, x2, x3, x4, x5, x6, x7) = (2.*x2.*(x3+x6) + x1.*x7.*x4)./(2.*x1.*x7 + 4.*x2) + x5./2;
f3(x1, x2, x3, x4, x5, x6, x7) = (3.*x2.*(x3+x6) + x1.*x7.*x4)./(2.*x1.*x7 + 6.*x2) + x5./2;
f4(x1, x2, x3, x4, x5, x6, x7) = (4.*x2.*(x3+x6) + x1.*x7.*x4)./(2.*x1.*x7 + 8.*x2) + x5./2;
f5(x1, x2, x3, x4, x5, x6, x7) = sqrt( (1.*x2.*(x3+x6).*x5)./(x1.*x7 + 1.*x2) );
f6(x1, x2, x3, x4, x5, x6, x7) = sqrt( (2.*x2.*(x3+x6).*x5)./(x1.*x7 + 2.*x2) );
f7(x1, x2, x3, x4, x5, x6, x7) = sqrt( (3.*x2.*(x3+x6).*x5)./(x1.*x7 + 3.*x2) );
x=[8.0; 8.0; 4.0; 4.0; 3.0; 2.5; 1.6];
e=10^(-8);
n=20;
f1x1(x1,x2,x3,x4,x5,x6,x7) = diff(f1,x1);
f1x2(x1,x2,x3,x4,x5,x6,x7) = diff(f1,x2);
f1x3(x1,x2,x3,x4,x5,x6,x7) = diff(f1,x3);
f1x4(x1,x2,x3,x4,x5,x6,x7) = diff(f1,x4);
f1x5(x1,x2,x3,x4,x5,x6,x7) = diff(f1,x5);
f1x6(x1,x2,x3,x4,x5,x6,x7) = diff(f1,x6);
f1x7(x1,x2,x3,x4,x5,x6,x7) = diff(f1,x7);
f2x1(x1,x2,x3,x4,x5,x6,x7) = diff(f2,x1);
f2x2(x1,x2,x3,x4,x5,x6,x7) = diff(f2,x2);
f2x3(x1,x2,x3,x4,x5,x6,x7) = diff(f2,x3);
f2x4(x1,x2,x3,x4,x5,x6,x7) = diff(f2,x4);
f2x5(x1,x2,x3,x4,x5,x6,x7) = diff(f2,x5);
f2x6(x1,x2,x3,x4,x5,x6,x7) = diff(f2,x6);
f2x7(x1,x2,x3,x4,x5,x6,x7) = diff(f2,x7);
f3x1(x1,x2,x3,x4,x5,x6,x7) = diff(f3,x1);
f3x2(x1,x2,x3,x4,x5,x6,x7) = diff(f3,x2);
f3x3(x1,x2,x3,x4,x5,x6,x7) = diff(f3,x3);
f3x4(x1,x2,x3,x4,x5,x6,x7) = diff(f3,x4);
f3x5(x1,x2,x3,x4,x5,x6,x7) = diff(f3,x5);
f3x6(x1,x2,x3,x4,x5,x6,x7) = diff(f3,x6);
f3x7(x1,x2,x3,x4,x5,x6,x7) = diff(f3,x7);
f4x1(x1,x2,x3,x4,x5,x6,x7) = diff(f4,x1);
f4x2(x1,x2,x3,x4,x5,x6,x7) = diff(f4,x2);
f4x3(x1,x2,x3,x4,x5,x6,x7) = diff(f4,x3);
f4x4(x1,x2,x3,x4,x5,x6,x7) = diff(f4,x4);
f4x5(x1,x2,x3,x4,x5,x6,x7) = diff(f4,x5);
f4x6(x1,x2,x3,x4,x5,x6,x7) = diff(f4,x6);
f4x7(x1,x2,x3,x4,x5,x6,x7) = diff(f4,x7);
f5x1(x1,x2,x3,x4,x5,x6,x7) = diff(f5,x1);
f5x2(x1,x2,x3,x4,x5,x6,x7) = diff(f5,x2);
f5x3(x1,x2,x3,x4,x5,x6,x7) = diff(f5,x3);
f5x4(x1,x2,x3,x4,x5,x6,x7) = diff(f5,x4);
f5x5(x1,x2,x3,x4,x5,x6,x7) = diff(f5,x5);
f5x6(x1,x2,x3,x4,x5,x6,x7) = diff(f5,x6);
f5x7(x1,x2,x3,x4,x5,x6,x7) = diff(f5,x7);
f6x1(x1,x2,x3,x4,x5,x6,x7) = diff(f6,x1);
f6x2(x1,x2,x3,x4,x5,x6,x7) = diff(f6,x2);
f6x3(x1,x2,x3,x4,x5,x6,x7) = diff(f6,x3);
f6x4(x1,x2,x3,x4,x5,x6,x7) = diff(f6,x4);
f6x5(x1,x2,x3,x4,x5,x6,x7) = diff(f6,x5);
f6x6(x1,x2,x3,x4,x5,x6,x7) = diff(f6,x6);
f6x7(x1,x2,x3,x4,x5,x6,x7) = diff(f6,x7);
f7x1(x1,x2,x3,x4,x5,x6,x7) = diff(f7,x1);
f7x2(x1,x2,x3,x4,x5,x6,x7) = diff(f7,x2);
f7x3(x1,x2,x3,x4,x5,x6,x7) = diff(f7,x3);
f7x4(x1,x2,x3,x4,x5,x6,x7) = diff(f7,x4);
f7x5(x1,x2,x3,x4,x5,x6,x7) = diff(f7,x5);
f7x6(x1,x2,x3,x4,x5,x6,x7) = diff(f7,x6);
f7x7(x1,x2,x3,x4,x5,x6,x7) = diff(f7,x7);
f11=matlabFunction(f1);
f22=matlabFunction(f2);
f33=matlabFunction(f3);
f44=matlabFunction(f4);
f55=matlabFunction(f5);
f66=matlabFunction(f6);
f77=matlabFunction(f7);
f1x11=matlabFunction(f1x1);
f1x22=matlabFunction(f1x2);
f1x33=matlabFunction(f1x3);
f1x44=matlabFunction(f1x4);
f1x55=matlabFunction(f1x5);
f1x66=matlabFunction(f1x6);
f1x77=matlabFunction(f1x7);
f2x11=matlabFunction(f2x1);
f2x22=matlabFunction(f2x2);
f2x33=matlabFunction(f2x3);
f2x44=matlabFunction(f2x4);
f2x55=matlabFunction(f2x5);
f2x66=matlabFunction(f2x6);
f2x77=matlabFunction(f2x7);
f3x11=matlabFunction(f3x1);
f3x22=matlabFunction(f3x2);
f3x33=matlabFunction(f3x3);
f3x44=matlabFunction(f3x4);
f3x55=matlabFunction(f3x5);
f3x66=matlabFunction(f3x6);
f3x77=matlabFunction(f3x7);
f4x11=matlabFunction(f4x1);
f4x22=matlabFunction(f4x2);
f4x33=matlabFunction(f4x3);
f4x44=matlabFunction(f4x4);
f4x55=matlabFunction(f4x5);
f4x66=matlabFunction(f4x6);
f4x77=matlabFunction(f4x7);
f5x11=matlabFunction(f5x1);
f5x22=matlabFunction(f5x2);
f5x33=matlabFunction(f5x3);
f5x44=matlabFunction(f5x4);
f5x55=matlabFunction(f5x5);
f5x66=matlabFunction(f5x6);
f5x77=matlabFunction(f5x7);
f6x11=matlabFunction(f6x1);
f6x22=matlabFunction(f6x2);
f6x33=matlabFunction(f6x3);
f6x44=matlabFunction(f6x4);
f6x55=matlabFunction(f6x5);
f6x66=matlabFunction(f6x6);
f6x77=matlabFunction(f6x7);
f7x11=matlabFunction(f7x1);
f7x22=matlabFunction(f7x2);
f7x33=matlabFunction(f7x3);
f7x44=matlabFunction(f7x4);
f7x55=matlabFunction(f7x5);
f7x66=matlabFunction(f7x6);
f7x77=matlabFunction(f7x7);
for i=1:n
F=[f11(x(1),x(2),x(3),x(4),x(5),x(6),x(7));f22(x(1),x(2),x(3),x(4),x(5),x(6),x(7));...
f33(x(1),x(2),x(3),x(4),x(5),x(6),x(7)); f44(x(1),x(2),x(3),x(4),x(5),x(6),x(7));...
f55(x(1),x(2),x(3),x(4),x(5),x(6),x(7));f66(x(1),x(2),x(3),x(4),x(5),x(6),x(7));...
f77(x(1),x(2),x(3),x(4),x(5),x(6),x(7))];
J=[f1x11(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f1x22(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f1x33(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f1x44(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f1x55(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f1x66(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f1x77(x(1),x(2),x(3),x(4),x(5),x(6),x(7));
f2x11(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f2x22(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f2x33(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f2x44(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f2x55(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f2x66(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f2x77(x(1),x(2),x(3),x(4),x(5),x(6),x(7));
f3x11(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f3x22(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f3x33(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f3x44(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f3x55(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f3x66(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f3x77(x(1),x(2),x(3),x(4),x(5),x(6),x(7));
f4x11(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f4x22(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f4x33(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f4x44(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f4x55(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f4x66(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f4x77(x(1),x(2),x(3),x(4),x(5),x(6),x(7));
f5x11(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f5x22(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f5x33(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f5x44(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f5x55(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f5x66(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f5x77(x(1),x(2),x(3),x(4),x(5),x(6),x(7));
f6x11(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f6x22(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f6x33(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f6x44(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f6x55(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f6x66(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f6x77(x(1),x(2),x(3),x(4),x(5),x(6),x(7));
f7x11(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f7x22(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f7x33(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f7x44(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f7x55(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),f7x66(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),...
f7x77(x(1),x(2),x(3),x(4),x(5),x(6),x(7))];
y=-J\F;
x=x+y
end

Risposta accettata

Torsten
Torsten il 21 Dic 2022
Modificato: Torsten il 22 Dic 2022
If you rename y(1)= x(3)+x(6) and y(2)=x(1)*x(7) in your equations for f1,...,f7, you will see that you have 7 equations for 5 unknowns (x(2),x(4),x(5),y(1),y(2)). This doesn't sound good for solvability of the system, especially for the regularity of the Jacobian.
So remove two of the equations f1,...,f7 and replace x(3)+x(6) by y(1) and x(1)*x(7) by y(2).
One possible solution for your equations is
x(2) = 0, y(1) = const1, y(2) = const2 (~=0), x(4) = const3, x(5) = -const3
where const1, const2 ~=0 and const3 are arbitrary constants.
  1 Commento
Editor
Editor il 22 Dic 2022
@Torsten I have implemented your suggestion and it works accordingly. Thank you very much

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Numerical Integration and Differential Equations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by