我正在尝试使用matplotlib动画化一组5个耦合的ODE,以描述流量随时间的变化。
我的项目是根据起始的湖泊和不同的流速确定污染物如何通过大湖前进。为此,我有5个ODE。到目前为止,我已经成功建立了一个模型并获得了不同流程和不同起始位置的结果。我目前仍在为这些结果设置动画。我制作了一些动画,但是它们不能正常工作。
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import matplotlib.animation as anim
""" Volume [m^3] """
V1 = 12004e9 # Superior
V2 = 3550e9 # Huron
V3 = 4860e9 # Michigan
V4 = 499e9 # Erie
V5 = 1656e9 # Ontario
""" Flows [m^3/s] """
r1 = 2.2e3*3.154e7 #->Superior
r2 = 1.6e3*3.154e7 #->Huron
r3 = 1.5e3*3.154e7 #->Michigan
r4 = 0.7e3*3.154e7 #->Erie
r5 = 1.1e3*3.154e7 #->Ontario
r6 = 5.3e3*3.154e7 #Huron->Erie
r7 = 6e3*3.154e7 #Erie->Ontario
r8 = 7.1e3*3.154e7 #Ontario->
r = np.array([r1,r2,r3,r4,r5,r6,r7,r8])
""" Time """
t=18000
T = np.linspace(0,t,t+1)
""" Initial Condition"""
x10 = 0.001*V1
S0 = np.array([x10,0,0,0,0])
def Sup(S,T,R):
dx1dt = -R[0]/V1*S[0]
dx2dt = R[0]/V1*S[0] + R[2]/V3*S[2] - R[5]/V2*S[1]
dx3dt = -R[2]/V3*S[2]
dx4dt = R[5]/V2*S[1] - R[6]/V4*S[3]
dx5dt = R[6]/V4*S[3] - R[7]/V5*S[4]
return np.array([dx1dt, dx2dt, dx3dt, dx4dt, dx5dt])
R = r #there are multiple flow variants used later in the code
S1 = odeint(Sup,S0,T,args=(R,))
SSuperior = S1[:,0]
SHuron = S1[:,1]
SMichigan = S1[:,2]
SErie = S1[:,3]
SOntario = S1[:,4]
""" Animation"""
fig = plt.figure()
ax = plt.axes(xlim = (-50, 8500))
line, = ax.plot([], [], animated = True, lw = 2)
def init():
line.set_data([],[])
ax.set_xlim(T.min(),T.max())
ax.set_ylim(SOntario.min(), SOntario.max())
return line,
def animate(i):
line.set_data(T,SOntario[i])
return line,
anim = anim.FuncAnimation(fig, animate, frames = 500, init_func = init, blit=True )
我第一个期望的动画只是对绘图进行动画处理,但是我所能获得的只是一条水平线上下移动。虽然这部分正确,但这不是预期的动画