particle in a box by shooting method¶
- BVP solved by many IVPs.
- Unlike IVP, BVP has many solutions: that's how the energy levels: E(n) come from.
- u'(0) does not do much: only control the overall normalization.
- u(x=L) = 0 is satisfied only for some quantized E(n).
using Plots, LinearAlgebra, ForwardDiff
function box(ee; wf = false)
# standard shooting method:
# for a given energy ee, compute u(x=L)
# wf = true will give (x_arr, u_arr) also
# particle in a box of size L
pot(x) = 0.0
L = 5.0
dx = 0.025
x_arr = range(0, L, step = dx)
u_arr = similar(x_arr)
du_arr = similar(x_arr)
# shooting + stupid-fied Numerov
u_arr[1] = 0.0
du_arr[1] = 0.25 # just a guess
# useful construction: vector form
_f(x, uu) = [uu[2], -2 * (ee - pot(x)) * uu[1]]
for i1 = 1:length(x_arr)-1
xnow = x_arr[i1]
# vector form
u1 = [u_arr[i1], du_arr[i1]]
# RK4 won't disappoint
F1 = dx * _f(xnow, u1)
F2 = dx * _f(xnow + dx / 2, u1 + F1 / 2)
F3 = dx * _f(xnow + dx / 2, u1 + F2 / 2)
F4 = dx * _f(xnow + dx, u1 + F3)
u2 = u1 + (F1 + 2 * F2 + 2 * F3 + F4) / 6
u_arr[i1+1], du_arr[i1+1] = u2
end
if wf
return x_arr, u_arr
else
return u_arr[end]
end
end
box (generic function with 1 method)
let
L = 5.0
Egs = pi^2 / 2 / L^2
E_arr = range(0.0, 8^2 * Egs, length = 250)
p1 = plot(ylabel="u(x=L, E)", xlabel="E", title = "particle in a box",
ylim=(-0.6, 0.6))
E_exact = [n^2 * Egs for n in 1:7]
plot!(p1, E_arr, box.(E_arr), label="")
scatter!(p1, E_exact, x -> 0.0, label="exact")
end
let
L = 5.0
Egs = pi^2 / 2 / L^2
x_arr, u_arr = box(Egs, wf = true)
fig = plot(title="particle in a box", xlabel="x")
for nn = 1:5
ee = (nn)^2 * Egs
x_arr, u_arr = box(ee, wf = true)
plot!(fig, x_arr, u_arr / maximum(u_arr) .+ ee, label = "")
end
plot!(fig, x_arr, [@. nn^2 * Egs * x_arr^0 for nn = 1:5],
label = "",
color = "gray",
line = (dash = :dash))
plot!(fig, fill(0, 20), (0:6/19:6), label="", color="black")
plot!(fig, fill(5, 20), (0:6/19:6), label="", color="black")
end
Harmonic potential with Numerov method¶
Known results:
\begin{align} E_N &= (N + \frac{1}{2}) \\ \psi_N &= (x - \partial_x)^N \, e^{-x^2/2} \end{align}
function SHO_wf(n::Integer)
function operator(f)
return x -> x * f(x) - ForwardDiff.derivative(f, x)
end
if n == 0
return x -> exp(-x^2 / 2)
else
return operator(SHO_wf(n - 1))
end
end
function hermite(n)
# you may also want to know this...
if n == 0
return x -> 1
elseif n == 1
return x -> 2 * x
else
return x -> 2 * x * hermite(n - 1)(x) - 2 * (n - 1) * hermite(n - 2)(x)
end
end
hermite (generic function with 1 method)
function sho(ee; wf = false, L = 6.0)
pot(x) = 0.5 * x^2
dx = 0.01
x_arr = range(-L, L, step = dx)
u_arr = similar(x_arr)
# shooting + Numerov
u_arr[1] = 0.0
u_arr[2] = 0.25 * dx # only affect normalization
# useful construction
f(x, u) = -2 * (ee - pot(x)) * u
for i1 = 2:length(x_arr)-1
x1 = x_arr[i1-1]
x2 = x_arr[i1]
x3 = x_arr[i1+1]
u1 = u_arr[i1-1]
u2 = u_arr[i1]
f1 = f(x1, u1)
f2 = f(x2, u2)
# fixed point iteration
u3 = u2 # starting guess
for i2 = 1:10
u3 = (-u1 + 2 * u2) + dx^2 * (f(x3, u3) + 10 * f2 + f1) / 12.0
end
u_arr[i1+1] = u3
end
if wf
return x_arr, u_arr
else
return u_arr[end]
end
end
let
L = 6.0
Egs = 1 / 2
E_arr = range(0.0, 26, length = 120)
plot(
E_arr,
sho.(E_arr, L = L),
ylim = (-1.0, 1.0),
label = "u(x=L, E)",
xlabel = "E",
title = "harmonic potential (L=$L)",
)
scatter!([nn + 1 / 2 for nn = 0:25], x -> 0.0, label = "exact")
end
let
L = 6.0
x_arr, u_arr = sho(0.5, wf = true, L = L)
p1 = plot(
x -> x^2,
label = "",
color = "black",
ylim = (0, 10),
title = "harmonic potential (L=$L)",
)
for nn = 0:7
ee = nn + 1 / 2
x_arr, u_arr = sho(ee, wf = true, L = L)
plot!( x_arr, u_arr / maximum(u_arr) .+ ee, label = "" )
end
plot!(
x_arr,
[@. (nn + 1 / 2) * x_arr^0 for nn = 1:7],
label = "",
color = "gray",
line = (dash = :dash),
)
x1_arr = range(-L, L, length = 40)
psi_check = SHO_wf(6).(x1_arr)
psi_check /= maximum(psi_check)
scatter!(x1_arr, psi_check .+ 6.5, color = "grey", label = "")
end
Matrix Method¶
The Hamiltonia
\begin{align} H = -\frac{1}{2 m} \frac{d^2}{dx^2} + \frac{1}{2} m \omega^2 x^2 \end{align}
for simplicity, we set $m = \omega = 1$. We will be solving the system on an $N_1 \times N_1$ spatial grid with lattice spacing $a$, i.e. for $n = 1, 2, \ldots$
It is useful to think of $x$ as a label:¶
$x_i \rightarrow -L + (i-1) \, a$
and $\psi(x)$ is just a column vector
$\psi(x) \rightarrow \psi(i)$
The $\hat{x}$ operator is simply a multiplication operator: i.e. it is diagonal.¶
The differential operator is more tricky:¶
$\frac{d}{dx}\psi(x) \rightarrow (\psi(n+1)-\psi(n-1))/(2a)$
or
$\frac{d}{dx} \rightarrow \frac{1}{2 a} \, (\delta(n+1, n) - \delta(n-1, n))$
i.e. it is off-diagonal. And a 2nd order differential operator becomes
$$ \left( \frac{d^2}{dx^2} \right)_{mn} \rightarrow \frac{1}{a^2} (\delta(m+1,n) + \delta(m-1, n) - 2 \delta(m,n)) $$
The final Hamiltonian is then
\begin{align} H_{ij} = -\frac{1}{2 m} \, \frac{\delta(i+1,j) + \delta(i-1, j) - 2 \delta(i,j)}{a^2} + V(x_i) \, \delta(i,j) \end{align}
strategy:¶
- construct the Hamiltonian as a matrix
- diagonalize it and solve for eigenvalues and eigenvectors
- no blind shooting $E$ is involved!
function SHO_ham(; L = 6.0, N1 = 60, fivept = false)
pot(x) = 0.5 * x^2
x_arr = range(-L, L, length = N1)
dx = 2 * L / (N1 - 1)
# represent the Hamiltonian on the grid
# useful construction
kdelta(i1, i2) = i1 == i2 ? 1 : 0
ham = zeros(N1, N1)
for i1 = 1:N1, i2 = 1:N1
x = x_arr[i1]
if fivept == true
# 5-point
kd_arr = [kdelta(i1, i2 + jj) for jj = -2:2]
coeff = [-1, 16, -30, 16, -1] / 12.0
else
# 3-point
kd_arr = [kdelta(i1, i2 + jj) for jj = -1:1]
coeff = [1, -2, 1]
end
kin = sum(coeff .* kd_arr)
kin *= -0.5 / dx^2
potential = pot(x) * kdelta(i1, i2)
# can also regulate further...
ham[i1, i2] = kin + potential
end
return ham
end
SHO_ham (generic function with 1 method)
let
# finite volume effect
L = 9.0
N1 = 75
ham = SHO_ham(L = L, N1 = N1)
evalues, evecs = eigen(ham)
scatter(evalues[1:25], label = "matrix method 3-pt.")
ham = SHO_ham(L = L, N1 = N1, fivept = true)
evalues, evecs = eigen(ham)
scatter!(evalues[1:25], label = "matrix method 5-pt.")
plot!([nn + 0.5 for nn = 0:25], label = "exact: L=$L", color = "black")
end
save this gem!¶
function hamX(pot, xv; m=1.0)
# pot(x) = -10*exp(-x^2)
N = length(xv)
dx = (xv[end]-xv[1])/(N-1)
# represent the Hamiltonian on the grid
# useful construction
kdelta(i1, i2) = i1 == i2 ? 1 : 0
ham = zeros(N, N)
for i1 = 1:N, i2 = 1:N
x = xv[i1]
kin = kdelta(i1, i2 + 1) + kdelta(i1, i2 - 1) - 2*kdelta(i1, i2)
kin *= -0.5/m / dx^2
potential = pot(x) * kdelta(i1, i2)
# can also regulate further...
ham[i1, i2] = kin + potential
end
return ham
end
hamX (generic function with 1 method)
effect of squeezing the potential: energy levels got pushed up!¶
function _plotfig(L)
xv = range(-L, L, length=120)
# box pot
bpot = x -> abs(x) > (L-0.8) ? 1E8 : 0.0
ham1 = hamX(bpot, xv, m = 0.5)
evalues, evecs = eigen(ham1)
p1 = plot(
bpot,
label = "",
color = "black",
ylim = (-1, 6),
title = "box potential (L=$L)",
)
for nn = 1:7
ee = evalues[nn]
u_arr = evecs[:, nn]
plot!( xv, u_arr / maximum(u_arr) .+ ee, label = "" )
end
plot!(
xv,
[@. evalues[nn] * xv^0 for nn = 1:7],
label = "",
color = "gray",
line = (dash = :dash),
)
return p1
end
p1 = _plotfig(5.0)
p2 = _plotfig(6.0)
plot(p2, p1, layout=(1,2))