写一个有限差分法模拟地震波的python代码
时间: 2023-02-20 22:58:18 浏览: 136
三维各向异性有限差分地震波模拟程序,用matlab编写.zip
5星 · 资源好评率100%
答:下面是一个用有限差分法模拟地震波的python代码:
import numpy as np
# Parameters
nx = 101
nt = 100
c = 1
dx = 2 / (nx - 1)
dt = 0.025
# Initial conditions
u = np.ones(nx)
u[int(0.5 / dx):int(1 / dx + 1)] = 2
# Loop through time
for n in range(nt):
un = u.copy()
for i in range(1, nx):
u[i] = un[i] - c * dt / dx * (un[i] - un[i - 1])
# Plot the solution
import matplotlib.pyplot as plt
plt.plot(np.linspace(0, 2, nx), u)
plt.show()
阅读全文