我正在尝试编写一个python程序,使用具有二阶空间离散和周期边界条件的显式欧拉方法来求解一阶一维波动方程(传输方程)。
我是python的新手,我用numpy编写了这个程序,但我觉得我在某个地方犯了一个错误,因为wave变形了。一旦它离开左边界,它似乎变得扭曲,而不是简单地向左转换。我很确定这是一个编程错误,但有可能是舍入错误吗?我没有正确使用numpy吗?有关以更多蟒蛇风格的方式编写此程序的任何建议吗?谢谢!
PDE为
在有限差分形式中它是
求解
以下是我的尝试:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# wave speed
c = 1
# spatial domain
xmin = 0
xmax = 1
n = 50 # num of grid points
# x grid of n points
X, dx = np.linspace(xmin,xmax,n,retstep=True)
# for CFL of 0.1
dt = 0.1*dx/c
# initial conditions
def initial_u(x):
return np.exp(-0.5*np.power(((x-0.5)/0.08), 2))
# each value of the U array contains the solution for all x values at each timestep
U = []
# explicit euler solution
def u(x, t):
if t == 0: # initial condition
return initial_u(x)
uvals = [] # u values for this time step
for j in range(len(x)):
if j == 0: # left boundary
uvals.append(U[t-1][j] + c*dt/(2*dx)*(U[t-1][j+1]-U[t-1][n-1]))
elif j == n-1: # right boundary
uvals.append(U[t-1][j] + c*dt/(2*dx)*(U[t-1][0]-U[t-1][j-1]))
else:
uvals.append(U[t-1][j] + c*dt/(2*dx)*(U[t-1][j+1]-U[t-1][j-1]))
return uvals
# solve for 500 time steps
for t in range(500):
U.append(u(X, t))
# plot solution
plt.style.use('dark_background')
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
# animate the time data
k = 0
def animate(i):
global k
x = U[k]
k += 1
ax1.clear()
plt.plot(X,x,color='cyan')
plt.grid(True)
plt.ylim([-2,2])
plt.xlim([0,1])
anim = animation.FuncAnimation(fig,animate,frames=360,interval=20)
plt.show()
任何人都可以解释为什么这(波浪失真)正在发生?
答案 0 :(得分:3)