import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation nx = 100 nt = 100 x = np.linspace(0.,10.,nx) t = np.linspace(0.,10.,nt) q = np.zeros((nt,nx)) q[0,x<3] = 1.0 # Step function initial condition v0 = 0.4 # Constant advection velocity for n in range(0,nt-1): dt = t[n+1] - t[n] q[n+1,1:-1] = q[n,1:-1] - dt * v0 * ( q[n,2:] - q[n,:-2] ) / ( x[2:] - x[:-2] ) q[n+1,0] = q[n,0] q[n+1,-1] = q[n,-1] n = 0 def animupdate(frameNum, a0): global n,q,x,nt y = q[n,:] a0.set_data(x, y) n = (n + 1) % nt fig = plt.figure() ax = plt.axes(xlim=(x.min(),x.max()),ylim=(0.,1.5)) a0, = ax.plot([], []) anim = animation.FuncAnimation(fig,animupdate,fargs=(a0,),interval=40) plt.show()