1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import numpy as np import pandas as pd from scipy.integrate import odeint from matplotlib import pyplot as plt
# 微分方程 def diff(y, t, s2ip, i2rp): s, i, r = y s2i = i * s * s2ip i2r = i * i2rp ds = -s2i di = s2i - i2r dr = i2r return [ds, di, dr]
# 自变量范围 t = np.arange(0, 90) # 求解 y = odeint( # 微分方程 func=diff, # y初值 y0=(1, 0.01, 0), # 自变量 t=t, # 其他参数(此处为s2ip,i2rp) args=(0.4, 0.2) )
# 返回数据为积分后的数值序列 data = pd.DataFrame(y) data.columns = ['S', 'I', 'R'] data.plot() plt.show()
|