Skip to content

Instantly share code, notes, and snippets.

@mick001
Created August 28, 2015 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mick001/6e81dbdded68802fc7f2 to your computer and use it in GitHub Desktop.
Save mick001/6e81dbdded68802fc7f2 to your computer and use it in GitHub Desktop.
Transport equation with decay, a Python implementation. Full article can be found at http://www.firsttimeprogrammer.blogspot.com/2015/07/pdes-time-again-transport-equation.html
#Transport equation with decay implementation
import numpy as np
from numpy import pi
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
fig.set_dpi(100)
ax1 = fig.add_subplot(1,1,1)
#Initial contitions
x0 = np.linspace(-2,15,10000)
t0 = 0
#Increment
dt = 0.01
#Wave speed k > 0
k = 3
#Decay constant b > 0
b = 2
#Scale
scale = 4
#Transport function
def u(x,t):
return scale*np.exp(-(x-k*t)**2)*np.exp(-b*t)
t = []
g = []
for i in range(500):
g.append(u(x0,t0))
t.append(t0)
t0 = t0 + dt
k = 0
def animate(i):
global k
ax1.clear()
plt.plot(x0,g[k],color='red',label='Time elapsed: '+str(round(t[k],4)))
plt.grid(True)
plt.ylim([-1,scale+1])
plt.xlim([-2,15])
plt.legend(loc=2)
plt.title('Transport equation with decay')
k += 1
anim = animation.FuncAnimation(fig,animate,frames=360,interval=20)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment