根据等式
我在python中模拟了这个。我使用了中心微分,并根据线性方程 的 Von-Neumann 稳定性标准确定了步长。
这就是我得到的: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()