---
title: Guide lab
heading: Waveguide mode lists, Bessel zeros and counting a cavity's modes, in Python
eyebrow: S5 · Lesson 23 · Guide lab
xp: 60
---

::: hook
Lessons 13 to 18 found cutoff frequencies one mode at a time. A designer needs the whole list: which modes propagate in a guide at a given frequency, where a circular guide's modes start (which needs the zeros of Bessel functions), and how many resonances a cavity has below some frequency, which sets how a shielded room or a microwave oven behaves. This lab computes all three, the Bessel zeros from the power series itself, and checks the cavity count against Weyl's formula.
:::

::: play
- The curve is Weyl's estimate of the number of electromagnetic modes below frequency f in a cavity of volume V, $N \approx \frac{8\pi}{3}V\frac{f^3}{c^3}$ (both polarizations), here for a 1 m × 0.8 m × 0.6 m box.
- The count grows as the cube of frequency: a room-sized cavity has thousands of modes below 3 GHz, which is why a reverberation chamber's field is nearly uniform statistically.
- The lab counts the exact modes and compares.
:::

```sim plot
view: graph
x: f = 0.2..6 GHz
y: N = 1..100000 log
plot: 8*pi/3*0.48*(f*1e9)^3/299792458^3 | label: Weyl estimate
```

::: predict
## How many modes does WR-90 (22.86 × 10.16 mm) carry at 15 GHz, and which?

- [ ] One, TE₁₀
- [ ] Two, TE₁₀ and TE₂₀
- [x] Three: TE₁₀, TE₂₀ and TE₀₁
- [ ] Five, including TE₁₁ and TM₁₁

::: reveal
Cutoffs are 6.56 GHz (TE₁₀), 13.11 (TE₂₀), 14.75 (TE₀₁) and 16.15 (TE₁₁ and TM₁₁ together). At 15 GHz the first three propagate; above 16.15 GHz there are five.
:::
:::

::: nameit
## What the lab computes

- **Rectangular guide modes:** for every (m, n) not both zero, TE$_{mn}$ cuts off at $\frac c2\sqrt{(m/a)^2 + (n/b)^2}$; TM$_{mn}$ exists only for m, n ≥ 1, at the same cutoff.
- **Bessel zeros:** $J_n(x) = \sum_k \frac{(-1)^k}{k!(k + n)!}\left(\frac x2\right)^{2k + n}$; TM$_{nm}$ modes use the zeros of $J_n$ and TE$_{nm}$ modes the zeros of $J_n'$, found by bisection.
- **Cavity modes:** a rectangular cavity has TE$_{mnl}$ (l ≥ 1, m and n not both 0) and TM$_{mnl}$ (m, n ≥ 1) modes at $\frac c2\sqrt{(m/a)^2 + (n/b)^2 + (l/d)^2}$. Weyl's law estimates the count below f as $\frac{8\pi}{3}V\frac{f^3}{c^3}$, more accurate the more modes there are.
:::

::: check
kind: python

## Write `rect_modes(a, b, fmax)`: a list of (name, cutoff) for every TE and TM mode of an a × b guide (meters) cutting off below fmax (Hz), sorted by cutoff.

```python starter
def rect_modes(a, b, fmax):
    return []
```

```python solution
import math

def rect_modes(a, b, fmax):
    c = 299792458
    out = []
    for m in range(0, 30):
        for n in range(0, 30):
            if m == 0 and n == 0:
                continue
            fc = c / 2 * math.sqrt((m / a) ** 2 + (n / b) ** 2)
            if fc < fmax:
                out.append(('TE%d%d' % (m, n), fc))
                if m >= 1 and n >= 1:
                    out.append(('TM%d%d' % (m, n), fc))
    return sorted(out, key=lambda t: (t[1], t[0]))
```

```python tests
m = rect_modes(22.86e-3, 10.16e-3, 15e9)
assert [n for n, _ in m] == ['TE10', 'TE20', 'TE01'], "The predict: three modes at 15 GHz."
assert abs(m[0][1] - 6.557e9) < 1e6, "TE10 at 6.56 GHz."
assert len(rect_modes(22.86e-3, 10.16e-3, 17e9)) == 5, "Above 16.15 GHz, TE11 and TM11 join."
```

::: right
Right. A mode chart like this is the first thing to compute for any guide, to see where it stops being single-mode.
:::
:::

::: check
kind: python

## Write `J(n, x)` from the power series (60 terms) and `bessel_zero(n, lo, hi, derivative=False)`: the zero of $J_n$ (or of $J_n'$, by a central difference with h = 10⁻⁶) between lo and hi, by bisection.

```python starter
def J(n, x):
    return 0.0

def bessel_zero(n, lo, hi, derivative=False):
    return lo
```

```python solution
import math

def J(n, x):
    s, t = 0.0, (x / 2) ** n / math.factorial(n)
    for k in range(60):
        s += t
        t *= -(x / 2) ** 2 / ((k + 1) * (k + 1 + n))
    return s

def bessel_zero(n, lo, hi, derivative=False):
    f = (lambda x: (J(n, x + 1e-6) - J(n, x - 1e-6)) / 2e-6) if derivative else (lambda x: J(n, x))
    for _ in range(100):
        m = (lo + hi) / 2
        if f(lo) * f(m) <= 0:
            hi = m
        else:
            lo = m
    return m
```

```python tests
assert abs(bessel_zero(1, 1, 2.5, True) - 1.8412) < 1e-4, "TE11: the first zero of J1', 1.8412."
assert abs(bessel_zero(0, 2, 3) - 2.4048) < 1e-4, "TM01: the first zero of J0, 2.4048."
assert abs(bessel_zero(0, 3, 4.5, True) - bessel_zero(1, 3, 4.5)) < 1e-6, "TE01 and TM11 share 3.8317, since J0' = −J1."
```

::: right
Right. The series converges for any x; at large x many terms cancel, which is why tables and asymptotic forms take over there.
:::
:::

::: check
kind: python

## Write `cavity_modes(a, b, d, F)`: count the TE and TM modes of an a × b × d box (meters) below F (Hz), and `weyl(a, b, d, F)`.

```python starter
def cavity_modes(a, b, d, F):
    return 0

def weyl(a, b, d, F):
    return 0.0
```

```python solution
import math

def cavity_modes(a, b, d, F):
    c = 299792458
    M, N, L = int(2 * F * a / c) + 1, int(2 * F * b / c) + 1, int(2 * F * d / c) + 1
    cnt = 0
    for m in range(M + 1):
        for n in range(N + 1):
            for l in range(L + 1):
                f = c / 2 * math.sqrt((m / a) ** 2 + (n / b) ** 2 + (l / d) ** 2)
                if f >= F:
                    continue
                if l >= 1 and not (m == 0 and n == 0):
                    cnt += 1
                if m >= 1 and n >= 1:
                    cnt += 1
    return cnt

def weyl(a, b, d, F):
    return 8 * math.pi / 3 * a * b * d * F ** 3 / 299792458 ** 3
```

```python tests
n3, w3 = cavity_modes(1, 0.8, 0.6, 3e9), weyl(1, 0.8, 0.6, 3e9)
assert n3 == 3996 and abs(n3 / w3 - 1) < 0.01, "Below 3 GHz: 3996 modes, within 1% of Weyl's 4030."
assert abs(cavity_modes(1, 0.8, 0.6, 6e9) / weyl(1, 0.8, 0.6, 6e9) - 1) < 0.002, "At 6 GHz the agreement tightens."
assert cavity_modes(0.02286, 0.01016, 0.0222, 10e9) == 1, "A WR-90 cavity 22.2 mm long has only TE101 (9.41 GHz) below 10 GHz."
```

::: right
Right. Weyl's law gets better as modes multiply: the exact count's surface corrections shrink relative to the volume term.
:::
:::

::: explain
## Why does a large metal room have thousands of resonances below a few gigahertz?

::: model
A cavity resonates wherever a whole number of half wavelengths fits along each of its three dimensions at once. Every combination of three such numbers gives a mode, so the number of modes below a frequency counts the lattice points inside an octant of a sphere whose radius grows with frequency; that grows as the cube of frequency and in proportion to the volume, which is Weyl's law. A room a meter or more across has dimensions many wavelengths long at gigahertz frequencies, so the count reaches thousands, closely spaced in frequency. With so many overlapping modes, stirring the field with a rotating paddle makes it statistically uniform, which reverberation chambers exploit for testing.
:::
:::
