0

solving nonlinear system

I write this code to solve eight equations. But this error is exhibited. "can't convert expression to float" What is the problem? import sympy as sym from sympy.solvers import solve import numpy as np from math import log from math import sqrt from math import cos T=-22 L=23.39 D0=0.266475941 D100=0.255457305 D200=0.229910814 D300=0.199324382 D450=0.15539571 D600=0.119089308 D900=0.070524324 D1200=0.043925175 sym.init_printing() a,b,c,d,j,f,g,h=sym.symbols('a,b,c,d,j,f,g,h') A=sym.Eq(0.062015383726393*T*L/(c + g*a) + 2.50461005642387*L/(b + g + 0.00150093890629873*d*j + 0.00150093890629873*d*g - (h**0.5)),D0) B=sym.Eq(2.51685653969386*L/(b + j) + 0.0294709874154182*T*L/(c + g) + (0.301991375261666*h + 0.486445651805026*T*L)/(d*g) - 0.0425080517681347,D100) C=sym.Eq((1.63757186584073*T + 3.30124351745675*log(h) + 0.952978463603516*a*log(h) - 0.22574369565679*d)/(b + j + 0.115826919448799*c + g*log(T + b)),D200) D=sym.Eq(1.42041574752534*a/(g + 0.640226710296927*b + 0.193086683763814*d) + (495 + 0.745975457073521*T*a)/(j*g + 0.0207514715201547*d*c + b*sqrt(g)),D300) J=sym.Eq(7.60657578727923/c + 0.0190340589524935*a**2/(g + 0.460411094180136*b) + (0.460411094180136*h + 1.25627439526128*T*a)/(46.5721463433838*j + d*g),D450) F=sym.Eq((0.678699415617512*L*h + 0.104253726575244*T*h)/(21.0226270392147*d + d*j + d*g + h*g + 0.112737474380886*b*c + 0.112737474380886*b*h),D600) G=sym.Eq((L + 0.062444236690004*L*sqrt(h))/(b + j + 2.46016390558638*g + g*d**0.341389457034226 - 0.0123838604808271*T*b),D900) H=sym.Eq(0.0201527324507928*T + 0.00533320842635642*L*sqrt(h))/(g + 0.0654908487489791*j + 0.000128770004806114*b*d + cos(-0.0101921210626875*f),D1200) print(sym.solve([A,B,C,D,J,F,G,H],(a,b,c,d,j,f,g,h)))

20th May 2022, 6:03 AM
Fatemeh Hoseini
3 Answers
+ 1
HI Fatemeh Hoseini! Avoid put floats directly inside the equations. Put them in variables or a tuple. Then your cide vill be more readable. And use more parentheses were there can be ambiguities. Just as an example: . . . constA = ( 0.062015383726393, 2.50461005642387, 0.00150093890629873, 0.00150093890629873 ) A = sym.Eq((constA[0] * T * L)/(c + g*a) + (constA[1] * L)/(b + g + constA[2]*d*j + constA[3]*d*g - (h**0.5)), D0) . . .
20th May 2022, 6:21 AM
Per Bratthammar
Per Bratthammar - avatar
+ 1
It is always helpful for others if you save your code in the playground, and link to it in your question. That way people can immediately test it. You need to fix your imports, you cannot use functions of the math library on symbolic expressions, use sympy instead from sympy import log from sympy import sqrt from sympy import cos In your last equation verify to use parentheses in the correct places. You need to pass one expression and one value to sympy.Eq() The code runs without errors after these changes, but timeout on Sololearn
20th May 2022, 6:49 AM
Tibor Santa
Tibor Santa - avatar
+ 1
You don't need to create a new thread. Just post in your own thread if you want to add something. https://www.sololearn.com/Discuss/3034980/?ref=app
20th May 2022, 9:55 AM
Lisa
Lisa - avatar