According to the equation
$$ \frac{\partial y}{\partial t} = -a\frac{\partial y}{\partial x} $$
I simulated this in python. I used center differentiation, and I determined step size based on Von-Neumann stability criterion for a linear equation $ \Delta t = \frac{\Delta x^2}{2a}$ .
This is what I get:https://gyazo.com/86024db42f71a6b5cb34eca7eb0d115f
import os
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.animation as animation
alpha = 1
seconds = 1000
dx = 1
dt = .1/alpha*dx**2
iter = int(np.round(seconds/dt))
print(iter, dt)
x = np.linspace(0,1000,1000)
# z = np.linspace(0,990, 10, dtype= int)
y = np.zeros([iter,1000])
y[0,:] = 50*np.sin(x[:]*np.pi/1000)
dy_t = np.zeros(1000)
for t in range(0,iter-1):
for x in range(1, 998):
dy_t[x] = -alpha*(y[t, x+1]-y[t,x-1])/(2*dx)
for x in range(1,998):
y[t+1,x] = y[t,x]+dy_t[x]*dt
# animation
fig, ax = plt.subplots()
line, = ax.plot(y[0,:])
def init():
line.set_ydata([np.nan] * 1000)
return line,
def animate(i):
line.set_ydata(y[i,:])
return line,
ani = animation.FuncAnimation(
fig, animate, range(0, iter), init_func=init, interval=5, blit=True, save_count=50)
plt.show()