One-way wave equation: $$ u_t + c u_x = 0$$ $$ u(0,x) = f(x) \quad \text{on} \, [0,1] $$
numpy
is used for vectors and linear algebrascipy
is unused here, but is common for scientific computing applicationsmatpllotlib.pyplot
is used for plotting%matplotlib inline
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
dx: grid spacing in the $x$-direction
x: grid coordinates
xx: really fine grid coordinates
nx = 100
x = np.linspace(0, 1, nx, endpoint=False)
dx = x[1] - x[0]
c = 1.0 # speed
T = 1.0 / c # end time
lmbda = 0.93
dt = dx * lmbda / c
nt = int(T/dt)
print('T = %g' % T)
print('tsteps = %d' % nt)
print(' dx = %g' % dx)
print(' dt = %g' % dt)
print('lambda = %g' % lmbda)
Square wave with amplitude 1
def f(x):
return [1 if xi>0.4 and xi<0.6 else 0 for xi in x]
import time
plotit = True
u = f(x)
if plotit:
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111)
ax.set_title('u vs x')
line1, = ax.plot(x, u, lw=3, clip_on=False)
def init():
line1.set_data([], [])
def timestepper(n):
uex = f((x - c * (n+1) * dt) % 1.0)
line1.set_data(x, uex)
return line1
from JSAnimation import IPython_display
from matplotlib import animation
animation.FuncAnimation(fig, timestepper, init_func=init, frames=nt, interval=20, blit=True)