微分方程分類
微分方程解析解
import numpy as np
from scipy import integrate
import sympy
def apply_ics(sol, ics, x, known_params):
free_params = sol.free_symbols - set(known_params)
eqs = [(sol.lhs.diff(x, n) - sol.rhs.diff(x, n)).subs(x, 0).subs(ics)
for n in range(len(ics))]
sol_params = sympy.solve(eqs, free_params)
return sol.subs(sol_params)
sympy.init_printing() #初始化打印環(huán)境
t, omega0, gamma = sympy.symbols("t, omega_0, gamma", positive = True) #標(biāo)記參數(shù)掺冠,且均為正
x = sympy.Function('x') #標(biāo)記x是微分函數(shù),非變量
ode = x(t).diff(t, 2) + 2*gamma*omega0*x(t).diff(t) + omega0**2*x(t)
ode_sol = sympy.dsolve(ode) #用diff()和dsolve得到通解
ics = {x(0): 1, x(t).diff(t).subs(t, 0): 0} #將初始條件字典匹配
x_t_sol = apply_ics(ode_sol, ics, t, [omega0, gamma])
print(sympy.latex(x_t_sol)) #以latex公式形式輸出
微分方程數(shù)值解
場(chǎng)線圖與數(shù)值解
import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt
import sympy
def plot_direction_field(x, y_x, f_xy, x_lim = (-5,5), y_lim = (-5,5), ax = None):
f_np = sympy.lambdify((x, y_x), f_xy, 'numpy')
x_vec = np.linspace(x_lim[0], x_lim[1], 20)
y_vec = np.linspace(y_lim[0], y_lim[1], 20)
if ax is None:
_, ax = plt.subplot(figsize = (4,4))
dx = x_vec[1] - x_vec[0]
dy = y_vec[1] - y_vec[0]
for m, xx in enumerate(x_vec):
for n, yy in enumerate(y_vec):
Dy = f_np(xx,yy) * dx
Dx = 0.8*dx**2/np.sqrt(dx**2 + Dy**2)
Dy = 0.8*Dy*dy/np.sqrt(dx**2 + Dy**2)
ax.plot([xx-Dx/2, xx+Dx/2], [yy-Dy/2, yy+Dy/2], 'b', lw = 0.5)
ax.axis('tight')
ax.set_title(r"$%s$"%(sympy.latex(sympy.Eq(y_x.diff(x), f_xy))), fontsize = 18)
return ax
x = sympy.symbols('x')
y = sympy.Function('y')
f = x - y(x)**2
f_np = sympy.lambdify((y(x), x), f) #符號(hào)表達(dá)式轉(zhuǎn)隱函數(shù)
y0 = 1
xp = np.linspace(0, 5, 100)
yp = integrate.odeint(f_np, y0, xp) #初始y0解f_np, x范圍xp
xn = np.linspace(0, -5, 100)
yn = integrate.odeint(f_np, y0, xn)
fig, ax = plt.subplots(1, 1, figsize = (4,4))
plot_direction_field(x, y(x), f, ax = ax) #繪制f的場(chǎng)線圖
ax.plot(xn, yn, 'b', lw = 2)
ax.plot(xp, yp, 'r', lw = 2)
plt.show()
洛倫茲曲線與數(shù)值解
import numpy as np
from scipy.integrate import odeint
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def dmove(Point, t, sets):
p, r, b = sets
x, y, z = Point
return np.array([p*(y-x), x*(r-z), x*y-b*z])
t = np.arange(0, 30, 0.001)
P1 = odeint(dmove, (0., 1., 0.), t, args=([10., 28., 3.],))
P2 = odeint(dmove, (0., 1.01, 0.), t, args=([10., 28., 3.],))
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(P1[:,0], P1[:,1], P1[:,2])
ax.plot(P2[:,0], P2[:,1], P2[:,2])
plt.show
洛倫茲曲線很好地展示了非線性的直觀形態(tài)泳姐,同時(shí)也是展示混沌系統(tǒng)的經(jīng)典例子形娇。這個(gè)例子告訴我們息堂,混沌系統(tǒng)可以是一個(gè)確定系統(tǒng),不一定是隨機(jī)過(guò)程,同時(shí)它有初值敏感的特征
傳染病模型
SI-Model(只感染不恢復(fù))
import numpy as np
import scipy.integrate as spi
import matplotlib.pyplot as plt
N = 10000 # N為人群總數(shù)
beta = 0.25 # β為傳染率系數(shù)
gamma = 0 # gamma為恢復(fù)率系數(shù)
I_0 = 1 # I_0為感染者的初始人數(shù)
S_0 = N- I_0 # S_0為易感染者的初始人數(shù)
T = 150 # T為傳播時(shí)間
INI = (S_0, I_0) # INI為初始狀態(tài)下的數(shù)組
def funcSI(inivalue,_):
Y = np.zeros(2)
X = inivalue
Y[0] = -(beta*X[0]*X[1])/N+gamma*X[1]#易感個(gè)體變化
Y[1] = (beta*X[0]*X[1])/N-gamma*X[1]#感染個(gè)體變化
return Y
T_range = np.arange(0, T+1)
RES = spi.odeint(funcSI, INI, T_range)
plt.plot(RES[:,0],color = 'darkblue',label = 'Susceptible',marker = '.')
plt.plot(RES[:,1],color = 'red',label = 'Infection',marker = '.')
plt.title('SI Model')
plt.legend()
plt.xlabel('Day')
plt.ylabel('Number')
plt.show()
SIS-Model(會(huì)恢復(fù))
import numpy as np
import scipy.integrate as spi
import matplotlib.pyplot as plt
N = 10000 # N為人群總數(shù)
beta = 0.25 # β為傳染率系數(shù)
gamma = 0.05 # gamma為恢復(fù)率系數(shù)
I_0 = 1 # I_0為感染者的初始人數(shù)
S_0 = N- I_0 # S_0為易感染者的初始人數(shù)
T = 150 # T為傳播時(shí)間
INI = (S_0, I_0) # INI為初始狀態(tài)下的數(shù)組
def funcSIS(inivalue,_):
Y = np.zeros(2)
X = inivalue
Y[0] = -(beta*X[0])/N*X[1]+gamma*X[1]#易感個(gè)體變化
Y[1] = (beta*X[0]*X[1])/N-gamma*X[1]#感染個(gè)體變化
return Y
T_range = np.arange(0, T+1)
RES = spi.odeint(funcSIS, INI, T_range)
plt.plot(RES[:,0],color = 'darkblue',label = 'Susceptible',marker = '.')
plt.plot(RES[:,1],color = 'red',label = 'Infection',marker = '.')
plt.title('SIS Model')
plt.legend()
plt.xlabel('Day')
plt.ylabel('Number')
plt.show()
SIR-Model(研發(fā)出疫苗读存,可以治療)
import numpy as np
import scipy.integrate as spi
import matplotlib.pyplot as plt
N = 10000 # N為人群總數(shù)
beta = 0.25 # β為傳染率系數(shù)
gamma = 0.05 # gamma為恢復(fù)率系數(shù)
I_0 = 1 # I_0為感染者的初始人數(shù)
R_0 = 0 # R_0為治愈者的初始人數(shù)
S_0 = N - I_0 - R_0 # S_0為易感染者的初始人數(shù)
T = 150 # T為傳播時(shí)間
INI = (S_0, I_0, R_0) # INI為初始狀態(tài)下的數(shù)組
def funcSIR(inivalue,_):
Y = np.zeros(3)
X = inivalue
Y[0] = -(beta*X[0] *X[1])/N #易感個(gè)體變化
Y[1] = (beta*X[0]*X[1])/N-gamma*X[1]#感染個(gè)體變化
Y[2] = gamma*X[1] #治愈個(gè)體變化
return Y
T_range = np.arange(0, T+1)
RES = spi.odeint(funcSIR, INI, T_range)
plt.plot(RES[:,0],color = 'darkblue',label = 'Susceptible',marker = '.')
plt.plot(RES[:,1],color = 'red',label = 'Infection',marker = '.')
plt.plot(RES[:,2],color = 'green',label = 'Recovery',marker = '.')
plt.title('SIR Model')
plt.legend()
plt.xlabel('Day')
plt.ylabel('Number')
plt.show()
SIRS-Model(抗體有時(shí)效)
import numpy as np
import scipy.integrate as spi
import matplotlib.pyplot as plt
N = 10000 # N為人群總數(shù)
beta = 0.25 # β為傳染率系數(shù)
gamma = 0.05 # gamma為恢復(fù)率系數(shù)
Ts = 7 # Ts為抗體持續(xù)時(shí)間
I_0 = 1 # I_0為感染者的初始人數(shù)
R_0 = 0 # R_0為治愈者的初始人數(shù)
S_0 = N - I_0 - R_0 # S_0為易感染者的初始人數(shù)
T = 150 # T為傳播時(shí)間
INI = (S_0, I_0, R_0) # INI為初始狀態(tài)下的數(shù)組
def funcSIRS(inivalue,_):
Y = np.zeros(3)
X = inivalue
Y[0] = -(beta*X[0]*X[1])/N+X[2]/Ts #易感個(gè)體變化
Y[1] = (beta*X[0]*X[1])/N-gamma*X[1]#感染個(gè)體變化
Y[2] = gamma*X[1]-X[2]/Ts #治愈個(gè)體變化
return Y
T_range = np.arange(0, T+1)
RES = spi.odeint(funcSIRS, INI, T_range)
plt.plot(RES[:,0],color = 'darkblue',label = 'Susceptible',marker = '.')
plt.plot(RES[:,1],color = 'red',label = 'Infection',marker = '.')
plt.plot(RES[:,2],color = 'green',label = 'Recovery',marker = '.')
plt.title('SIRS Model')
plt.legend()
plt.xlabel('Day')
plt.ylabel('Number')
plt.show()
SIER-Model(有潛伏期)
import numpy as np
import scipy.integrate as spi
import matplotlib.pyplot as plt
N = 10000 # N為人群總數(shù)
beta = 0.6 # β為傳染率系數(shù)
gamma = 0.1 # gamma為恢復(fù)率系數(shù)
Te = 14 # Te為疾病潛伏期
I_0 = 1 # I_0為感染者的初始人數(shù)
E_0 = 0 # E_0為潛伏者的初始人數(shù)
R_0 = 0 # R_0為治愈者的初始人數(shù)
S_0 = N - I_0 - R_0 - E_0 # S_0為易感染者的初始人數(shù)
T = 150 # T為傳播時(shí)間
INI = (S_0, E_0, I_0, R_0) # INI為初始狀態(tài)下的數(shù)組
def funcSIER(inivalue,_):
Y = np.zeros(4)
X = inivalue
Y[0] = -(beta*X[0]*X[2])/N #易感個(gè)體變化
Y[1] = (beta*X[0]*X[2]/N-X[1]/Te) # 潛伏個(gè)體變化
Y[2] = X[1]/Te-gamma*X[2]#感染個(gè)體變化
Y[3] = gamma*X[2] #治愈個(gè)體變化
return Y
T_range = np.arange(0, T+1)
RES = spi.odeint(funcSEIR, INI, T_range)
plt.plot(RES[:,0],color = 'darkblue',label = 'Susceptible',marker =
'.')
plt.plot(RES[:,1],color = 'orange',label = 'Exposed',marker = '.')
plt.plot(RES[:,2],color = 'red',label = 'Infection',marker = '.')
plt.plot(RES[:,3],color = 'green',label = 'Recovery',marker = '.')
plt.title('SIER Model')
plt.legend()
plt.xlabel('Day')
plt.ylabel('Number')
plt.show()
SIERS-Model(考慮抗體時(shí)效)
import numpy as np
import scipy.integrate as spi
import matplotlib.pyplot as plt
N = 10000 # N為人群總數(shù)
beta = 0.6 # β為傳染率系數(shù)
gamma = 0.1 # gamma為恢復(fù)率系數(shù)
Ts = 7 # Ts為抗體持續(xù)時(shí)間
Te = 14 # Te為疾病潛伏期
I_0 = 1 # I_0為感染者的初始人數(shù)
E_0 = 0 # E_0為潛伏者的初始人數(shù)
R_0 = 0 # R_0為治愈者的初始人數(shù)
S_0 = N - I_0 - R_0 - E_0 # S_0為易感染者的初始人數(shù)
T = 150 # T為傳播時(shí)間
INI = (S_0, E_0, I_0, R_0) # INI為初始狀態(tài)下的數(shù)組
def funcSIERS(inivalue,_):
Y = np.zeros(4)
X = inivalue
Y[0] = -(beta*X[0]*X[2])/N+X[3]/Ts #易感個(gè)體變化
Y[1] = (beta*X[0]*X[2]/N-X[1]/Te) # 潛伏個(gè)體變化
Y[2] = X[1]/Te-gamma*X[2]#感染個(gè)體變化
Y[3] = gamma*X[2]-X[3]/Ts #治愈個(gè)體變化
return Y
T_range = np.arange(0, T+1)
RES = spi.odeint(funcSEIRS, INI, T_range)
plt.plot(RES[:,0],color = 'darkblue',label = 'Susceptible',marker =
'.')
plt.plot(RES[:,1],color = 'orange',label = 'Exposed',marker = '.')
plt.plot(RES[:,2],color = 'red',label = 'Infection',marker = '.')
plt.plot(RES[:,3],color = 'green',label = 'Recovery',marker = '.')
plt.title('SIERS Model')
plt.legend()
plt.xlabel('Day')
plt.ylabel('Number')
plt.show()