我想为非线性平流方程实现 Lax Wendroff 方法,它是
对于问题:
for
,
import numpy as np
def NLLaxWendroff(Nx,Nt,t_end):
Nx = Nx+1 # Number of grid points
xmax = 1. # Domain limit to the right
xmin = 0. # Domain limit to the left
h = (xmax-xmin)/(Nx-1) # Mesh size
x = np.arange(xmin,xmax,h) # Discretized mesh
U = np.exp(-10*(4*x-1)**2) # Initial solution
dt = 1.0/Nt # Time step
t = 0. # Initial time
# upwind scheme (7.18) from Dr.Georgoulis Notes after rearrangement (solving for u_{i}^{n+1})
while (t <= t_end):
t = t+dt
v = t/(2*h)
a = (t**2)/(2*h)
Un = U
Um = np.roll(Un,1)
Up = np.roll(Un,-1)
F_plus = 0.5 * (Up + Un)
F_minus = 0.5 * (Un + Um)
U = Un - v * (Um-Up) + a * (F_plus * Un-Um - F_minus *Um-Up)
plt.plot(x,U)
plt.title('t='+str(round(t_end,2)),fontsize=16)
plt.xlabel('x',fontsize=18)
plt.ylabel('u',fontsize=18)
plt.show()
NLLaxWendroff(100,100,0.6)
但是会出现以下错误:
<ipython-input-55-f6e78f745ea8>:24: RuntimeWarning: overflow encountered in multiply
U = Un - (v * (Up-Um)) + a * ( (F_plus * (Un-Um)) - (F_minus *(Up-Um)) )
<ipython-input-55-f6e78f745ea8>:24: RuntimeWarning: invalid value encountered in subtract
U = Un - (v * (Up-Um)) + a * ( (F_plus * (Un-Um)) - (F_minus *(Up-Um)) )
<ipython-input-55-f6e78f745ea8>:22: RuntimeWarning: invalid value encountered in subtract
F_plus = 1/2 * (Un - Um)
<ipython-input-55-f6e78f745ea8>:23: RuntimeWarning: invalid value encountered in subtract
F_minus = 1/2 * (Un - Up)
<ipython-input-55-f6e78f745ea8>:24: RuntimeWarning: invalid value encountered in add
U = Un - (v * (Up-Um)) + a * ( (F_plus * (Un-Um)) - (F_minus *(Up-Um)) )
```