---
title: Wave lab
heading: Waves in lossy media, at boundaries and through coatings, in Python
eyebrow: C10 · Lesson 34 · Wave lab
xp: 50
---

::: hook
Lessons 27 to 31 covered how plane waves travel, carry power, take on polarizations, decay in conductors and reflect at boundaries. With complex numbers all of it reduces to a few short functions: one propagation constant that covers copper, seawater and glass alike; the Fresnel formulas for any angle, including total internal reflection; and a matrix method that computes the reflection of any stack of thin layers, the way anti-reflection coatings are designed.
:::

::: play
- The curves are the power reflected from air into a material of index n, against the angle of incidence, for the two polarizations: s (E parallel to the surface) and p (E in the plane of incidence).
- At normal incidence both reflect $((n - 1)/(n + 1))^2$: 4% for glass. As the angle grows, s reflects more and more; p falls to zero at Brewster's angle before rising.
- At grazing incidence both approach 100%, which is why a lake mirrors the far shore.
:::

```sim plot
view: graph
x: t = 0..89.9 °
y: R = 0..1
slider: n = 1.2..4 step 0.05 = 1.5
plot: ((cos(t*pi/180) - n*sqrt(1 - (sin(t*pi/180)/n)^2))/(cos(t*pi/180) + n*sqrt(1 - (sin(t*pi/180)/n)^2)))^2 | label: s polarization
plot: ((n*cos(t*pi/180) - sqrt(1 - (sin(t*pi/180)/n)^2))/(n*cos(t*pi/180) + sqrt(1 - (sin(t*pi/180)/n)^2)))^2 | label: p polarization
readout: Brewster's angle, ° = asin(n/sqrt(1 + n^2))*180/pi | digits: 4
readout: at normal incidence = ((n - 1)/(n + 1))^2 | digits: 3
```

::: predict
## A single quarter-wave layer of MgF₂ (n = 1.38) coats glass (n = 1.5). At the design wavelength, about how much light does the surface reflect?

- [ ] 4%
- [x] 1.4%
- [ ] 0.1%
- [ ] 0

::: reveal
1.4%, down from 4% for bare glass. A quarter-wave layer reflects $\left(\frac{n_0n_s - n_1^2}{n_0n_s + n_1^2}\right)^2$, which is zero only if $n_1 = \sqrt{n_0n_s} = 1.22$. No durable material has an index that low, so good coatings use several layers, which is exactly what the matrix method in this lab computes.
:::
:::

::: nameit
## One propagation constant for every medium

$$\gamma = \alpha + j\beta = \sqrt{j\omega\mu(\sigma + j\omega\varepsilon)}$$

covers everything from a lossless dielectric (σ = 0: α = 0, $\beta = \omega\sqrt{\mu\varepsilon}$) to a good conductor ($\alpha = \beta = 1/\delta$). Python's `cmath.sqrt` takes the principal root, which has the positive real part a decaying wave needs.

## Fresnel at any angle

With $\cos\theta_t = \sqrt{1 - (n_1\sin\theta_i/n_2)^2}$, computed as a complex number so it also works beyond the critical angle,

$$r_s = \frac{n_1\cos\theta_i - n_2\cos\theta_t}{n_1\cos\theta_i + n_2\cos\theta_t}, \qquad r_p = \frac{n_2\cos\theta_i - n_1\cos\theta_t}{n_2\cos\theta_i + n_1\cos\theta_t}$$

and the reflectances are $\lvert r\rvert^2$. Beyond the critical angle $\cos\theta_t$ is imaginary and both become 1.

## Thin-film stacks

At normal incidence each layer of index n and thickness d has a characteristic matrix

$$M = \begin{bmatrix}\cos\delta & j\sin\delta/n \\ jn\sin\delta & \cos\delta\end{bmatrix}, \qquad \delta = \frac{2\pi nd}{\lambda}$$

Multiply them in order, form $\begin{bmatrix}B \\ C\end{bmatrix} = M_\text{total}\begin{bmatrix}1 \\ n_s\end{bmatrix}$ with the substrate index $n_s$, and the reflection is $r = (n_0B - C)/(n_0B + C)$.

## Polarization loss

A linear antenna at angle ψ receiving $E_x = \cos\omega t$, $E_y = b\cos(\omega t + \delta)$ collects the fraction $\frac{(\cos\psi + b\sin\psi\cos\delta)^2 + (b\sin\psi\sin\delta)^2}{1 + b^2}$ of the power.
:::

::: example
## Find α and β for copper (σ = 5.8 × 10⁷ S/m) at 1 MHz, and for seawater (σ = 4 S/m, $\varepsilon_r$ = 81) at 1 kHz.

::: solution
1. **Copper at 1 MHz.**
   $$\begin{aligned} \alpha &= \beta \\ &= 1.51 \times 10^4\ \text{m}^{-1} \end{aligned}$$
   a skin depth of 66 µm.
2. **Is seawater a conductor at 1 kHz?**
   $$\frac{\sigma}{\omega\varepsilon} = 8.9 \times 10^5$$
   so it's still a good conductor.
3. **Seawater at 1 kHz.**
   $$\begin{aligned} \alpha &= \beta \\ &= 0.126\ \text{m}^{-1} \end{aligned}$$
   a skin depth of 7.96 m, which is why submarines are reached at very low frequencies.

**Answer:** copper has α = β = $1.51 \times 10^4$ m⁻¹ at 1 MHz, and seawater has α = β = 0.126 m⁻¹ at 1 kHz.
:::
:::

::: example
## What does glass (n = 1.5) reflect at 45° for each polarization?

::: solution
1. **Angle inside the glass.**
   $$\begin{aligned} \cos\theta_t &= \sqrt{1 - (0.707/1.5)^2} \\ &= 0.882 \end{aligned}$$
2. **Perpendicular (s) polarization.**
   $$\begin{aligned} R_s &= \left(\frac{0.707 - 1.5 \times 0.882}{0.707 + 1.323}\right)^2 \\ &= 0.092 \end{aligned}$$
3. **Parallel (p) polarization.**
   $$\begin{aligned} R_p &= \left(\frac{1.5 \times 0.707 - 0.882}{1.061 + 0.882}\right)^2 \\ &= 0.0085 \end{aligned}$$
4. **What it means.** Reflected light is already strongly s-polarized at 45°, which is what polarizing sunglasses exploit.

**Answer:** glass reflects 9.2% of s-polarized light and 0.85% of p-polarized light at 45°.
:::
:::

::: check
given: n = 1.33 | 1.5 | 2.4 | 3.5
answer: ((n - 1)/(n + 1))^2*100
unit: %
tolerance: 0.5%

## What percentage of light does an air–material surface reflect at normal incidence, for n = {n}?

::: right
Right: {answer}%.
:::

::: misses
- [any] Not yet. $((n - 1)/(n + 1))^2$.
:::
:::

::: check
kind: python

## Write `propagation(f, er, sigma)`: return `(alpha, beta)` for a plane wave at frequency f in a medium with relative permittivity er and conductivity sigma (μ = μ₀).

```python starter
import math

def propagation(f, er, sigma):
    return (0.0, 2 * math.pi * f / 3e8)
```

```python solution
import cmath, math

def propagation(f, er, sigma):
    w = 2 * math.pi * f
    g = cmath.sqrt(1j * w * 4e-7 * math.pi * (sigma + 1j * w * er * 8.854187817e-12))
    return (g.real, g.imag)
```

```python tests
import math
a, b = propagation(1e6, 1, 5.8e7)
assert abs(1 / a - 66.085e-6) < 1e-8 and abs(a - b) / a < 1e-9, f"Copper at 1 MHz: α = β, skin depth 66.1 µm; got α = {a:.4g}, β = {b:.4g}."
a, b = propagation(1e3, 81, 4)
assert abs(1 / a - 7.9578) < 1e-3, f"Seawater at 1 kHz: skin depth 7.96 m, got {1 / a:.4f}."
a, b = propagation(1e9, 4, 0)
assert a == 0 and abs(b - 41.917) < 1e-3, f"A lossless dielectric with εr = 4 at 1 GHz: α = 0, β = 41.92 rad/m; got {a}, {b:.4f}."
```

::: right
Right. One complex square root covers conductors, dielectrics and everything between.
:::
:::

::: check
kind: python

## Write `fresnel(n1, n2, theta)`: the power reflectances `(Rs, Rp)` for light going from index n1 into n2 at `theta` degrees. Use a complex square root so total internal reflection comes out as 1.

```python starter
def fresnel(n1, n2, theta):
    r = ((n1 - n2) / (n1 + n2)) ** 2
    return (r, r)
```

```python solution
import cmath, math

def fresnel(n1, n2, theta):
    t = math.radians(theta)
    ci = math.cos(t)
    ct = cmath.sqrt(1 - (n1 / n2 * math.sin(t)) ** 2)
    rs = (n1 * ci - n2 * ct) / (n1 * ci + n2 * ct)
    rp = (n2 * ci - n1 * ct) / (n2 * ci + n1 * ct)
    return (abs(rs) ** 2, abs(rp) ** 2)
```

```python tests
import math
Rs, Rp = fresnel(1, 1.5, 0)
assert abs(Rs - 0.04) < 1e-12 and abs(Rp - 0.04) < 1e-12, "Normal incidence on glass: 4% for both."
Rs, Rp = fresnel(1, 1.5, 45)
assert abs(Rs - 0.092013) < 1e-6 and abs(Rp - 0.008466) < 1e-6, f"Glass at 45°: 0.0920 and 0.0085; got {Rs:.6f}, {Rp:.6f}."
assert fresnel(1, 1.5, math.degrees(math.atan(1.5)))[1] < 1e-20, "At Brewster's angle the p reflection vanishes."
Rs, Rp = fresnel(1.5, 1, 60)
assert abs(Rs - 1) < 1e-12 and abs(Rp - 1) < 1e-12, "Inside glass at 60°, past the 41.8° critical angle: total reflection."
```

::: right
Right. The complex root takes care of total internal reflection without a special case.
:::
:::

::: check
kind: python

## Write `multilayer(ns, ds, lam)`: the normal-incidence reflectance of a stack, where `ns` lists the indices from the incident medium to the substrate and `ds` the thicknesses of the layers in between.

```python starter
def multilayer(ns, ds, lam):
    n0, nsub = ns[0], ns[-1]
    return ((n0 - nsub) / (n0 + nsub)) ** 2
```

```python solution
import math

def multilayer(ns, ds, lam):
    M = [[1, 0], [0, 1]]
    for n, d in zip(ns[1:-1], ds):
        dl = 2 * math.pi * n * d / lam
        L = [[math.cos(dl), 1j * math.sin(dl) / n], [1j * n * math.sin(dl), math.cos(dl)]]
        M = [[M[0][0] * L[0][0] + M[0][1] * L[1][0], M[0][0] * L[0][1] + M[0][1] * L[1][1]],
             [M[1][0] * L[0][0] + M[1][1] * L[1][0], M[1][0] * L[0][1] + M[1][1] * L[1][1]]]
    n0, nsub = ns[0], ns[-1]
    B = M[0][0] + M[0][1] * nsub
    C = M[1][0] + M[1][1] * nsub
    r = (n0 * B - C) / (n0 * B + C)
    return abs(r) ** 2
```

```python tests
import math
assert abs(multilayer([1, 1.5], [], 550e-9) - 0.04) < 1e-12, "Bare glass reflects 4%."
R = multilayer([1, 1.38, 1.5], [550e-9 / 4 / 1.38], 550e-9)
assert abs(R - 0.014110) < 1e-6, f"Quarter-wave MgF2 on glass: 1.41%, got {R:.6f}."
assert multilayer([1, math.sqrt(1.5), 1.5], [550e-9 / 4 / math.sqrt(1.5)], 550e-9) < 1e-20, "A layer of index √1.5 makes a perfect coating."
assert abs(multilayer([1, 1.38, 1.5], [550e-9 / 4 / 1.38], 450e-9) - 0.017211) < 1e-6, "Away from its design wavelength the coating works less well: 1.72% at 450 nm."
```

::: right
Right. Two-by-two matrices, one per layer, describe any stack.
:::
:::

::: check
kind: python

## Write `plf(b, delta, psi)`: the fraction of power a linear antenna at `psi` degrees collects from a wave $E_x = \cos\omega t$, $E_y = b\cos(\omega t + \delta)$, with `delta` in degrees.

```python starter
def plf(b, delta, psi):
    return 1.0
```

```python solution
import math

def plf(b, delta, psi):
    d, p = math.radians(delta), math.radians(psi)
    return ((math.cos(p) + b * math.sin(p) * math.cos(d)) ** 2 + (b * math.sin(p) * math.sin(d)) ** 2) / (1 + b * b)
```

```python tests
assert abs(plf(0, 0, 30) - 0.75) < 1e-12, "A linear wave 30° off: cos² 30° = 0.75."
assert abs(plf(1, 90, 37) - 0.5) < 1e-12, "A circular wave gives half the power at any angle."
assert abs(plf(1, 0, 45) - 1) < 1e-12, "Equal in-phase components make a 45° linear wave, matched at 45°."
assert abs(plf(0.5, 90, 0) - 0.8) < 1e-12, "An elliptical wave along its major axis: 1/(1 + b²) = 0.8."
```

::: right
Right. The same formula covers linear, circular and elliptical waves.
:::
:::

::: explain
## Why does a quarter-wave coating cancel reflection, and why only at one wavelength?

::: model
Light reflects twice: at the air-to-coating surface and at the coating-to-glass surface. A layer a quarter wavelength thick makes the second reflection travel an extra half wavelength there and back, so it returns half a cycle out of step with the first. If the two reflections are equally strong, which happens when the coating's index is the geometric mean of the two on either side, they cancel completely. At other wavelengths the layer is no longer a quarter wave thick, the phase difference isn't half a cycle, and the cancellation is partial, which is why coated lenses look faintly purple: red and blue, at the ends of the spectrum, reflect a little more than green.
:::
:::
