Here's a Python script that simulates and animates the evolution of a wave function in an infinite potential well according to the Schrödinger equation:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import animation
# Parameters
L = 1.0 # Well width
N = 500 # Number of spatial points
dx = L / N # Spatial step
x = np.linspace(0, L, N) # Spatial grid
dt = 0.0001 # Time step
t_max = 0.1 # Maximum simulation time
hbar = 1.0 # Reduced Planck constant
m = 1.0 # Particle mass
# Initial wave function (Gaussian wave packet)
def psi_initial(x):
x0 = L / 2 # Center of the packet
sigma = L / 20 # Width of the packet
k0 = 50 # Initial momentum
return np.exp(-(x - x0)**2 / (2 * sigma**2)) * np.exp(1j * k0 * x)
# Normalize the wave function
psi = psi_initial(x)
norm = np.sqrt(np.sum(np.abs(psi)**2) * dx)
psi = psi / norm
# Time evolution using the split-operator method
def evolve_split_operator(psi, dt):
# Fourier transform to momentum space
psi_k = np.fft.fft(psi)
# Momentum space grid
k = 2 * np.pi * np.fft.fftfreq(N, dx)
# Apply kinetic energy operator in momentum space
psi_k = np.exp(-1j * hbar * k**2 / (2 * m) * dt) * psi_k
# Transform back to position space
psi = np.fft.ifft(psi_k)
# Apply potential energy operator (zero inside the well, infinite outside)
# For infinite well, we just need to ensure the wave function is zero at boundaries
psi[0] = psi[N-1] = 0
return psi
# Set up the figure and axis
fig, ax = plt.subplots(figsize=(10, 6))
line_real, = ax.plot(x, np.real(psi), 'b-', label='Real part')
line_imag, = ax.plot(x, np.imag(psi), 'r-', label='Imaginary part')
line_prob, = ax.plot(x, np.abs(psi)**2, 'g-', label='Probability density')
ax.set_xlim(0, L)
ax.set_ylim(-2, 2)
ax.set_xlabel('Position')
ax.set_ylabel('Wave function')
ax.set_title('Wave Function Evolution in Infinite Potential Well')
ax.legend()
# Animation function
def animate(i):
global psi
# Evolve the wave function for 10 time steps per frame
for _ in range(10):
psi = evolve_split_operator(psi, dt)
# Update the plot
line_real.set_ydata(np.real(psi))
line_imag.set_ydata(np.imag(psi))
line_prob.set_ydata(np.abs(psi)**2)
return line_real, line_imag, line_prob
# Create the animation
frames = int(t_max / (dt * 10))
anim = FuncAnimation(fig, animate, frames=frames, interval=50, blit=True)
# Save the animation (optional)
# anim.save('infinite_well.mp4', writer=animation.FFMpegWriter(fps=30))
plt.tight_layout()
plt.show()
Would you like me to explain or break down this code?