I'm trying to use sympy to solve equations, but I would like to get a straight numerical answer. My script is like this:
from sympy import *
A,B,V=symbols('A,B,V')
eq1=Eq(630.26*(V-39.0)*V*(V+39)-A+B,0)
eq2=Eq(B,1.36*10**8*(V-39))
eq3=Eq(A,5.75*10**5*V*(V+39.0))
solve([eq1,eq2,eq3], [A,B,V], dict=True)
It gives me a long list of solutions that are in very expanded form. As an example,
[{V: 304.107299632956 - (-5162698.06009073 + 3004043.12120894*I)**(1/3)*(-0.5 + 0.866025403784439*I) - 32920.4469842867/((-5162698.06009073 + 3004043.12120894*I)**(1/3)*(-0.5 + 0.866025403784439*I)), B: 36054592750.082 - 1245.8292864816*I*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3) + 8.46536389385714e+17/((-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)*(1.0 - 1.73205080756888*I)) + 719.279873914469*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3), A: 97854838797.9765 - 3957.60119254414*I*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3) - 3.13901978017549e-5*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3) - 0.000285202926135405*I*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3) + 2925.78725273524*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)}, {V: 304.107299632956 - (-5162698.06009073 + 3004043.12120894*I)**(1/3) - 32920.4469842867/(-5162698.06009073 + 3004043.12120894*I)**(1/3), B: -1.05776452046245e-5*(4.0015351858068e+22 - 136000000.0*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)*(25062979.0 - (-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)))/(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3), A: 97854838797.9765 - 3936.45368131564*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3) + 5.56956529342379e+24/(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3) + 6.43347823930771e-5*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3) - 1.15822484655024e+18/(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)}, {V: 304.107299632956 - 32920.4469842867/((-5162698.06009073 + 3004043.12120894*I)**(1/3)*(-0.5 - 0.866025403784439*I)) - (-5162698.06009073 + 3004043.12120894*I)**(1/3)*(-0.5 - 0.866025403784439*I), B: 36054592750.082 + 8.46536389385714e+17/((-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)*(1.0 + 1.73205080756888*I)) + 719.279873914469*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3) + 1245.8292864816*I*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3), A: 97854838797.9765 + 2.31644969310047e+18/((-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)*(1.0 + 1.73205080756888*I)) - 3.21673911965385e-5*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3) + 5.57155558993486e-5*I*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3) - 1.11391305868476e+25/((-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3)*(1.0 - 1.73205080756888*I)) + 1968.22684065782*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3) + 3409.06888884012*I*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)}]
I can of course evaluate them with evalf but not all at once. I'm looking for a clean way receive the solutions of the equation in numerical form. I've made a workaround function for now. If there's a better way, I'd really like to know. My function to print answers is as follows:
def printeqsolve(input):
for i in input:
for j in i:
print "%r:" %j, i[j].evalf(chop=True)
print "---"
I'd also like to exclude non-real solutions, but when I restrict my symbols to Real no solutions are found.
You could also use nsolve
but need to give a "good enough" guess and cannot pass Eq instances:
>>> nsolve([e.lhs - e.rhs for e in eq1,eq2,eq3], [A,B,V], [0,0,0])
matrix(
[['4442890172.68209'],
['4289299466.1432'],
['70.5389666628177']])
>>> nsolve([e.lhs - e.rhs for e in eq1,eq2,eq3], [A,B,V], [1e5,1e4,1e3])
matrix(
[['266367838273.086'],
['84646784928.5322'],
['661.402830356854']])
In Python 3.8.3, it should be written as follows; otherwise, an error will be reported:
nsolve([e.lhs - e.rhs for e in (eq1,eq2,eq3)], [A,B,V], [0,0,0])
(eq1,eq2,eq3)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.