---
title: "Project: a field solver for a coax cable"
heading: Compute a cable's capacitance from Laplace's equation
eyebrow: A9 · Lesson 53 · Project
xp: 80
---

::: hook
A coaxial cable's datasheet lists its capacitance per meter: about 100 pF/m for the common RG-58. For a round coax there's a formula, but for a square trace inside a shield, a pair of wires over a ground plane, or a connector, there isn't, and engineers use field solvers: programs that solve Laplace's equation on a grid and then read off the capacitance. In this project you'll write one, test it against the coax formula, and see how the grid's coarseness limits its accuracy.
:::

::: predict
## A field solver approximates the round inner conductor by the grid squares inside it. Will it overestimate or underestimate the capacitance?

- [ ] Overestimate
- [x] Underestimate: the staircase conductor is slightly smaller than the real one
- [ ] Get it exactly
- [ ] It can't be predicted

::: reveal
Underestimate. Keeping only grid points inside the radius makes the inner conductor a little smaller than it should be, and a thinner inner conductor has less capacitance. On a coarse grid the error is a few percent; refining the grid shrinks it.
:::
:::

::: nameit
## The plan

1. **Set up the geometry.** On an $N \times N$ grid, mark the inner conductor (points with $r \le a$, held at 1 V) and the outer conductor (points with $r \ge b$, held at 0 V). Everything in between is free.
2. **Relax.** Repeatedly replace every free point with the average of its four neighbors until nothing changes (lesson 46).
3. **Measure the capacitance.** The stored energy per unit length is $W = \frac{\varepsilon_0}{2}\iint|\nabla V|^2dA$, and $W = \frac{1}{2}C'V^2$. With $V$ = 1 V,

$$C' = \varepsilon_0\iint|\nabla V|^2dA \approx \varepsilon_0\sum\left[(\Delta_xV)^2 + (\Delta_yV)^2\right]$$

where the sum runs over all neighboring pairs of grid points. In two dimensions the grid spacing cancels: $|\nabla V|^2 \approx (\Delta V/h)^2$ and each cell has area $h^2$.

## The exact answer for round coax

By Gauss's law, the field between the conductors is $E = \frac{\lambda}{2\pi\varepsilon r}$, which integrates to

$$V(r) = V_0\frac{\ln(b/r)}{\ln(b/a)} \qquad C' = \frac{2\pi\varepsilon_0\varepsilon_r}{\ln(b/a)}$$

Your solver should match the potential closely and the capacitance to within a few percent on a modest grid.
:::

::: example
## Estimate RG-58's capacitance: $a$ = 0.45 mm, $b$ = 1.47 mm, polyethylene with $\varepsilon_r$ = 2.25.

::: solution
1. **Capacitance per meter.**
   $$\begin{aligned} C' &= \frac{2\pi \times 8.854 \times 10^{-12} \times 2.25}{\ln(1.47/0.45)} \\ &= \frac{1.252 \times 10^{-10}}{1.184} \\ &= 106\ \text{pF/m} \end{aligned}$$
2. **Compare with datasheets.** Datasheets give about 100 pF/m; the real dielectric and conductor dimensions differ slightly from these round numbers.

**Answer:** about 106 pF/m.
:::
:::

::: example
## How do you know the relaxation has converged?

::: solution
1. **Run it longer.** See whether the answer changes.
2. **An example.** On an 81 × 81 grid with $a$ = 8 and $b$ = 24 cells, 5000 sweeps and 10 000 sweeps give the same capacitance to six digits.
3. **What's left.** What remains is discretization error from the staircase boundaries, which only a finer grid reduces.

**Answer:** it has converged when running more sweeps no longer changes the answer; the error that remains comes from the grid and needs a finer one.
:::
:::

::: check
kind: python

## Step 1. Write `coax(N, a, b, iters)`: an $N \times N$ grid centered on the middle, 1 V where $r \le a$ (in grid cells), 0 V where $r \ge b$, relaxed `iters` times by four-neighbor averaging on the free points only. Return the grid.

```python starter
import numpy as np

def coax(N, a, b, iters):
    c = (N - 1) / 2
    y, x = np.mgrid[0:N, 0:N]
    r = np.hypot(x - c, y - c)
    V = np.zeros((N, N))
    # mark inner (1 V) and outer (0 V); update only the free points each sweep
    return V

V = coax(81, 8, 24, 5000)
print(V[40, 56])
```

```python solution
import numpy as np

def coax(N, a, b, iters):
    c = (N - 1) / 2
    y, x = np.mgrid[0:N, 0:N]
    r = np.hypot(x - c, y - c)
    inner = r <= a
    outer = r >= b
    free = ~(inner | outer)
    V = np.where(inner, 1.0, 0.0)
    for _ in range(iters):
        avg = 0.25 * (np.roll(V, 1, 0) + np.roll(V, -1, 0) + np.roll(V, 1, 1) + np.roll(V, -1, 1))
        V = np.where(free, avg, V)
    return V

V = coax(81, 8, 24, 5000)
print(V[40, 56])
```

```python tests
import numpy as np
V = coax(81, 8, 24, 5000)
assert np.isclose(V[40, 40], 1) and np.isclose(V[0, 0], 0), "The inner conductor should be at 1 V and the outer region at 0 V."
for rr in (12, 16, 20):
    exact = np.log(24 / rr) / np.log(3)
    assert abs(V[40, 40 + rr] - exact) < 0.04, f"At r = {rr} the potential should be near {exact:.3f}; got {V[40, 40 + rr]:.3f}."
```

::: right
Right. Between the conductors the grid potential follows the logarithmic profile to within a couple of hundredths.
:::
:::

::: check
kind: python

## Step 2. Write `capacitance(V)` returning the capacitance per meter, $\varepsilon_0\sum[(\Delta_xV)^2 + (\Delta_yV)^2]$ over all neighboring pairs, for a 1 V solution. `coax` is included.

```python starter
import numpy as np
eps0 = 8.854e-12

def coax(N, a, b, iters):
    c = (N - 1) / 2
    y, x = np.mgrid[0:N, 0:N]
    r = np.hypot(x - c, y - c)
    inner = r <= a
    outer = r >= b
    free = ~(inner | outer)
    V = np.where(inner, 1.0, 0.0)
    for _ in range(iters):
        avg = 0.25 * (np.roll(V, 1, 0) + np.roll(V, -1, 0) + np.roll(V, 1, 1) + np.roll(V, -1, 1))
        V = np.where(free, avg, V)
    return V

def capacitance(V):
    return 0.0

print(capacitance(coax(81, 8, 24, 5000)) * 1e12, "pF/m")
```

```python solution
import numpy as np
eps0 = 8.854e-12

def coax(N, a, b, iters):
    c = (N - 1) / 2
    y, x = np.mgrid[0:N, 0:N]
    r = np.hypot(x - c, y - c)
    inner = r <= a
    outer = r >= b
    free = ~(inner | outer)
    V = np.where(inner, 1.0, 0.0)
    for _ in range(iters):
        avg = 0.25 * (np.roll(V, 1, 0) + np.roll(V, -1, 0) + np.roll(V, 1, 1) + np.roll(V, -1, 1))
        V = np.where(free, avg, V)
    return V

def capacitance(V):
    gx = np.diff(V, axis=1)
    gy = np.diff(V, axis=0)
    return eps0 * (np.sum(gx ** 2) + np.sum(gy ** 2))

print(capacitance(coax(81, 8, 24, 5000)) * 1e12, "pF/m")
```

```python tests
import numpy as np
exact = 2 * np.pi * 8.854e-12 / np.log(3)
C = capacitance(coax(81, 8, 24, 5000))
assert abs(C - exact) / exact < 0.08, f"Expected within 8% of {exact*1e12:.1f} pF/m; got {C*1e12:.1f}."
assert C < exact, "The staircase inner conductor is slightly too small, so the solver should come in a little low."
ones = np.ones((5, 5))
assert capacitance(ones) == 0, "A uniform potential has no field and stores no energy."
```

::: right
Right. About 48 pF/m against the exact 50.6: within 5%, and low, as the staircase predicts. A finer grid closes the gap.
:::
:::

::: check
given: a = 0.45 | 0.5; b = 1.47 | 1.75; er = 2.25 | 1.5
answer: 2*pi*8.854e-12*er/ln(b/a)*1e12
unit: pF/m
tolerance: 0.5%

## Step 3. A coax cable has inner radius {a} mm, outer radius {b} mm and dielectric $\varepsilon_r$ = {er}. What is its capacitance per meter, from the exact formula?

::: right
Right. $\frac{2\pi\varepsilon_0\varepsilon_r}{\ln(b/a)}$ = {answer}.
:::

::: misses
- [2*pi*8.854e-12/ln(b/a)*1e12] Include the dielectric's $\varepsilon_r$.
- [2*pi*8.854e-12*er/log(b/a)*1e12] Use the natural logarithm.
- [any] Not yet. Gauss's law with a cylinder, then integrate the field.
:::
:::

::: explain
## Why does the energy method give capacitance from the potential alone?

::: model
A capacitor at voltage $V$ stores energy $\frac{1}{2}CV^2$, and that energy lives in the electric field, with density $\frac{\varepsilon_0}{2}|\mathbf{E}|^2$. Since $\mathbf{E} = -\nabla V$, adding up $\frac{\varepsilon_0}{2}|\nabla V|^2$ over the space between the conductors gives the total energy, and dividing by $\frac{1}{2}V^2$ gives $C$. The relaxation supplies $V$ at every grid point, so the gradient, and therefore the capacitance, follow directly. It's also forgiving: small errors in $V$ change the energy only to second order.
:::
:::

::: practice

::: check
given: ratio = 2 | 2.3 | 3 | 3.5 | 4 | 5
answer: 2*pi*8.854e-12/ln(ratio)*1e12
unit: pF/m
tolerance: 0.5%

## An air coax has $b/a$ = {ratio}. What is its capacitance per meter?

::: misses
- [any] $\frac{2\pi\varepsilon_0}{\ln(b/a)}$.
:::
:::

::: check
given: a = 1 | 2; b = 4 | 6; r = 2.5 | 3
answer: ln(b/r)/ln(b/a)
tolerance: 0.5%

## A coax with $a$ = {a} mm and $b$ = {b} mm has its inner conductor at 1 V. What is the potential at $r$ = {r} mm?

::: misses
- [(b - r)/(b - a)] The potential falls logarithmically, not linearly.
- [any] $\frac{\ln(b/r)}{\ln(b/a)}$.
:::
:::

::: check
given: C = 100 | 50; V = 5 | 12
answer: 0.5*C*1e-12*V^2*1e9
unit: nJ
tolerance: 0.5%

## A cable has {C} pF/m and carries {V} V. How much energy is stored per meter, in nJ?

::: misses
- [any] $\frac{1}{2}C'V^2$.
:::
:::
:::
