我想用初始条件 U(x,0) = n(n+1)/cosh^2(x) 求解 Korteweg-De Vries 方程。然而,当 n>1(n 是整数)时,我的系统在数值上变得不稳定,这可能是什么原因?
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
# Parameters
L = 40 # Length of the interval
N = 256 # Number of grid points
dx = L / N # Spatial step size
x = np.linspace(-L/2, L/2, N) # Discretized spatial domain
dt = 0.1 # Time step size
T = 20 # End time
n = 1 # Parameter n for the initial condition
# Initial condition u(x,0) = n*(n+1)/cosh^2(x)
u0 = (n * (n + 1)) / np.cosh(x)**2
# Function to compute derivatives according to the KdV equation
def derivs(t, u):
# Periodic boundary conditions are applied using np.roll
uxx = np.roll(u, -1) - 2 * u + np.roll(u, 1) # Second derivative
uxx /= dx**2
uxxx = np.roll(uxx, -1) - np.roll(uxx, 1) # Third derivative
uxxx /= 2 * dx
# KdV equation: time derivative of u
return -6 * u * (np.roll(u, -1) - np.roll(u, 1)) / (2 * dx) - uxxx
# Solve the KdV equation using a 4th-order Runge-Kutta method
sol = solve_ivp(derivs, [0, T], u0, method='RK45', t_eval=np.arange(0, T, dt))
# Animation of the solution
plt.figure()
for i in range(len(sol.t)):
plt.clf() # Clear the previous plot
plt.plot(x, sol.y[:, i]) # Plot the solution at time t[i]
plt.title(f"Solution of the KdV equation at t={sol.t[i]:.1f}")
plt.xlabel("x") # Label for the x-axis
plt.ylabel("u(x,t)") # Label for the y-axis
plt.ylim([-0.5, n*(n+1)+0.5]) # Set y-axis limits for better visualization
plt.pause(0.1) # Short pause to animate the plot
plt.show() # Show the final plot window
对于一般的 n 来说,你的初始条件是错误的。应该是
u0 = (n * (n + 1)) / np.cosh( 0.5 * np.sqrt(2*n*(n+1.0)) * x ) ** 2
尽管如此,您仍然会得到少量的色散。