1

我正在尝试使用 sympy 来求解方程,但我想得到一个直接的数字答案。我的脚本是这样的:

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)

它给了我一长串非常扩展的解决方案。举个例子,

[{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)}]

我当然可以用 evalf 评估它们,但不能一次全部评估。我正在寻找一种干净的方式以数字形式接收方程的解。我现在已经做了一个解决方法。如果有更好的方法,我真的很想知道。我打印答案的功能如下:

def printeqsolve(input):
        for i in input:
                for j in i:
                        print "%r:" %j, i[j].evalf(chop=True)
                print "---"                    

我还想排除非实数解决方案,但是当我将符号限制为实数时,找不到解决方案。

4

2 回答 2

1

您也可以使用nsolve但需要给出“足够好”的猜测并且不能通过 Eq 实例:

>>> 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']])
于 2014-05-07T06:59:15.540 回答
0

在 Python 3.8.3 中,应该这样写;否则会报错:

nsolve([e.lhs - e.rhs for e in (eq1,eq2,eq3)], [A,B,V], [0,0,0])

(eq1,eq2,eq3)
于 2021-02-06T15:41:51.477 回答