|
''' |
|
Solution of the advection equation by the Lax-Friedrichs scheme |
|
by Shelvean Kapita, 2022 |
|
''' |
|
|
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
|
|
def u_0(x): |
|
# val = np.exp(-(x-4)**2) |
|
val = np.where(np.abs(x)<0.5, 1-2*np.abs(x),0) |
|
return val |
|
|
|
|
|
mu = 0.9 |
|
J = 301 |
|
L = 10 |
|
h = L/(J-1) |
|
k = mu*h |
|
tfinal = 3 |
|
tsteps = int(np.round(tfinal/k)) |
|
x = np.linspace(-2,8,J) |
|
v_0 = u_0(x) |
|
u_ex = u_0(x-tfinal) |
|
v = np.zeros((v_0.shape)) |
|
|
|
for j in range(tsteps): |
|
v[1:J-1] = 0.5*(1-mu)*v_0[2:J] + 0.5*(1+mu)*v_0[0:J-2] |
|
v[0],v[J-1] = 0, 0 |
|
v_0 = np.copy(v) |
|
plt.plot(x,v,linewidth='1.5') |
|
plt.plot(x,u_ex, linewidth='0.5',color='red') |
|
plt.ylim(-0.5, 2.1) |
|
plt.show() |
|
|