使用函数 scipy.integrate.RK45 求解耦合微分方程

1 python python-3.x

x' = f(x,y,t)
y' = g(x,y,t)
Run Code Online (Sandbox Code Playgroud)

初始条件已给出为 x0 和 y0 以及 t0。求 t0 到 a 范围内的解图。

我曾尝试对非耦合方程执行此操作,但似乎也存在问题。我必须使用这个函数来解决这个问题,所以其他函数不是选项。

from numpy import *
from matplotlib import pyplot as plt
def f(t,x):
    return -x
import scipy
from scipy import integrate as inte

solution = inte.RK45(f, 0 , [1] , 10 ,1, 0.001, e**-6)
print (solution)
Run Code Online (Sandbox Code Playgroud)

我希望输出是所有值的数组。

但这<scipy.integrate._ivp.rk.RK45 at 0x1988ba806d8>就是我得到的。

Zar*_*chi 6

您需要通过调用积分器step()方法来收集数据:

from math import e
from scipy import integrate as inte


def f(t,x):
    return -x


solution = inte.RK45(f, 0 , [1] , 10 ,1, 0.001, e**-6)

# collect data
t_values = []
y_values = []
for i in range(100):
    # get solution step state
    solution.step()
    t_values.append(solution.t)
    y_values.append(solution.y[0])
    # break loop after modeling is finished
    if solution.status == 'finished':
        break

data = zip(t_values, y_values)
Run Code Online (Sandbox Code Playgroud)

输出:

(0.12831714796342164, 0.879574381033538)
(1.1283171479634215, 0.3239765636806864)
(2.1283171479634215, 0.11933136762238628)
(3.1283171479634215, 0.043953720407578944)
(4.128317147963422, 0.01618962035012491)
(5.128317147963422, 0.005963176828962677)
(6.128317147963422, 0.002196436798667919)
(7.128317147963422, 0.0008090208875093502)
(8.128317147963422, 0.00029798936023261037)
(9.128317147963422, 0.0001097594143523445)
(10, 4.5927433621121034e-05)
Run Code Online (Sandbox Code Playgroud)