"Quantum harmonic oscillator"

clear

# Ref. "Quantum harmonic oscillator" in Wikipedia

# Compute psi_n(x) for n = 3

n = 3
m = hbar / omega
X = sqrt(m * omega / hbar) * x
H = (-1)^n * exp(X^2) * d(exp(-X^2),n)
C = (m * omega / pi / hbar)^(1/4) / sqrt(2^n * n!)
psi = C * exp(-m * omega * x^2 / 2 / hbar) * H

# Define a function to integrate f(x) numerically

d = 1/10
summa(f) = float(d*sum(k,-50,50,eval(f,x,d*k)))

-- Check normalization
check(infixform(summa(psi * psi)) == "1")

-- Expectation value for position
check(infixform(summa(psi * x * psi)) == "0")

-- Expectation value for momentum
check(infixform(summa(psi * (-i * hbar) * d(psi,x))) == "0")

-- Expectation value for kinetic energy
K = psi * (-hbar^2 / 2 / m) * d(psi,x,2)
check(infixform(summa(K)) == "1.75 hbar omega")

-- Expectation value for potential energy
V = psi * (m * omega^2 / 2) * x^2 * psi
check(infixform(summa(V)) == "1.75 hbar omega")

-- Expectation value for Hamiltonian (total energy)
check(infixform(summa(K + V)) == "3.5 hbar omega")

-- Energy eigenvalue
check(infixform(hbar * omega * (n + 0.5)) == "3.5 hbar omega")
