scipy.integrate.odeint()使用说明

    科技2022-07-17  119

    import numpy as np import scipy.integrate as spi import matplotlib.pyplot as plt #模型参数 daysTotal = 365 E0 = 1 dataOffset = 'auto' days0 = 70 r0 = 2.4 r1 = 0.85 sigma = 0.312 gamma = 0.357 beta0 = 0.857 beta1 = 0.304 population = 1000 #定义模型 def model(Y,t,N,beta0,days0,beta1,gamma,sigma): S,E,I,R = Y beta = beta0 if t < days0 else beta1 dS = - beta * S * I / N dE = beta * S * I / N - sigma * E dI = sigma * E - gamma * I dR = gamma * I return dS,dE,dI,dR T = np.arange(daysTotal) N0 = population - E0,E0,0,0 res = spi.odeint(model,N0,T,args=(population,beta0,days0,beta1,gamma,sigma)) #该函数用来求微分方程,第一个变量时微分方程模型,第二个变量是y初值,第三个变量是自变量x(此处是时间t),之后args = (微分方程模型中剩余的变量,顺序对应) plt.figure(figsize = (10,10),dpi = 100) plt.plot(T,res[:,0],label = 'S') plt.plot(T,res[:,1],label = 'E') plt.plot(T,res[:,2],label = 'I') plt.plot(T,res[:,3],label = 'R') plt.show()

    结果显示:

    Processed: 0.008, SQL: 8