---
title: "Quantum lab: solving the Schrödinger equation numerically"
heading: Finite differences, shooting, and the levels of real wells
eyebrow: "B10 · Lesson 51 · Quantum lab"
xp: 60
---

::: hook
Only a handful of potentials have exact solutions. Device engineers solve the rest numerically, and two methods cover most of what they need. **Finite differences** turn the Schrödinger equation into a matrix eigenvalue problem: every level at once. **Shooting** integrates the equation from one wall at a trial energy and adjusts the energy until the solution lands on the other wall: one level at a time, very accurately. In this lab you'll write both, check them against the infinite well and the oscillator, use shooting on the 10 nm GaAs well of lesson 12, and solve the MOSFET triangular well that lessons 37 and 38 could only approximate.
:::

::: predict
## A finite-difference solver uses 1000 interior points across an infinite well. How accurate is its ground-state energy?

- [ ] Within 10%
- [ ] Within 1%
- [x] Within about one part in a million
- [ ] Exact

::: reveal
The second-difference formula is exact for $\sin(kx)$ up to a factor $\frac{\sin^2(kh/2)}{(kh/2)^2} \approx 1 - \frac{(kh)^2}{12}$. With $kh = \frac{\pi}{1001}$ that's an error of $8 \times 10^{-7}$. Higher levels, with more wiggles per grid point, are less accurate in proportion to $j^2$.
:::
:::

::: nameit
## What you'll write

1. `fd_levels(V, a, b, n=1000, k=4, m=1.0)`: the lowest k energies (eV) for the potential V(x) (x in nm, V in eV) on (a, b) with hard walls, effective mass m (in units of $m_e$), using n interior grid points. The Hamiltonian is the symmetric tridiagonal matrix with $2t + V(x_i)$ on the diagonal and −t beside it, where $t = \frac{c}{mh^2}$ and c = 0.0381 eV·nm². Use `np.linalg.eigvalsh`.
2. `numerov(V, E, a, b, n=4000, m=1.0)`: integrate $\psi'' = \frac{m(V - E)}{c}\psi$ from ψ(a) = 0, ψ(a + h) = h to x = b with Numerov's method, and return ψ(b). Then `shoot_level(V, a, b, lo, hi, m=1.0, n=4000)`: bisect on the sign of ψ(b) between energies lo and hi, which must bracket one level, 50 times.
3. `triangular_levels(F, m, k=2)`: the lowest k levels of the triangular well V = eFx (F in MV/cm, so eF = 0.1F eV/nm) with a hard wall at x = 0, using `fd_levels` on a domain long enough that the far wall doesn't matter.

## Numerov's method

For $\psi'' = f(x)\psi$, with $w_i = 1 - \frac{h^2}{12}f_i$:

$$\psi_{i+1} = \frac{(12 - 10w_i)\psi_i - w_{i-1}\psi_{i-1}}{w_{i+1}}$$

It's accurate to $h^4$ per unit length for smooth potentials. At a jump in V, put a grid point exactly on the jump and give V its average value there; otherwise the well's edges are effectively misplaced by up to a grid step.

## What to expect

- Infinite well: $E_j = \frac{0.376j^2}{L^2}$ eV.
- Oscillator $V = \frac{(\hbar\omega)^2}{4c}x^2$: $E_j = (j + \frac12)\hbar\omega$.
- 10 nm GaAs well with 0.3 eV walls: 34.21, 132.59 and 271.50 meV.
- Triangular well: $E_1 = 2.33811(c\,e^2F^2/m)^{1/3}$ and $E_2 = 4.08795(\ldots)^{1/3}$.
:::

::: example
## Write the oscillator potential for ħω = 50 meV with x in nm.

::: solution
1. **The potential.** With $c = \frac{\hbar^2}{2m}$, which is 0.0381 eV·nm²,
   $$\begin{aligned} V &= \frac12m\omega^2x^2 \\ &= \frac{(\hbar\omega)^2}{4c}x^2 \\ &= \frac{0.0025}{0.1524}x^2 \\ &= 0.0164x^2\ \text{eV} \end{aligned}$$
2. **Ground-state width.**
   $$\begin{aligned} \sigma &= \sqrt{c/\hbar\omega} \\ &= 0.87\ \text{nm} \end{aligned}$$
3. **Choosing the box.** A box from −8 to 8 nm puts the walls about nine widths away, where ψ is negligible for the first several levels.

**Answer:** $V = 0.0164x^2$ eV with x in nm.
:::
:::

::: example
## How do you know which level shooting found?

::: solution
1. **Count the zeros.** Count the zeros of ψ between the walls: level j has j − 1 of them.
2. **Watch the sign at the far wall.** As E increases through the levels, ψ(b) changes sign once per level, so between two consecutive levels it has one fixed sign.
3. **Bracket and bisect.** Bracketing with energies on either side of a single sign change, and bisecting, can't skip to the wrong level.

**Answer:** count the nodes of ψ (level j has j − 1), and bracket a single sign change of ψ(b) so bisection can't jump to another level.
:::
:::

::: check
kind: python

## Step 1. Write `fd_levels(V, a, b, n=1000, k=4, m=1.0)`.

```python starter
import numpy as np
C = 0.0381

def fd_levels(V, a, b, n=1000, k=4, m=1.0):
    x = np.linspace(a, b, n + 2)[1:-1]
    h = x[1] - x[0]
    t = C / m / h**2
    # build H: 2t + V(x) on the diagonal, -t next to it
    return np.zeros(k)

print(fd_levels(lambda x: 0 * x, 0, 2))
```

```python solution
import numpy as np
C = 0.0381

def fd_levels(V, a, b, n=1000, k=4, m=1.0):
    x = np.linspace(a, b, n + 2)[1:-1]
    h = x[1] - x[0]
    t = C / m / h**2
    H = np.diag(2 * t + V(x)) - t * np.eye(n, k=1) - t * np.eye(n, k=-1)
    return np.linalg.eigvalsh(H)[:k]

print(fd_levels(lambda x: 0 * x, 0, 2))
```

```python tests
import numpy as np
E = fd_levels(lambda x: 0 * x, 0, 2)
assert len(E) == 4, "Return the lowest k = 4 levels."
for j in range(1, 5):
    exact = C * np.pi**2 * j**2 / 4
    assert abs(E[j - 1] / exact - 1) < 2e-5, f"Infinite well, L = 2 nm: level {j} should be {exact:.6f} eV; got {E[j - 1]:.6f}."
Eg = fd_levels(lambda x: 0 * x, 0, 10, 1000, 2, 0.067)
assert abs(Eg[0] - C * np.pi**2 / (0.067 * 100)) < 1e-6, "Divide by the effective mass m in t."
hw = 0.05
E = fd_levels(lambda x: hw**2 / (4 * C) * x**2, -8, 8)
for j in range(4):
    assert abs(E[j] - (j + 0.5) * hw) < 2e-5, f"Oscillator level {j} should be {(j + 0.5) * hw:.4f} eV; got {E[j]:.6f}."
```

::: right
Right. A tridiagonal matrix and one eigenvalue call give every level of any one-dimensional potential, to parts per million.
:::
:::

::: check
kind: python

## Step 2. Write `numerov(V, E, a, b, n=4000, m=1.0)` and `shoot_level(V, a, b, lo, hi, m=1.0, n=4000)`.

```python starter
import numpy as np
C = 0.0381

def numerov(V, E, a, b, n=4000, m=1.0):
    x = np.linspace(a, b, n + 1)
    h = x[1] - x[0]
    f = (V(x) - E) * m / C
    psi = np.zeros(n + 1)
    psi[1] = h
    # step psi forward with Numerov's formula
    return psi[-1]

def shoot_level(V, a, b, lo, hi, m=1.0, n=4000):
    # bisect on the sign of numerov(V, E, ...) 50 times
    return 0.5 * (lo + hi)

print(shoot_level(lambda x: 0 * x, 0, 1, 0.3, 0.5))
```

```python solution
import numpy as np
C = 0.0381

def numerov(V, E, a, b, n=4000, m=1.0):
    x = np.linspace(a, b, n + 1)
    h = x[1] - x[0]
    f = (V(x) - E) * m / C
    psi = np.zeros(n + 1)
    psi[1] = h
    w = 1 - h * h * f / 12
    for i in range(1, n):
        psi[i + 1] = ((12 - 10 * w[i]) * psi[i] - w[i - 1] * psi[i - 1]) / w[i + 1]
    return psi[-1]

def shoot_level(V, a, b, lo, hi, m=1.0, n=4000):
    flo = numerov(V, lo, a, b, n, m)
    for _ in range(50):
        mid = 0.5 * (lo + hi)
        fm = numerov(V, mid, a, b, n, m)
        if flo * fm <= 0:
            hi = mid
        else:
            lo, flo = mid, fm
    return 0.5 * (lo + hi)

print(shoot_level(lambda x: 0 * x, 0, 1, 0.3, 0.5))
```

```python tests
import numpy as np
e = shoot_level(lambda x: 0 * x, 0, 1, 0.3, 0.5)
assert abs(e - C * np.pi**2) < 1e-6, f"The unit infinite well's ground state is {C * np.pi**2:.6f} eV; got {e:.6f}."

def gaas_well(x):
    # 10 nm well centered at 30 nm, 0.3 eV walls; the average value right on a jump
    d = np.abs(x - 30) - 5
    return np.where(np.abs(d) < 1e-9, 0.15, np.where(d < 0, 0.0, 0.3))

for lo, hi, want in ((0.01, 0.06, 0.0342086), (0.10, 0.16, 0.1325907), (0.24, 0.29, 0.2715050)):
    e = shoot_level(gaas_well, 0, 60, lo, hi, 0.067, 4800)  # h = 0.0125 nm puts both jumps on grid points
    assert abs(e - want) < 1e-5, f"The GaAs well has a level at {want * 1000:.2f} meV; got {e * 1000:.3f} meV."
```

::: right
Right. Shooting reproduces the finite well's three levels from lesson 12 to within a microelectron-volt, without solving the transcendental equation.
:::
:::

::: check
kind: python

## Step 3. Write `triangular_levels(F, m, k=2)` using `fd_levels`.

```python starter
import numpy as np
C = 0.0381

def fd_levels(V, a, b, n=1000, k=4, m=1.0):
    x = np.linspace(a, b, n + 2)[1:-1]
    h = x[1] - x[0]
    t = C / m / h**2
    H = np.diag(2 * t + V(x)) - t * np.eye(n, k=1) - t * np.eye(n, k=-1)
    return np.linalg.eigvalsh(H)[:k]

def triangular_levels(F, m, k=2):
    eF = 0.1 * F
    # choose a domain several turning points long, then call fd_levels
    return np.zeros(k)

print(triangular_levels(1, 0.916))
```

```python solution
import numpy as np
C = 0.0381

def fd_levels(V, a, b, n=1000, k=4, m=1.0):
    x = np.linspace(a, b, n + 2)[1:-1]
    h = x[1] - x[0]
    t = C / m / h**2
    H = np.diag(2 * t + V(x)) - t * np.eye(n, k=1) - t * np.eye(n, k=-1)
    return np.linalg.eigvalsh(H)[:k]

def triangular_levels(F, m, k=2):
    eF = 0.1 * F
    xmax = 12 * (C / m / eF) ** (1 / 3) + 6
    return fd_levels(lambda x: eF * x, 0, xmax, 3000, k, m)

print(triangular_levels(1, 0.916))
```

```python tests
import numpy as np
for F, m in ((1, 0.916), (0.5, 0.19), (2, 0.3)):
    base = (C / m * (0.1 * F) ** 2) ** (1 / 3)
    E = triangular_levels(F, m)
    assert len(E) == 2, "Return the lowest two levels."
    assert abs(E[0] / base - 2.33811) < 1e-3, f"F = {F}, m = {m}: E1 should be 2.33811 × {base:.5f} = {2.33811 * base:.5f} eV; got {E[0]:.5f}."
    assert abs(E[1] / base - 4.08795) < 2e-3, f"F = {F}, m = {m}: E2 should be 4.08795 × {base:.5f} = {4.08795 * base:.5f} eV; got {E[1]:.5f}."
```

::: right
Right. The numerical levels match the Airy-function values, which the variational estimate (2.476) and WKB (2.320) only approached.
:::
:::

::: check
given: j = 1 | 3 | 10; N = 200 | 1000
answer: (j*pi/(N + 1))^2/12
tolerance: 2%

## A finite-difference solver uses N = {N} interior points in an infinite well. By about what fraction does it underestimate level j = {j}?

::: right
Right. $\frac{(kh)^2}{12}$ with $kh = \frac{j\pi}{N + 1}$: {answer}.
:::

::: misses
- [(j*pi/(N + 1))^2] Include the $\frac{1}{12}$ from the Taylor series of $\sin^2$.
- [any] Not yet. The discrete second difference of $\sin kx$ is $-\frac{4}{h^2}\sin^2\frac{kh}{2}$ times it.
:::
:::

::: check
given: w = 10 | 50 | 200
answer: (w/1000)^2/(4*0.0381)
unit: eV/nm²
tolerance: 0.5%

## What coefficient, in eV/nm², gives an electron oscillator potential $V = Ax^2$ with ħω = {w} meV?

::: right
Right. $A = \frac{(\hbar\omega)^2}{4c}$ = {answer} eV/nm².
:::

::: misses
- [(w/1000)^2/(2*0.0381)] $\frac12m\omega^2 = \frac{(\hbar\omega)^2}{2\hbar^2/m} = \frac{(\hbar\omega)^2}{4c}$.
- [any] Not yet. c = 0.0381 eV·nm².
:::
:::

::: check
given: F = 0.5 | 1 | 2; m = 0.19 | 0.916
answer: 4.08795*(0.0381/m*(0.1*F)^2)^(1/3)
unit: eV
tolerance: 0.5%

## What's the second level of the triangular well at {F} MV/cm with $m^*$ = {m}$m_e$?

::: right
Right. $4.088(c\,e^2F^2/m)^{1/3}$ = {answer} eV.
:::

::: misses
- [2.33811*(0.0381/m*(0.1*F)^2)^(1/3)] That's the first level.
- [any] Not yet. The second zero of the Airy function is 4.088.
:::
:::

::: explain
## Why does a shooting solution at a wrong energy blow up past the far wall, and how does that help find the level?

::: model
In a region where the energy is below the potential, the equation's solutions grow or decay exponentially. At an allowed energy, the solution that started at the first wall decays into the far barrier, matching the boundary condition. At any other energy it contains a little of the growing solution, which takes over and drives ψ toward large positive or negative values. Which way it goes flips as the trial energy passes through a level, so the sign of ψ at the far end acts as a switch, and bisection on that sign homes in on the level as precisely as the arithmetic allows.
:::
:::

::: practice

::: check
given: j = 2 | 5; N = 400 | 2000
answer: (j*pi/(N + 1))^2/12
tolerance: 2%

## Fractional finite-difference error for level {j} with N = {N}?

::: misses
- [any] $\frac{1}{12}\left(\frac{j\pi}{N + 1}\right)^2$.
:::
:::

::: check
given: w = 10 | 20 | 50 | 80 | 100 | 120
answer: (w/1000)^2/(4*0.0381)
unit: eV/nm²
tolerance: 0.5%

## Oscillator coefficient for ħω = {w} meV, in eV/nm²?

::: misses
- [any] $\frac{(\hbar\omega)^2}{4c}$.
:::
:::

::: check
given: F = 0.8 | 1.5; m = 0.19 | 0.916
answer: 4.08795*(0.0381/m*(0.1*F)^2)^(1/3)
unit: eV
tolerance: 0.5%

## Second triangular-well level at {F} MV/cm, $m^*$ = {m}$m_e$?

::: misses
- [any] $4.088(c\,e^2F^2/m)^{1/3}$.
:::
:::
:::
