我正在尝试解决一个包含三个耦合ODE的系统,这些ODE包含随时间变化的几个参数(在Python中)。我已经阅读了其他有关此问题的堆栈交换答案,并尝试仔细地遵循它们(使用ODEint),但是我仍然得到可以在各处跳跃的输出。
只是想知道Python中是否有一个ODE求解器旨在处理随时间变化的参数。我需要解决的最终系统是六个方程式,每个方程式约有15个参数(反应速率...模拟大爆炸核合成)。任何帮助都将是惊人的。谢谢你!
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 3 12:42:24 2019
"""
import scipy.integrate as integ
import numpy as np
import matplotlib.pyplot as plt
#Initate time array
n=200
t0=0.1
tf=100
time_vec = np.linspace(t0,tf,n)
#Initiate Nuclide Abundances
X_n0 = 0.5
X_p0 = 0.5
X_D0 = 0.0
X0_vec = (X_n0, X_p0, X_D0)
#define functions of changing parameters
def a(t):
return np.sqrt(t)
def rho_b(t, a):
return 1 / (a(t) ** 3)
#define functions of changing reaction rates
def brac_pn(t, rho_b):
return 2.5E4 * rho_b(t, a)
#Define function to plug into ODEint
def EvolveDensity(X, t, a, rho_b, brac_pn):
dX_n_dt = (X[0] * X[1]) + (brac_pn(t, rho_b) * X[2] )
dX_p_dt = (X[0] * X[1]) + (brac_pn(t, rho_b) * X[2] )
dX_D_dt = - (X[0] * X[1]) + (brac_pn(t, rho_b) * X[2] )
return [dX_n_dt, dX_p_dt, dX_D_dt]
#Solve Ode through time
Densities = integ.odeint(EvolveDensity, X0_vec, time_vec, args=(a, rho_b, brac_pn))
plt.plot(time_vec,Densities[:,0])
plt.plot(time_vec,Densities[:,1])
plt.plot(time_vec,Densities[:,2])