---
title: Matching lab
heading: L-section solutions for any load, exact multisection responses and stub-match bandwidths, in Python
eyebrow: S6 · Lesson 14 · Matching lab
xp: 60
---

::: hook
Lessons 8 to 12 designed matching networks with formulas, several of them approximate: the small-reflection theory behind multisection transformers, and the habit of judging a stub match only at its design frequency. This lab writes a general L-section solver and proves each solution by computing the input impedance it produces, evaluates a binomial transformer exactly by cascading its sections' ABCD matrices, and sweeps both single-stub solutions across frequency to see which one is broader and by how much.
:::

::: play
- The solid curve is the small-reflection estimate for a three-section binomial transformer from 100 Ω to 50 Ω, $|\Gamma| = \frac13|\cos\theta|^3$ with $\theta = \frac{\pi}{2}\frac{f}{f_0}$; the dashed one is a single quarter-wave section's exact response.
- The binomial's flat bottom is the maximally flat response. The lab computes its exact response by cascading ABCD matrices: the 0.05 bandwidth is 69.7%, against the estimate's 71%.
- Both repeat every $2f_0$, since each section is a quarter wave at $f_0$.
:::

```sim plot
view: graph
x: r = 0..2
y: G = 0..0.4
plot: abs(cos(pi/2*r)^3)/3 | label: three-section binomial (estimate)
plot: 1/sqrt(1 + 8/cos(pi/2*r)^2) | dashed | label: single section (exact)
plot: 0.05 + 0*r | dashed | label: Γm = 0.05
```

::: predict
## Pozar's 60 − j80 Ω load is matched at 2 GHz with a short-circuited stub, either the solution nearer the load (d = 0.110λ, stub 0.095λ) or the farther one (d = 0.259λ, stub 0.405λ). How do their bandwidths for |Γ| ≤ 0.2 compare?

- [ ] They're identical
- [x] The nearer, shorter solution covers about 20%; the farther one about 6%
- [ ] The farther one is broader, because its stub is longer
- [ ] Both cover about 50%

::: reveal
Sweeping both with the load modeled as 60 Ω in series with the 0.995 pF that gives −j80 Ω at 2 GHz, the first stays below 0.2 from 0.918 to 1.115 $f_0$ (19.6%) and the second only from 0.967 to 1.027 $f_0$ (6.0%). Longer lines change their electrical length faster with frequency, so short solutions are broader.
:::
:::

::: nameit
## The L-section solutions (Pozar)

For a load $R_L + jX_L$ and line $Z_0$:

- If $R_L > Z_0$, put the shunt element next to the load: $B = \frac{X_L \pm \sqrt{R_L/Z_0}\sqrt{R_L^2 + X_L^2 - Z_0R_L}}{R_L^2 + X_L^2}$ and $X = \frac{1}{B} + \frac{X_LZ_0}{R_L} - \frac{Z_0}{BR_L}$.
- If $R_L < Z_0$, put the series element next to the load: $X = \pm\sqrt{R_L(Z_0 - R_L)} - X_L$ and $B = \pm\frac{\sqrt{(Z_0 - R_L)/R_L}}{Z_0}$.

A solution is proven by computing the input impedance: shunt-first gives $1/(1/Z_L + jB) + jX$; series-first gives $1/(1/(Z_L + jX) + jB)$.

## Exact cascades

Each quarter-wave section at frequency ratio $r = f/f_0$ has electrical length $\theta = \frac{\pi}{2}r$ and ABCD matrix [[cos θ, jZ sin θ], [j sin θ/Z, cos θ]]; multiplying them and loading with $Z_L$ gives the exact input impedance, with every multiple reflection included.

## Sweeping a stub match

With the load modeled by its actual elements, the line and stub lengths scale with frequency: $\beta d = 2\pi(d/\lambda_0)r$. The input admittance is the transformed load admittance plus the stub's $-jY_0\cot\beta\ell$.
:::

::: check
kind: python

## Write `lmatch(ZL, Z0=50)`: both L-section solutions as a list of (kind, X, B) with kind 'shunt-first' or 'series-first', and `zin(kind, X, B, ZL)`: the input impedance each gives.

```python starter
def lmatch(ZL, Z0=50.0):
    return []

def zin(kind, X, B, ZL):
    return ZL
```

```python solution
import math

def lmatch(ZL, Z0=50.0):
    RL, XL = ZL.real, ZL.imag
    out = []
    for s in (1, -1):
        if RL > Z0:
            B = (XL + s * math.sqrt(RL / Z0) * math.sqrt(RL * RL + XL * XL - Z0 * RL)) / (RL * RL + XL * XL)
            X = 1 / B + XL * Z0 / RL - Z0 / (B * RL)
            out.append(('shunt-first', X, B))
        else:
            X = s * math.sqrt(RL * (Z0 - RL)) - XL
            B = s * math.sqrt((Z0 - RL) / RL) / Z0
            out.append(('series-first', X, B))
    return out

def zin(kind, X, B, ZL):
    if kind == 'shunt-first':
        return 1 / (1 / ZL + 1j * B) + 1j * X
    return 1 / (1 / (ZL + 1j * X) + 1j * B)
```

```python tests
for ZL in (200 - 100j, 25 - 15j, 10 + 5j, 100 + 80j):
    sols = lmatch(ZL)
    assert len(sols) == 2 and all(abs(zin(k, X, B, ZL) - 50) < 1e-9 for k, X, B in sols), "Every solution must give exactly 50 Ω."
k, X, B = lmatch(200 - 100j)[0]
assert k == 'shunt-first' and abs(X - 100) < 1e-9 and abs(B - 0.006) < 1e-12, "200 − j100 Ω: B = 6 mS, X = 100 Ω."
```

::: right
Right. Checking every solution by computing what it actually presents catches sign slips that the formulas invite.
:::
:::

::: check
kind: python

## Write `binomial(Z0, ZL, N)`: the N section impedances from $\ln\frac{Z_{n+1}}{Z_n} = 2^{-N}C_n^N\ln\frac{Z_L}{Z_0}$, and `gamma(zs, Z0, ZL, r)`: the exact |Γ| at frequency ratio r by cascading ABCD matrices (first section next to $Z_0$).

```python starter
def binomial(Z0, ZL, N):
    return [Z0] * N

def gamma(zs, Z0, ZL, r):
    return 0.0
```

```python solution
import math

def binomial(Z0, ZL, N):
    zs, z = [], Z0
    for n in range(N):
        z = z * math.exp(2 ** -N * math.comb(N, n) * math.log(ZL / Z0))
        zs.append(z)
    return zs

def gamma(zs, Z0, ZL, r):
    th = math.pi / 2 * r
    A, B, C, D = 1, 0, 0, 1
    for z in zs:
        a, b, c, d = math.cos(th), 1j * z * math.sin(th), 1j * math.sin(th) / z, math.cos(th)
        A, B, C, D = A * a + B * c, A * b + B * d, C * a + D * c, C * b + D * d
    Zi = (A * ZL + B) / (C * ZL + D)
    return abs((Zi - Z0) / (Zi + Z0))
```

```python tests
zs = binomial(50, 100, 3)
assert all(abs(a - b) < 0.01 for a, b in zip(zs, [54.53, 70.71, 91.70])), "50 to 100 Ω, three sections: 54.5, 70.7, 91.7 Ω."
assert gamma(zs, 50, 100, 1) < 1e-9, "Matched at the design frequency."
lo, hi = 0.3, 1.0
for _ in range(100):
    m = (lo + hi) / 2
    if gamma(zs, 50, 100, m) > 0.05:
        lo = m
    else:
        hi = m
assert abs(2 * (1 - hi) - 0.697) < 0.002, "The exact 0.05 bandwidth is 69.7%, a little under the 71% estimate."
```

::: right
Right. The small-reflection estimate ignores multiple reflections; for a 2:1 match the error is under two points of bandwidth.
:::
:::

::: check
kind: python

## Write `stub_gamma(d, l, r)`: the |Γ| of Pozar's short-stub match at frequency ratio r, with the load 60 Ω in series with the capacitance giving −j80 Ω at 2 GHz, a stub of l wavelengths at a distance d wavelengths (both at 2 GHz) on 50 Ω line; and `bandwidth(d, l, g=0.2)`: the fractional band around r = 1 where |Γ| ≤ g, stepping r by 0.0001.

```python starter
def stub_gamma(d, l, r):
    return 0.0

def bandwidth(d, l, g=0.2):
    return 0.0
```

```python solution
import math

def stub_gamma(d, l, r, Z0=50.0, f0=2e9):
    C = 1 / (2 * math.pi * f0 * 80)
    ZL = 60 + 1 / (1j * 2 * math.pi * f0 * r * C)
    t = math.tan(2 * math.pi * d * r)
    Zd = Z0 * (ZL + 1j * Z0 * t) / (Z0 + 1j * ZL * t)
    Ys = 1 / (1j * Z0 * math.tan(2 * math.pi * l * r))
    Z = 1 / (1 / Zd + Ys)
    return abs((Z - Z0) / (Z + Z0))

def bandwidth(d, l, g=0.2):
    lo = hi = 1.0
    while stub_gamma(d, l, lo - 1e-4) <= g:
        lo -= 1e-4
    while stub_gamma(d, l, hi + 1e-4) <= g:
        hi += 1e-4
    return hi - lo
```

```python tests
d1, l1, d2, l2 = 0.110423, 0.094975, 0.259445, 0.405025
assert stub_gamma(d1, l1, 1) < 1e-4 and stub_gamma(d2, l2, 1) < 1e-4, "Both are matched at 2 GHz."
assert abs(bandwidth(d1, l1) - 0.196) < 0.002, "Solution 1: about 19.6%."
assert abs(bandwidth(d2, l2) - 0.060) < 0.002, "Solution 2: about 6.0%."
```

::: right
Right. The farther solution has more line, so its phases swing faster with frequency; the nearer one is three times broader.
:::
:::

::: explain
## Why is a matching network made with shorter lines usually broader in bandwidth?

::: model
A transmission line's electrical length is proportional to frequency, so the phase it adds changes by an amount proportional to its length as the frequency moves away from the design value. A matching network works by arranging that the reflections from its parts cancel at the design frequency; with longer lines, those cancellation phases drift faster as the frequency changes, so the match breaks down sooner. The shorter solution of a stub match or a network with fewer and shorter sections keeps its reflections nearly cancelled over a wider range, which is why designers prefer the solution with the least total line.
:::
:::
