---
title: Approximation methods lab
heading: Perturbation theory, the variational principle and WKB, checked against exact numerical answers
eyebrow: B10 · Lesson 39 · Approximation methods lab
xp: 50
---

::: hook
Lessons 35 to 38 gave three ways to estimate energies when the Schrödinger equation can't be solved: perturbation theory, the variational principle and WKB. Each has a regime where it shines and one where it fails, and the only honest way to know which you're in is to compare with an exact answer. In this lab you'll compute exact levels by diagonalizing large matrices, then test each approximation against them on the anharmonic oscillator and the triangular well, the MOSFET channel's potential. Units are ħ = m = 1.
:::

::: play
- A particle in the quartic well $V = gx^4$ (ħ = m = 1). The curve is the variational energy for a Gaussian trial function $e^{-bx^2}$ against b; the dashed line is the exact ground-state energy.
- The curve's minimum always sits above the exact line, as the variational principle guarantees, here by 2%.
- Change g: both scale as $g^{1/3}$, so the 2% stays put. The readouts give the variational minimum and the exact value.
:::

```sim plot
view: graph
x: b = 0.2..3
y: E = 0..2
slider: g = 0.2..5 step 0.1 = 1
plot: b/2 + 3*g/(16*b^2) | label: Gaussian trial energy | samples: 400
plot: 0.667986*g^(1/3) | dashed | label: exact ground state
readout: variational minimum = 0.75*(0.75*g)^(1/3) | digits: 4
readout: exact = 0.667986*g^(1/3) | digits: 4
```

::: predict
## WKB estimates the levels of the quartic well $V = x^4$. Where is it least accurate?

- [x] The ground state
- [ ] The highest levels
- [ ] It's equally accurate for all levels
- [ ] It fails completely for this potential

::: reveal
The ground state, by 18%, while the second level is within 1.3% and the third within 0.6%. WKB assumes the wavelength changes slowly compared with itself, which holds for highly excited states with many wiggles and fails for the ground state, which has none. It's a semiclassical method: best where the motion is most classical.
:::
:::

::: nameit
## Exact answers by matrix

Write the Hamiltonian in a large basis, here the harmonic oscillator's states, using $\hat x = \frac{\hat a + \hat a^\dagger}{\sqrt{2\omega}}$ and $\hat p = i\sqrt{\frac\omega2}(\hat a^\dagger - \hat a)$, and diagonalize. With 60 to 80 basis states the lowest levels converge to many digits. Choosing ω near the problem's natural scale speeds convergence.

## The three approximations

- **Perturbation theory** for $H = H_0 + \lambda x^4$: the ground state shifts by $\frac34\lambda$ in first order and $-\frac{21}{8}\lambda^2$ in second; the series diverges for any λ, so it's useful only while λ is small.
- **Variational:** a Gaussian $e^{-bx^2}$ in $\frac{p^2}{2} + gx^4$ gives $\langle H\rangle = \frac b2 + \frac{3g}{16b^2}$, minimized at $b^3 = \frac{3g}{4}$ with $E = \frac34b$: always above the truth.
- **WKB:** $\oint p\,dx = (n + \frac12)h$, which for $x^4$ becomes $E_n = \left[\frac{(n + \frac12)\pi}{2\sqrt2\,I}\right]^{4/3}$ with $I = \int_0^1\sqrt{1 - u^4}\,du = 0.8740$.

## Finite differences

For a potential with a hard wall, like the triangular well $V = Fx$ for x > 0, a finite-difference matrix on a grid gives the levels directly; they match $E_n = \left(\frac{F^2}{2}\right)^{1/3}a_n$ with the Airy zeros $a_n$ = 2.33811, 4.08795, 5.52056.
:::

::: example
## How well does perturbation theory do for $\frac{p^2}{2} + \frac{x^2}{2} + \lambda x^4$ at λ = 0.01 and λ = 0.1?

::: solution
1. **At λ = 0.01.** The exact ground state is 0.507256, and first order gives 0.5075, within 0.05%.
2. **At λ = 0.1, first order.** The exact value is 0.559146, while first order gives 0.575, 2.8% high.
3. **At λ = 0.1, second order.** Adding second order gives 0.54875, now 1.9% low.
4. **Why it stops helping.** The corrections alternate in sign and eventually grow, since the series is asymptotic: past a few terms, more terms make it worse. For strong anharmonicity the matrix or variational method is the right tool.

**Answer:** at λ = 0.01 first order is within 0.05%; at λ = 0.1 it's 2.8% high, and second order is 1.9% low.
:::
:::

::: example
## What does the Gaussian trial function give for the pure quartic well $\frac{p^2}{2} + x^4$, and how close is it?

::: solution
1. **Best width.**
   $$\begin{aligned} b &= \left(\frac34\right)^{1/3} \\ &= 0.9086 \end{aligned}$$
2. **Energy.**
   $$\begin{aligned} E &= \frac34b \\ &= 0.6814 \end{aligned}$$
3. **The exact value.** Diagonalizing in a basis of 80 oscillator states gives 0.667986, so the estimate is 2.0% high, with a single parameter.
4. **Why it does so well.** A trial function with the right shape, smooth with no nodes, gets the energy right even when its details differ, because errors in the wave function enter the energy only at second order.

**Answer:** the Gaussian gives 0.6814, 2.0% above the exact 0.667986.
:::
:::

::: example
## Compare WKB with the exact levels of the quartic well for n = 0, 1 and 2.

::: solution
1. **WKB levels.** WKB gives 0.5463, 2.3636 and 4.6705.
2. **Exact levels.** They're 0.6680, 2.3936 and 4.6968.
3. **Errors.** The errors are 18%, 1.3% and 0.6%.
4. **The pattern.** It's typical: WKB improves rapidly with n, while the variational method is best for the ground state. Used together, they bracket a problem from both ends.

**Answer:** WKB is 18% off for n = 0, 1.3% for n = 1 and 0.6% for n = 2.
:::
:::

::: check
kind: python

## Step 1. Write `anharmonic(lam, Nb)` that builds $\frac12(p^2 + x^2) + \lambda x^4$ in the first Nb oscillator states ($\hat x = \frac{\hat a + \hat a^\dagger}{\sqrt2}$) and returns the lowest eigenvalue.

```python starter
import numpy as np

def anharmonic(lam, Nb):
    return 0.5 + 0.75 * lam

print(anharmonic(0.1, 60))
```

```python solution
import numpy as np

def anharmonic(lam, Nb):
    a = np.diag(np.sqrt(np.arange(1, Nb)), 1)
    X = (a + a.T) / np.sqrt(2)
    H = np.diag(np.arange(Nb) + 0.5) + lam * np.linalg.matrix_power(X, 4)
    return float(np.linalg.eigvalsh(H)[0])

print(anharmonic(0.1, 60))
```

```python tests
assert abs(anharmonic(0.01, 60) - 0.5072562) < 1e-6, f"λ = 0.01: 0.507256; got {anharmonic(0.01, 60):.7f}."
assert abs(anharmonic(0.1, 60) - 0.5591463) < 1e-6, f"λ = 0.1: 0.559146; got {anharmonic(0.1, 60):.7f}."
assert abs(anharmonic(0.0, 10) - 0.5) < 1e-12, "With no anharmonicity it's the oscillator's ½."
```

::: right
Right. The matrix is exact once enough basis states are included; truncating the basis is the only approximation, and 60 states are plenty here.
:::
:::

::: check
kind: python

## Step 2. Write `variational(g)` that minimizes $\frac b2 + \frac{3g}{16b^2}$ over 30 000 values of b from 0.05 to 3 and returns `(b, E)`.

```python starter
import numpy as np

def variational(g):
    return 1.0, 0.5 + 3 * g / 16

print(variational(1.0))
```

```python solution
import numpy as np

def variational(g):
    b = np.linspace(0.05, 3, 30000)
    E = b / 2 + 3 * g / (16 * b * b)
    i = int(np.argmin(E))
    return float(b[i]), float(E[i])

print(variational(1.0))
```

```python tests
b, E = variational(1.0)
assert abs(b - 0.9086) < 1e-3 and abs(E - 0.68142) < 1e-4, f"Expect b = 0.909 and E = 0.6814; got {b:.4f}, {E:.5f}."
assert E > 0.667986, "The variational energy must lie above the exact one."
```

::: right
Right. $E = \frac34b$ at the minimum, 2% above the truth: the variational principle at work.
:::
:::

::: check
kind: python

## Step 3. Write `quartic_exact(Nb)` returning the lowest three levels of $\frac{p^2}{2} + x^4$ by diagonalizing in Nb oscillator states of frequency ω = 2, with $\hat x = \frac{\hat a + \hat a^\dagger}{\sqrt{2\omega}}$ and $\hat p = i\sqrt{\frac\omega2}(\hat a^\dagger - \hat a)$.

```python starter
import numpy as np

def quartic_exact(Nb):
    return np.array([0.68, 2.4, 4.7])

print(quartic_exact(80))
```

```python solution
import numpy as np

def quartic_exact(Nb):
    w = 2.0
    a = np.diag(np.sqrt(np.arange(1, Nb)), 1)
    X = (a + a.T) / np.sqrt(2 * w)
    P = 1j * np.sqrt(w / 2) * (a.T - a)
    H = (P @ P).real / 2 + np.linalg.matrix_power(X, 4)
    return np.linalg.eigvalsh(H)[:3]

print(quartic_exact(80))
```

```python tests
import numpy as np
E = quartic_exact(80)
assert np.allclose(E, [0.667986, 2.393644, 4.696795], atol=1e-5), f"Expect 0.66799, 2.39364, 4.69680; got {E}."
```

::: right
Right. These are the reference values: 0.667986, 2.393644 and 4.696795, which every approximation in this lab is judged against.
:::
:::

::: check
kind: python

## Step 4. Write `wkb_quartic(n)` returning the WKB estimate of level n of $\frac{p^2}{2} + x^4$: compute $I = \int_0^1\sqrt{1 - u^4}\,du$ with `np.trapz` (or `np.trapezoid`) on a million points, then $E_n = \left[\frac{(n + \frac12)\pi}{2\sqrt2\,I}\right]^{4/3}$.

```python starter
import numpy as np

def wkb_quartic(n):
    return (n + 0.5)**(4 / 3)

print([wkb_quartic(n) for n in range(3)])
```

```python solution
import numpy as np

def wkb_quartic(n):
    u = np.linspace(0, 1, 1000001)
    f = np.sqrt(1 - u**4)
    I = float(np.sum((f[1:] + f[:-1]) / 2) * (u[1] - u[0]))
    return ((n + 0.5) * np.pi / (2 * np.sqrt(2) * I))**(4 / 3)

print([wkb_quartic(n) for n in range(3)])
```

```python tests
import numpy as np
w = [wkb_quartic(n) for n in range(3)]
assert np.allclose(w, [0.54627, 2.36356, 4.67052], atol=2e-4), f"Expect 0.5463, 2.3636, 4.6705; got {w}."
exact = [0.667986, 2.393644, 4.696795]
errs = [abs(a - b) / b for a, b in zip(w, exact)]
assert errs[0] > 0.15 and errs[2] < 0.01, "WKB is poor for the ground state and good for higher ones."
```

::: right
Right. The errors fall from 18% to 0.6% over three levels: WKB becomes exact as the motion becomes classical.
:::
:::

::: check
kind: python

## Step 5. Write `triangular(F, n)` returning the lowest n levels of $V = Fx$ for x > 0 with a hard wall at 0, from a finite-difference matrix on 2000 interior points spanning 0 to 20.

```python starter
import numpy as np

def triangular(F, n):
    return np.arange(1, n + 1) * F

print(triangular(1.0, 3))
```

```python solution
import numpy as np

def triangular(F, n):
    N, L = 2000, 20.0
    dx = L / (N + 1)
    xs = dx * np.arange(1, N + 1)
    c = 1 / (2 * dx * dx)
    H = np.diag(2 * c + F * xs) + np.diag(-c * np.ones(N - 1), 1) + np.diag(-c * np.ones(N - 1), -1)
    return np.linalg.eigvalsh(H)[:n]

print(triangular(1.0, 3))
```

```python tests
import numpy as np
E = triangular(1.0, 3)
airy = 0.5**(1 / 3) * np.array([2.33811, 4.08795, 5.52056])
assert np.allclose(E, airy, rtol=1e-4), f"Expect (F²/2)^(1/3) times the Airy zeros, {airy}; got {E}."
assert abs(triangular(8.0, 1)[0] / triangular(1.0, 1)[0] - 4) < 1e-3, "Levels scale as F^(2/3): eight times the field, four times the energy."
```

::: right
Right. The levels scale as $F^{2/3}$, the law behind a MOSFET channel's subband energies at different gate fields.
:::
:::

::: check
given: g = 0.5 | 2 | 8
answer: 0.75*(0.75*g)^(1/3)
tolerance: 0.5%

## What does the Gaussian trial function give for the ground state of $\frac{p^2}{2} + \given{g}x^4$ (ħ = m = 1)?

::: right
Right. $\frac34\left(\frac{3g}{4}\right)^{1/3}$, 2% above the exact $0.668g^{1/3}$.
:::

::: misses
- [0.667986*g^(1/3)] That's the exact value; the Gaussian gives a bit more.
- [any] Minimize $\frac b2 + \frac{3g}{16b^2}$.
:::
:::

::: check
given: l = 0.02 | 0.03 | 0.05
answer: 0.5 + 0.75*l - 21/8*l^2
tolerance: 0.01%

## Through second order, what is the ground state of $\frac12(p^2 + x^2) + \given{l}x^4$?

::: right
Right. $\frac12 + \frac34\lambda - \frac{21}{8}\lambda^2$.
:::

::: misses
- [0.5 + 0.75*l] That's first order only.
- [any] Add the second-order term $-\frac{21}{8}\lambda^2$.
:::
:::

::: explain
## Why is the variational method most accurate for the ground state, while WKB is least accurate there?

::: model
The variational principle bounds the ground-state energy from above, and its error is second order in the error of the trial function: a reasonable smooth guess with no nodes, close to the true ground state's shape, gives a very good energy. WKB instead assumes the wave function oscillates many times over distances where the potential barely changes, so the wavelength is well defined at each point. The ground state has no oscillations at all and its wavelength is comparable to the whole well, so that assumption fails worst there. Higher states oscillate many times, and WKB's description becomes excellent just as finding good variational trial functions becomes hard.
:::
:::

::: practice

::: check
given: F = 0.5 | 2 | 10
answer: (F^2/2)^(1/3)*2.33811
tolerance: 0.5%

## Ground state of the triangular well $V = \given{F}x$ with a hard wall (ħ = m = 1)?

::: misses
- [any] $\left(\frac{F^2}{2}\right)^{1/3}a_1$ with $a_1$ = 2.33811.
:::
:::

::: check
given: n = 3 | 5 | 10
answer: ((n + 0.5)*pi/(2*sqrt(2)*0.874019))^(4/3)
tolerance: 0.5%

## WKB estimate of level {n} of $\frac{p^2}{2} + x^4$?

::: misses
- [any] $\left[\frac{(n + \frac12)\pi}{2\sqrt2\,I}\right]^{4/3}$ with I = 0.8740.
:::
:::
:::
