Using dimensionless length
The integration function uses a second order approximation for second derivative.
Developed for PHYS 212, March 2016
Modified for Jupyter notebook, Spring 2018
Slight updates December 2020
Marty Ligare
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
# Following is an Ipython magic command that puts figures in the notebook.
%matplotlib notebook
# M.L. modification of matplotlib defaults
# Changes can also be put in matplotlibrc file,
# or effected using mpl.rcParams[]
mpl.style.use('classic')
plt.rc('figure', figsize = (6, 4.5)) # Reduces overall size of figures
plt.rc('axes', labelsize=16, titlesize=14)
plt.rc('figure', autolayout = True) # Adjusts supblot parameters for new size
def u(x):
return x**2
def integrate(x):
y = np.zeros(len(x)) # Initialize array for values of psi
y[0] = 1
y[1] = 1
for i in range(1,n-1): # Update values of y[2], y[3], y[4], etc.
y[i+1] = 2*y[i] - y[i-1] - dx**2*(e-u(x[i]))*y[i]
return y
e
(energy)¶e = 9.00001 # "Guess" for value of dimensionless energy
lep = 0 # Left end-point
rep = lep + 6 # Right end-point
n = 1000001 # Number of points (number of intervals = np - 1)
dx = (rep-lep)*1./(n-1) # Distance between points
x = np.linspace(lep, rep, n) # Create array of x-values
y = integrate(x) # Create values for wavefunction
plt.figure()
plt.xlabel('$x^\prime$') # Label for horizontal axis
plt.ylabel("$\psi$") # Label for vertical axis
plt.title("simple harmonic oscillator")
plt.grid(True)
plt.axhline(0,color='green') # Makes solid green x-axis
plt.ylim(-2,2)
plt.plot(x,y);
The "guess" of $E^\prime = 9.00001$ gives decent as $x^\prime$ gets large. Recall
$$ E = \frac{1}{2}\hbar\omega E^\prime = \frac{1}{2}\hbar\omega \times 9 = 4.5\hbar\omega $$which is the corrrect energy for the illustrated $n=4$ state.
%version_information
is an IPython magic extension for showing version information for dependency modules in a notebook;
%version_information
is available on Bucknell computers on the linux network. You can easily install it on any computer.
%load_ext version_information
version_information numpy, matplotlib