简体   繁体   English

在 python 中用 ODEINT 求解微分方程

[英]Solving differential equation with ODEINT in python

I have a coupled system of differential equations that I've already solved with Euler in Excel.我有一个耦合的微分方程系统,我已经在 Excel 中用欧拉解决了这个问题。 Now I want to make it more precise with an ODE-solver in python.现在我想使用 python 中的 ODE 求解器使其更精确。 However, there must be a mistake in my code because the curves look different than in Excel.但是,我的代码中一定有一个错误,因为曲线看起来与 Excel 中的不同。 I don't expect the curves to reach 1 and 0 in the end.我不希望曲线最终达到 1 和 0。

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

# define reactor
def reactor(x,z):
    n_a = x[0]
    n_b = x[1]
    n_c = x[2]

    dn_adz = A * (-1) * B * (n_a/(n_a + n_b + n_c)) / (1 + C * (n_c/(n_a + n_b + n_c)))
    dn_bdz = A * (1) * B * (n_a/(n_a + n_b + n_c)) / (1 + C * (n_c/(n_a + n_b + n_c)))
    dn_cdz = A * (1) * B * (n_a/(n_a + n_b + n_c)) / (1 + C * (n_c/(n_a + n_b + n_c)))
    dxdz = [dn_adz,dn_bdz,dn_cdz]
    return dxdz

# initial conditions
n_a0 = 0.5775
n_b0 = 0.0
n_c0 = 0.0
x0 = [n_a0, n_b0, n_c0]

# parameters
A = 0.12
B = 3.1e-9
C = 4.02e15

# number of steps
n = 100

# z step interval (m)
z = np.linspace(0,0.0274,n)

# solve ODEs
x = odeint(reactor,x0,z)


# Plot the results
plt.plot(z,x[:,0],'b-')
plt.plot(z,x[:,1],'r--')
plt.plot(z,x[:,2],'k:')
plt.show()

Is is a problem with the initial condition that stays constant and does not change from step to step?初始条件保持不变且不随步骤而变化是否存在问题? Should it be like in Excel with Euler, where the next step uses the conditions/values of the precious step?是否应该像在带有欧拉的 Excel 中一样,下一步使用珍贵步骤的条件/值?

From the structure of the right sides you get constant combinations of the state variables, n_a+n_b=n_a0+n_b0 and n_a+n_c=n_a0+n_c0 .从右侧的结构中,您可以获得 state 变量n_a+n_b=n_a0+n_b0n_a+n_c=n_a0+n_c0的常量组合。 This means that the dynamic reduces to the one-dimensional dynamic of n_a .这意味着动态减少到n_a的一维动态。

By the first equation, the derivative of n_a is negative for positive n_a , so that the solution is falling towards n_a=0 .通过第一个方程, n_a n_a负,因此解正朝着n_a=0下降。 By the constants of the dynamics, n_b converges to n_a0+n_b0 and n_c converges to n_a0+n_c0 .通过动力学常数, n_b收敛到n_a0+n_b0并且n_c收敛到n_a0+n_c0

It is unclear how you get convergence towards 1 in some components, as that is not supported by the initial conditions.目前尚不清楚如何在某些组件中收敛到1 ,因为初始条件不支持这一点。 Apart from that, the described odeint result fits this qualitative behavior.除此之外,所描述的odeint结果符合这种定性行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM