使用numpy解决具有波状初始条件的传输方程

时间:2018-03-29 20:47:38

标签: python numpy numerical-methods differential-equations pde

我正在尝试编写一个python程序,使用具有二阶空间离散和周期边界条件的显式欧拉方法来求解一阶一维波动方程(传输方程)。

我是python的新手,我用numpy编写了这个程序,但我觉得我在某个地方犯了一个错误,因为wave变形了。一旦它离开左边界,它似乎变得扭曲,而不是简单地向左转换。我很确定这是一个编程错误,但有可能是舍入错误吗?我没有正确使用numpy吗?有关以更多蟒蛇风格的方式编写此程序的任何建议吗?谢谢!

PDE为waveeq

在有限差分形式中它是fdeq

求解ujn1

sol

以下是我的尝试:

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()

这就是波浪的开始 enter image description here

这就是它经过几次迭代后的结果 enter image description here

任何人都可以解释为什么这(波浪失真)正在发生?

1 个答案:

答案 0 :(得分:3)

您的实施是正确的。失真来自相对较大的空间步长dx。在其当前值0.2处,它与波的大小相当,这使得波在图上可见多边形。这些离散误差累积超过500步。这是我从plt.plot(X, U[-1])获得的代码:

distorted

这是我在使用n = 100(将时间和空间分步减半),运行解决方案for t in range(1000)以补偿较小的时间步长,并再次绘制plt.plot(X, U[-1])之后得到的结果:< / p>

much better

du / dx的对称差分近似具有与三阶导数成比例的阶dx**3的误差。这些累积的方式很复杂,因为解决方案正在四处移动,但在任何情况下,如果dx与它一起扩展,则dt会改善问题。