描述化学方程的Scipy ODE错误

2024-04-27 18:48:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一大组代表生物系统中化学通量的ODE。分子被反应、隔离和循环。我试图让这个功能发挥作用,让我知道在一系列的条件下,有多少特定的产品可以被生产出来。在

我在用这些包裹

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
import pylab as p 

这是我的职责

^{pr2}$

接下来我的代码如下

# Timespan (0 - hours - increment)
tspan=np.arange(0,50,0.1)

#Starting Concentrations 
#y0 = H2o co2 chcooh ch4 o2 ca hco-3 caco3
y0=[100,40,10,10,10,80,70,10]

Conc=odeint(rxn,y0,tspan,full_output = 1,mxstep=5000)

CW = Conc[:,0]
CC = Conc[:,1]
CA = Conc[:,2]
CM = Conc[:,3]
CO = Conc[:,4]
CCa= Conc[:,5]
CCo= Conc[:,6]
CCc= Conc[:,7]

plt.plot(tspan,CC,label='co2')
plt.plot(tspan,CA,label='ch3cooh')
plt.plot(tspan,CM,label='ch4')
plt.plot(tspan,CO,label='o2')
plt.plot(tspan,CCa,label='Ca')
plt.plot(tspan,CCo,label='hco3-')
plt.plot(tspan,CCc,label='caco3')

plt.xlabel("time (hours)")
plt.ylabel("moles")
plt.title("Nutrient Flux?")
plt.legend()

p.show()    

当运行此程序时,我会遇到大量与收敛和类型有关的错误。即:

 lsoda--  at t (=r1) and step size h (=r2), the    
       corrector convergence failed repeatedly       
       or with abs(h) = hmin   ls
      in above,  r1 =  0.3550854455646D+01   r2 =  0.2492601566412D-09

以及

      File "Reaction.py", line 62, in <module>
    CW = Conc[:,0]
TypeError: tuple indices must be integers or slices, not tuple

我已经在其他堆栈交换答案中读过这些错误的含义,但是,我真的不明白这么简单的一组ode怎么会这么僵硬,我也不知道从哪里得到的typeerror。我对python非常陌生(可能现在很明显),我觉得这是由于某种编码错误造成的。如果能帮上忙我会很感激的。在


Tags: importplotas错误nppltlabelco2
2条回答

我只是缩短了寻找解的时间间隔,发现对于tspan=np.arange(0,5,0.1)来说,解随时间增长很快,达到{}。对于tspan=np.arange(0,10,0.1),它到达~1e38。所以你的解可能是指数趋于无穷大,问题可能出在你的参数,初始条件或方程中。在

为了避免您问题中的第二个错误,我使用了Conc=odeint(rxn,y0,tspan,full_output = 1,mxstep=10000)[0] 因为odeint返回元组(y, infodict),而我们只需要y。在

tspan=np.arange(0,10,0.1)的解决方案: enter image description here

由于您需要完整的输出,odeint也将返回一个infodict。在

Conc, info_dict = odeint(rxn, y0, tspan, full_output=1, mxstep=5000)

顺便说一句,也许你想看看chemreac,它是为你这类问题而构建的。在

相关问题 更多 >