---
title: Optics lab
heading: Refraction and reflection, lens systems by matrices, coatings and fibers in code
eyebrow: B8 · Lesson 17 · Optics lab
xp: 50
---

::: hook
Optical design is calculation: how much a surface reflects at each angle, where a lens system focuses, what a stack of coatings does to each color, how many modes a fiber carries. Each has a compact method. In this lab you'll write Snell's law with total internal reflection, the Fresnel equations for both polarizations, ray-transfer matrices for lens systems, the transfer-matrix method for thin-film stacks, and a fiber's V-number, checking each against the results of lessons 7 to 16.
:::

::: play
- Light meets glass (n = 1.5) from air. The two curves are the reflectances for s and p polarization against the angle of incidence.
- At normal incidence both are 4%. The p curve dips to zero at Brewster's angle, 56.3°, and both climb to 100% at grazing incidence.
- Change the glass's index: Brewster's angle moves to $\tan^{-1}n$, and a denser material reflects more at every angle.
:::

```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) - sqrt(n^2 - sin(t*pi/180)^2))/(cos(t*pi/180) + sqrt(n^2 - sin(t*pi/180)^2)))^2 | label: s polarization | samples: 400
plot: ((n^2*cos(t*pi/180) - sqrt(n^2 - sin(t*pi/180)^2))/(n^2*cos(t*pi/180) + sqrt(n^2 - sin(t*pi/180)^2)))^2 | label: p polarization | color: ch2 | samples: 400
readout: Brewster's angle, degrees = atan(n)*180/pi | digits: 4
readout: reflectance at normal incidence = ((n - 1)/(n + 1))^2 | digits: 4
```

::: predict
## Two thin lenses, each with a 100 mm focal length, sit 50 mm apart. What is the focal length of the pair?

- [ ] 50 mm
- [x] About 67 mm
- [ ] 100 mm
- [ ] 200 mm

::: reveal
About 67 mm. In contact they'd make 50 mm, since powers add; separating them weakens the pair, to $\frac{f_1f_2}{f_1 + f_2 - d} = \frac{10\,000}{150} = 66.7$ mm. Ray-transfer matrices give this, and any other arrangement of lenses and gaps, by multiplying 2 × 2 matrices.
:::
:::

::: nameit
## Snell and Fresnel

At a boundary, $n_1\sin\theta_1 = n_2\sin\theta_2$; if $\frac{n_1}{n_2}\sin\theta_1 > 1$ there's no transmitted ray and everything reflects. The amplitude reflection coefficients are

$$r_s = \frac{n_1\cos\theta_1 - n_2\cos\theta_2}{n_1\cos\theta_1 + n_2\cos\theta_2}, \qquad r_p = \frac{n_2\cos\theta_1 - n_1\cos\theta_2}{n_2\cos\theta_1 + n_1\cos\theta_2},$$

and the reflectances are their squares.

## Ray-transfer matrices

A paraxial ray is a height y and an angle θ. A gap d multiplies it by $\begin{pmatrix} 1 & d \\ 0 & 1 \end{pmatrix}$, a thin lens by $\begin{pmatrix} 1 & 0 \\ -1/f & 1 \end{pmatrix}$. A whole system is the product of its elements' matrices, written right to left in the order the light meets them, and its effective focal length is $-\frac1C$ where C is the lower-left entry.

## Thin-film stacks

At normal incidence each layer of index n and thickness t has the characteristic matrix $\begin{pmatrix} \cos\delta & \frac{i}{n}\sin\delta \\ in\sin\delta & \cos\delta \end{pmatrix}$, $\delta = \frac{2\pi nt}{\lambda}$. Multiply the layers' matrices from the top down, apply the product to $\begin{pmatrix} 1 \\ n_s \end{pmatrix}$ to get $\begin{pmatrix} B \\ C \end{pmatrix}$, and the reflectance is $\left|\frac{n_0B - C}{n_0B + C}\right|^2$. This handles any number of layers at any thickness, where the quarter-wave formulas only cover special cases.

## Fiber modes

A step-index fiber of core radius a and numerical aperture NA has $V = \frac{2\pi a\,\text{NA}}{\lambda}$. It carries one mode when V < 2.405, and roughly $\frac{V^2}{2}$ modes when V is large. The single-mode cutoff wavelength is $\frac{2\pi a\,\text{NA}}{2.405}$.
:::

::: example
## Light inside glass (n = 1.5) meets the surface with air at 0.9 rad (51.6°). What happens, and what about at 0.7 rad?

::: solution
1. **Look for a transmitted ray.** Snell's law would need
   $$1.5\sin 51.6° = 1.17 > 1$$
   so there's no transmitted ray: total internal reflection, with reflectance 1 for both polarizations.
2. **Critical angle.**
   $$\begin{aligned} \theta_c &= \sin^{-1}\frac{1}{1.5} \\ &= 41.8° \end{aligned}$$
3. **At 0.7 rad (40.1°).** Just inside the critical angle, the ray escapes at 75.1°, and the reflectance is already high: 40% for s and 11% for p.
4. **Close to the critical angle.** The reflectance rises steeply to 100% over the last 1.7°.
5. **Binoculars.** Prisms in binoculars use total internal reflection because no metal mirror reflects as well.

**Answer:** at 0.9 rad the light is totally reflected. At 0.7 rad it escapes at 75.1°, with 40% of s and 11% of p reflected.
:::
:::

::: example
## What does a single quarter-wave layer of magnesium fluoride (1.38) do on glass (1.5) at 550 nm, and at 450 nm?

::: solution
1. **At 550 nm.** At its design wavelength the transfer matrix gives R = 1.41%, compared with 4.0% for bare glass.
2. **Check with the formula.** That matches the quarter-wave formula:
   $$\begin{aligned} R &= \left(\frac{n_s - n_1^2}{n_s + n_1^2}\right)^2 \\ &= \left(\frac{1.5 - 1.38^2}{1.5 + 1.38^2}\right)^2 \\ &= 0.0141 \end{aligned}$$
3. **At 450 nm.** The same layer is 0.31 of a wave thick rather than a quarter, and the reflectance climbs back to about 1.7%.
4. **Broadband coatings.** Broadband coatings stack several layers so that the dips at different wavelengths overlap, which is the kind of design the transfer matrix makes routine.

**Answer:** it reflects 1.41% at 550 nm and about 1.7% at 450 nm, against 4.0% for bare glass.
:::
:::

::: example
## Is standard single-mode fiber (a = 4.1 µm, NA = 0.12) single-mode at 1550 nm? And a multimode fiber with a = 25 µm and NA = 0.2 at 850 nm?

::: solution
1. **Single-mode fiber at 1550 nm.**
   $$\begin{aligned} V &= \frac{2\pi \times 4.1 \times 0.12}{1.55} \\ &= 1.99 \end{aligned}$$
   below 2.405, so yes.
2. **Its cutoff.**
   $$\begin{aligned} \lambda_c &= \frac{2\pi \times 4.1 \times 0.12}{2.405} \\ &= 1.29\ \mu\text{m} \end{aligned}$$
   so at 1310 nm it is only barely single-mode.
3. **Multimode fiber at 850 nm.**
   $$\begin{aligned} V &= \frac{2\pi \times 25 \times 0.2}{0.85} \\ &= 37.0 \end{aligned}$$
4. **Number of modes.** It carries about
   $$\frac{V^2}{2} = 683$$
   modes, each traveling at a slightly different speed: that modal dispersion limits it to a few hundred meters at high data rates.

**Answer:** the standard fiber is single-mode at 1550 nm (cutoff 1.29 µm). The multimode fiber has V = 37.0 and carries about 683 modes.
:::
:::

::: check
kind: python

## Step 1. Write `snell(n1, n2, th)` returning the refracted angle in radians, or `None` when there's total internal reflection, and `fresnel(n1, n2, th)` returning `(Rs, Rp)`, with both equal to 1 under total internal reflection.

```python starter
import math

def snell(n1, n2, th):
    return math.asin(n1 * math.sin(th) / n2)

def fresnel(n1, n2, th):
    R = ((n1 - n2) / (n1 + n2))**2
    return R, R

print(fresnel(1, 1.5, math.atan(1.5)))
```

```python solution
import math

def snell(n1, n2, th):
    s = n1 * math.sin(th) / n2
    return None if s > 1 else math.asin(s)

def fresnel(n1, n2, th):
    t = snell(n1, n2, th)
    if t is None:
        return 1.0, 1.0
    a, b = math.cos(th), math.cos(t)
    rs = (n1 * a - n2 * b) / (n1 * a + n2 * b)
    rp = (n2 * a - n1 * b) / (n2 * a + n1 * b)
    return rs * rs, rp * rp

print(fresnel(1, 1.5, math.atan(1.5)))
```

```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, "At normal incidence both are 4%."
Rs, Rp = fresnel(1, 1.5, math.atan(1.5))
assert abs(Rp) < 1e-12 and abs(Rs - 0.14793) < 1e-5, f"At Brewster's angle: Rp = 0 and Rs = 14.8%; got {Rs:.5f}, {Rp:.2e}."
assert snell(1.5, 1, 0.9) is None and fresnel(1.5, 1, 0.9) == (1.0, 1.0), "Past the critical angle everything reflects."
Rs, Rp = fresnel(1.5, 1, 0.7)
assert abs(Rs - 0.4014) < 1e-3 and abs(Rp - 0.1084) < 1e-3, f"Just inside the critical angle: Rs ≈ 0.40, Rp ≈ 0.11; got {Rs:.4f}, {Rp:.4f}."
```

::: right
Right. The p reflection vanishes at Brewster's angle and both rise steeply just before the critical angle, where the transmitted ray grazes the surface.
:::
:::

::: check
kind: python

## Step 2. Write `lens(f)` and `gap(d)` returning ray-transfer matrices, and `efl(M)` returning a system's effective focal length, $-\frac1C$. Use them to find the focal length of two 100 mm lenses 50 mm apart.

```python starter
import numpy as np

def lens(f):
    return np.eye(2)

def gap(d):
    return np.eye(2)

def efl(M):
    return float('inf')

print(efl(lens(100) @ gap(50) @ lens(100)))
```

```python solution
import numpy as np

def lens(f):
    return np.array([[1.0, 0.0], [-1.0 / f, 1.0]])

def gap(d):
    return np.array([[1.0, d], [0.0, 1.0]])

def efl(M):
    return -1.0 / M[1, 0]

print(efl(lens(100) @ gap(50) @ lens(100)))
```

```python tests
import numpy as np
assert abs(efl(lens(100) @ gap(50) @ lens(100)) - 200 / 3) < 1e-9, "Two 100 mm lenses 50 mm apart: 66.7 mm."
assert abs(efl(lens(100) @ gap(0) @ lens(100)) - 50) < 1e-9, "In contact, powers add: 50 mm."
assert abs(efl(lens(-50) @ gap(60) @ lens(100)) - 50 * 100 / (100 - 50 - 60) * -1) < 1e-9, "A telephoto pair of +100 and −50 mm, 60 mm apart, has a 500 mm focal length."
assert abs(np.linalg.det(lens(30) @ gap(12) @ lens(-20))) - 1 < 1e-12, "In the same medium, every system matrix has determinant 1."
```

::: right
Right. A positive lens followed by a negative one, as in a telephoto, can have a focal length far longer than the system is long.
:::
:::

::: check
kind: python

## Step 3. Write `multilayer_R(n0, layers, ns, lam)` returning the normal-incidence reflectance of a stack: `layers` is a list of `(n, t)` from the top down, `ns` the substrate's index, `lam` the wavelength, in the same length units as t.

```python starter
import numpy as np

def multilayer_R(n0, layers, ns, lam):
    return ((n0 - ns) / (n0 + ns))**2

print(multilayer_R(1, [(1.38, 550 / (4 * 1.38))], 1.5, 550))
```

```python solution
import numpy as np

def multilayer_R(n0, layers, ns, lam):
    M = np.eye(2, dtype=complex)
    for n, t in layers:
        d = 2 * np.pi * n * t / lam
        M = M @ np.array([[np.cos(d), 1j * np.sin(d) / n], [1j * n * np.sin(d), np.cos(d)]])
    B, C = M @ np.array([1, ns])
    r = (n0 * B - C) / (n0 * B + C)
    return float(abs(r)**2)

print(multilayer_R(1, [(1.38, 550 / (4 * 1.38))], 1.5, 550))
```

```python tests
assert abs(multilayer_R(1, [], 1.5, 550) - 0.04) < 1e-12, "With no layers, bare glass reflects 4%."
R = multilayer_R(1, [(1.38, 550 / (4 * 1.38))], 1.5, 550)
assert abs(R - ((1.5 - 1.38**2) / (1.5 + 1.38**2))**2) < 1e-12, f"One quarter-wave MgF2 layer: 1.41%; got {R:.5f}."
stack = [(2.3, 550 / (4 * 2.3)), (1.38, 550 / (4 * 1.38))] * 8
Y = (2.3 / 1.38)**16 * 1.5
assert abs(multilayer_R(1, stack, 1.5, 550) - ((1 - Y) / (1 + Y))**2) < 1e-9, "Eight pairs give lesson 8's mirror formula: 99.92%."
assert abs(multilayer_R(1, [(1.38, 550 / 2 / 1.38)], 1.5, 550) - 0.04) < 1e-12, "A half-wave layer is invisible at its design wavelength."
```

::: right
Right. A half-wave layer drops out entirely at its design wavelength, a quick test of any thin-film code.
:::
:::

::: check
kind: python

## Step 4. Write `fiber(a, NA, lam)` returning `(V, modes, cutoff)`: the V-number, the rough mode count $\frac{V^2}{2}$, and the single-mode cutoff wavelength.

```python starter
import math

def fiber(a, NA, lam):
    return 0.0, 1.0, lam

print(fiber(4.1e-6, 0.12, 1550e-9))
```

```python solution
import math

def fiber(a, NA, lam):
    V = 2 * math.pi * a * NA / lam
    return V, V * V / 2, 2 * math.pi * a * NA / 2.405

print(fiber(4.1e-6, 0.12, 1550e-9))
```

```python tests
V, m, c = fiber(4.1e-6, 0.12, 1550e-9)
assert abs(V - 1.9944) < 1e-3 and abs(c - 1.2854e-6) < 1e-9, f"Standard fiber: V = 1.99, cutoff 1285 nm; got {V:.4f}, {c * 1e9:.1f} nm."
V2, m2, c2 = fiber(25e-6, 0.2, 850e-9)
assert abs(V2 - 36.96) < 0.01 and abs(m2 - 683) < 1, f"The multimode fiber carries about 683 modes; got {m2:.1f}."
```

::: right
Right. Single-mode fiber sits just below the 2.405 limit at 1550 nm, which keeps its core as large as possible while still carrying one mode.
:::
:::

::: check
kind: python

## Step 5. A camera's two-element lens is a +50 mm lens followed 20 mm later by a −100 mm lens. Write `system()` that returns its matrix, and `back_focus(M)` that returns how far behind the last lens a parallel beam comes to a focus: the distance d where the product gap(d) @ M has a zero upper-left entry. `lens` and `gap` are included.

```python starter
import numpy as np

def lens(f):
    return np.array([[1.0, 0.0], [-1.0 / f, 1.0]])

def gap(d):
    return np.array([[1.0, d], [0.0, 1.0]])

def system():
    return lens(50)

def back_focus(M):
    return 50.0

print(back_focus(system()))
```

```python solution
import numpy as np

def lens(f):
    return np.array([[1.0, 0.0], [-1.0 / f, 1.0]])

def gap(d):
    return np.array([[1.0, d], [0.0, 1.0]])

def system():
    return lens(-100) @ gap(20) @ lens(50)

def back_focus(M):
    return -M[0, 0] / M[1, 0]

print(back_focus(system()))
```

```python tests
import numpy as np
M = system()
assert abs(-1 / M[1, 0] - 5000 / 70) < 1e-9, "The pair's focal length is 50 × (−100)/(50 − 100 − 20) = 71.4 mm."
bf = back_focus(M)
assert abs(bf - 300 / 7) < 1e-9, f"A parallel beam focuses 42.86 mm behind the second lens; got {bf:.4f}."
assert abs((gap(bf) @ M)[0, 0]) < 1e-12, "At the back focus, the output height doesn't depend on the input height."
```

::: right
Right. The pair has a 71.4 mm focal length but focuses only 42.9 mm behind its last lens: the negative element pushes the principal plane out in front, which is how a telephoto stays shorter than its focal length.
:::
:::

::: check
kind: parts
given: f1 = 50 | 100; f2 = 50 | 200; d = 10 | 40

## Lenses of {f1} mm and {f2} mm sit {d} mm apart.

::: part
answer: f1*f2/(f1 + f2 - d)
unit: mm
tolerance: 0.5%

### What is the pair's effective focal length?

::: misses
- [f1*f2/(f1 + f2)] That's for lenses in contact; the gap weakens the pair.
- [any] $\frac{f_1f_2}{f_1 + f_2 - d}$.
:::
:::

::: part
answer: f1*f2/(f1 + f2)
unit: mm
tolerance: 0.5%

### What would it be with the lenses in contact?

::: misses
- [any] Powers add: $\frac1f = \frac{1}{f_1} + \frac{1}{f_2}$.
:::
:::
:::

::: check
given: a = 2 | 4 | 25; NA = 0.1 | 0.14 | 0.22
answer: 2*pi*a*NA/2.405*1000
unit: nm
tolerance: 0.5%

## A step-index fiber has core radius {a} µm and NA = {NA}. Below what wavelength does it carry more than one mode?

::: right
Right. $\lambda_c = \frac{2\pi a\,\text{NA}}{2.405}$.
:::

::: misses
- [2*pi*a*NA*1000] Divide by 2.405, the first zero of the Bessel function $J_0$.
- [any] Set V = 2.405.
:::
:::

::: explain
## Why does a ray-transfer matrix system have to be multiplied in the reverse of the order the light meets the elements?

::: model
Each matrix takes a ray's height and angle just before an element and returns them just after it. The first element the light meets acts on the input ray first, so its matrix must sit next to the input vector, on the right; the next element then acts on that result, so its matrix goes to the left of the first, and so on. Writing $M = M_3M_2M_1$ means the ray passes element 1, then 2, then 3. Matrix multiplication doesn't commute, so swapping the order describes a different system, such as a lens and gap in the wrong sequence, and gives different answers.
:::
:::

::: practice

::: check
given: n = 1.33 | 1.5 | 2.4
answer: asin(1/n)*180/pi
unit: °
tolerance: 0.5%

## Critical angle from index {n} into air?

::: misses
- [any] $\sin\theta_c = \frac1n$.
:::
:::

::: check
given: ns = 1.5 | 1.7; n1 = 1.23 | 1.38
answer: ((ns - n1^2)/(ns + n1^2))^2*100
unit: %
tolerance: 0.5%

## Reflectance of a quarter-wave layer of index {n1} on index {ns}, at its design wavelength?

::: misses
- [any] $\left(\frac{n_s - n_1^2}{n_s + n_1^2}\right)^2$.
:::
:::
:::
