Sympy解决了联立方程化学平衡

时间:2018-05-10 14:32:26

标签: python sympy

我试图用Sympy的nsolve作为一种数值方法来解决同时化学平衡中的反应进程变量。不幸的是,对于目前的情况,没有任何初始猜测似乎达到了令人满意的解决方案。我相信这个系统确实有一个合适的解决方案,其中所有四个进度变量(未知数)都有0到1之间的实际解。

以下是简化代码:

from sympy import *
var('x1,x2,x3,x4')
P = 1.          # Pressure [bar]
n1 = 2.*x1      # x1 is progress variable for reaction 1
n2 = x2         # x2 is progress variable for reaction 2
n3 = 2.*x3      # x3 is progress variable for reaction 3
n4 = x4         # x4 is progress variable for reaction 4
n5 = 1.-x1-x2-3.*x3-2.*x4
nTot = n1+n2+n3+n4+n5

rxn1 = Eq(P*n1**2/(nTot*n5),3.06e-3)
rxn2 = Eq(n2/n5,8.02e5)
rxn3 = Eq(nTot*n3**2/(P*n5**3),1.07e21)
rxn4 = Eq(nTot*n4/(P*n5**2),8.25e14)

reactions = (rxn1,rxn2,rxn3,rxn4)
unknowns = (x1,x2,x3,x4)
answer = nsolve(reactions,unknowns,(1e-4,.1,.3,.2))
print (answer)

以下是Sympy的输出(我在64位Python 2.7的Spyder3环境中使用Sympy v1.1.1):

Traceback (most recent call last):

  File "<ipython-input-9-43632f4fa60f>", line 18, in <module>
    answer = nsolve(reactions,unknowns,(1e-4,.1,.3,.2))

  File "C:\Users\<user>\AppData\Local\Continuum\anaconda2\lib\site-packages\sympy\utilities\decorator.py", line 91, in func_wrapper
    return func(*args, **kwargs)

  File "C:\Users\<user>\AppData\Local\Continuum\anaconda2\lib\site-packages\sympy\solvers\solvers.py", line 2847, in nsolve
    x = findroot(f, x0, J=J, **kwargs)

  File "C:\Users\<user>\AppData\Local\Continuum\anaconda2\lib\site-packages\mpmath\calculus\optimization.py", line 975, in findroot
    % (norm(f(*xl))**2, tol))

ValueError: Could not find root within given tolerance. (1.1449e+42 > 2.1684e-19)
Try another starting point or tweak arguments.

我试图使用各种解决方法(毫不奇怪,没有可用的分析解决方案)和nsolve方法,因此这些非线性方程系统都没有成功。我也尝试用对数形式重新形成方程式,结果相似。

您建议我尝试使用其他解算器设置吗?

感谢您的帮助! 戴夫

0 个答案:

没有答案