且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SymPy/SciPy:求解具有不同变量的常微分方程组

更新时间:2021-09-06 23:43:15

如果要使用读取文件的同一脚本来解决系统(因此systemOfEquations可作为全局变量使用),并且systemOfEquations中使用的仅em 变量是xy以及可能的t,您可以像这样在同一文件中定义dX_dt:

If you are going to solve the system in the same script that reads the file (so systemOfEquations is available as a global variable), and if the only variables used in systemOfEquations are x, y and possibly t, you could define dX_dt in the same file like this:

def dX_dt(X, t):
    vals = dict(x=X[0], y=X[1], t=t)
    return [eq.evalf(subs=vals) for eq in systemOfEquations]

dX_dt可以在odeint中使用.在下面的ipython会话中,我已经运行了创建systemOfEquations并定义dX_dt的脚本:

dX_dt can be used in odeint. In the following ipython session, I have already run the script that creates systemOfEquations and defines dX_dt:

In [31]: odeint(dX_dt, [1,2], np.linspace(0, 1, 5))
Out[31]: 
array([[ 1.        ,  2.        ],
       [ 1.00947534,  1.90904183],
       [ 1.01905178,  1.82223595],
       [ 1.02872997,  1.73939226],
       [ 1.03851059,  1.66032942]]