---
title: Potential and capacitance lab
heading: Computing potentials and capacitance when there's no formula
eyebrow: B3 · Lesson 14 · Potential and capacitance lab
xp: 50
---

::: hook
Formulas exist for spheres, cylinders and infinite plates. A real electrode is a square pad, a trace, an odd-shaped housing, and its capacitance is an integral nobody can do by hand. Field solvers do it numerically, in two ways you'll write in this lab: **relaxation**, which makes every point of a grid the average of its neighbors until the potential settles, and the **method of moments**, which splits a conductor into small patches and solves for the charge on each. Along the way you'll add potentials, measure where a field's energy sits, and find the energy that disappears when two capacitors share charge.
:::

::: play
- A square box has its top held at 1 V and the other three sides at 0. The curve is the potential along a vertical line through the box, from the exact series solution.
- Move the line across the box: the potential always rises from 0 at the bottom to near 1 at the top, fastest near the middle of the box.
- The readout gives the potential halfway up. Through the center it's exactly 0.25, which you'll reproduce by relaxation in step 2.
:::

```sim plot
view: graph
x: y = 0..1
y: V = 0..1.05
slider: x = 0.05..0.95 step 0.05 = 0.5
plot: 4/pi*(sin(pi*x)*sinh(pi*y)/sinh(pi) + sin(3*pi*x)*sinh(3*pi*y)/(3*sinh(3*pi)) + sin(5*pi*x)*sinh(5*pi*y)/(5*sinh(5*pi)) + sin(7*pi*x)*sinh(7*pi*y)/(7*sinh(7*pi)) + sin(9*pi*x)*sinh(9*pi*y)/(9*sinh(9*pi)) + sin(11*pi*x)*sinh(11*pi*y)/(11*sinh(11*pi)) + sin(13*pi*x)*sinh(13*pi*y)/(13*sinh(13*pi))) | label: potential along the line | samples: 400
readout: potential halfway up = 4/pi*(sin(pi*x)*sinh(pi*0.5)/sinh(pi) + sin(3*pi*x)*sinh(3*pi*0.5)/(3*sinh(3*pi)) + sin(5*pi*x)*sinh(5*pi*0.5)/(5*sinh(5*pi)) + sin(7*pi*x)*sinh(7*pi*0.5)/(7*sinh(7*pi)) + sin(9*pi*x)*sinh(9*pi*0.5)/(9*sinh(9*pi))) | digits: 4
```

::: predict
## A square metal box has its lid held at 100 V and its other three sides at 0 V, insulated from the lid. What is the potential at the center?

- [ ] 50 V
- [ ] 33 V
- [x] 25 V
- [ ] 0 V

::: reveal
25 V, by superposition. Put four such boxes on top of each other, each with a different side at 100 V: adding their potentials gives a box with every side at 100 V, where the potential is 100 V everywhere. By symmetry each box contributes the same at the center, so each gives 25 V.
:::
:::

::: nameit
## Relaxation

In a charge-free region, the potential satisfies Laplace's equation, and on a grid that means each point is the **average of its four neighbors**. Start with any guess, hold the conductors at their voltages, and replace every interior point by its neighbors' average, over and over (**Jacobi iteration**). The error shrinks by a fixed factor each sweep, close to 1 for fine grids: for a grid 32 cells across, $\cos\frac{\pi}{32} = 0.9952$, so 477 sweeps per factor of ten.

## The method of moments

Split a conductor's surface into N small patches with unknown charges $Q_j$. The potential at patch i is $\sum_jP_{ij}Q_j$, where $P_{ij} = \frac{1}{4\pi\varepsilon_0r_{ij}}$ for distant patches and, for a square patch of side s acting on its own center, $P_{ii} = \frac{\ln(1 + \sqrt2)}{\pi\varepsilon_0s}$. Setting every patch to 1 V gives a linear system $P\mathbf Q = \mathbf 1$, and the capacitance is the total charge, $C = \sum Q_j$.

## Convergence and extrapolation

Charge crowds toward edges and corners, where uniform patches describe it poorly, so the error falls only about as $\frac1N$ with N patches along a side. If $C_N \approx C - \frac aN$, two solutions combine to cancel the leading error: $C \approx 2C_{2N} - C_N$ (**Richardson extrapolation**).

## Energy checks

The field of a charged sphere of radius R stores $\frac{kQ^2}{2R}$ in total, and the fraction within radius r is $1 - \frac Rr$: half of it lies within 2R. When a charged capacitor is joined to an uncharged one, charge is conserved but energy isn't: a fraction $\frac{C_2}{C_1 + C_2}$ of it is lost in the connection, whatever the resistance.
:::

::: example
## Using superposition, what is the potential at the center of a square box with its top at 100 V and its left side at 40 V, the other two sides grounded?

::: solution
1. **One side at a time.** Each side held at $V$ alone contributes $\frac V4$ at the center.
2. **Add the two sides.**
   $$\frac{100}{4} + \frac{40}{4} = 35\ \text{V}$$
   That's the same superposition that solves the predict question, applied with two different voltages.
3. **Test a solver.** A relaxation solver with both sides set gives 35 V at the center to within its convergence tolerance, which is a quick test for one you've just written.

**Answer:** the center sits at 35 V.
:::
:::

::: example
## A flat square plate 1 m on a side has a capacitance of about 40.8 pF. How does that compare with a disk and a sphere of the same surface area?

::: solution
1. **Disk.** A disk of area 1 m² has radius 0.564 m, and an isolated disk has
   $$\begin{aligned} C &= 8\varepsilon_0r \\ &= 40.0\ \text{pF} \end{aligned}$$
2. **Sphere.** A sphere of area 1 m² has radius 0.282 m, and
   $$\begin{aligned} C &= 4\pi\varepsilon_0r \\ &= 31.4\ \text{pF} \end{aligned}$$
3. **Flat shapes against the sphere.** The flat shapes hold about 30% more charge per volt than the sphere with the same area, because their charge can spread farther apart.
4. **Square against disk.** The square edges out the disk slightly by reaching its corners farther from the center.

**Answer:** the square's 40.8 pF is a little more than the disk's 40.0 pF and about 30% more than the sphere's 31.4 pF.
:::
:::

::: example
## A 10 µF capacitor at 12 V is connected to an uncharged 10 µF capacitor. What happens to the charge and the energy?

::: solution
1. **Charge is conserved.** 120 µC spreads over 20 µF, so both end at 6 V.
2. **Energy before.**
   $$\begin{aligned} W_1 &= \frac12 \times 10\ \mu\text{F} \times 144 \\ &= 0.72\ \text{mJ} \end{aligned}$$
3. **Energy after.**
   $$\begin{aligned} W_2 &= \frac12 \times 20\ \mu\text{F} \times 36 \\ &= 0.36\ \text{mJ} \end{aligned}$$
4. **Where it went.** Half the energy is gone, turned into heat in the wires (and a little radiation).
5. **The resistance doesn't matter.** The amount doesn't depend on the wires' resistance: less resistance makes a larger current for a shorter time, with the same loss.
6. **Switched-capacitor circuits.** They pay this loss on every transfer, which is why they move charge in small steps.

**Answer:** both capacitors end at 6 V, sharing the 120 µC, and the energy falls from 0.72 mJ to 0.36 mJ: half is lost as heat.
:::
:::

::: check
kind: python

## Step 1. Write `potential(q, src, pts)` that returns the potential at each point in `pts` (an M × 3 array) from point charges `q` (length N) at positions `src` (N × 3), using k = 8.988 × 10⁹.

```python starter
import numpy as np
k = 8.988e9

def potential(q, src, pts):
    return np.zeros(len(pts))

print(potential([1e-9], [[0, 0, 0]], [[1, 0, 0]]))
```

```python solution
import numpy as np
k = 8.988e9

def potential(q, src, pts):
    q = np.asarray(q, float)
    d = np.linalg.norm(np.asarray(pts, float)[:, None, :] - np.asarray(src, float)[None, :, :], axis=2)
    return (k * q[None, :] / d).sum(axis=1)

print(potential([1e-9], [[0, 0, 0]], [[1, 0, 0]]))
```

```python tests
import numpy as np
V = potential([1e-9], [[0, 0, 0]], [[1, 0, 0], [0, 2, 0]])
assert np.allclose(V, [8.988, 4.494]), f"1 nC gives 8.988 V at 1 m and 4.494 V at 2 m; got {V}."
D = potential([1e-9, -1e-9], [[-0.01, 0, 0], [0.01, 0, 0]], [[0, 0.05, 0], [0.05, 0, 0]])
assert abs(D[0]) < 1e-9, "On a dipole's bisecting plane the potential is zero."
assert abs(D[1] - (-74.9)) < 0.05, f"5 cm along the axis the dipole gives -74.9 V; got {D[1]:.3f}."
```

::: right
Right. Potentials add as plain numbers, which makes them far easier to sum than fields.
:::
:::

::: check
kind: python

## Step 2. Write `relax(n, V_top, sweeps)` that solves for the potential in a square box with n × n interior points (plus a boundary row on each side), the top row held at `V_top` and the other sides at 0, by Jacobi iteration with NumPy slices. Return the whole (n + 2) × (n + 2) array.

```python starter
import numpy as np

def relax(n, V_top, sweeps):
    p = np.zeros((n + 2, n + 2))
    p[0, :] = V_top
    return p

p = relax(31, 100.0, 4000)
print(p[16, 16])
```

```python solution
import numpy as np

def relax(n, V_top, sweeps):
    p = np.zeros((n + 2, n + 2))
    p[0, :] = V_top
    for _ in range(sweeps):
        p[1:-1, 1:-1] = 0.25 * (p[:-2, 1:-1] + p[2:, 1:-1] + p[1:-1, :-2] + p[1:-1, 2:])
    return p

p = relax(31, 100.0, 4000)
print(p[16, 16])
```

```python tests
import numpy as np
p = relax(31, 100.0, 4000)
assert abs(p[16, 16] - 25) < 0.01, f"The center of the box should settle at 25 V; got {p[16, 16]:.4f}."
assert np.all(p[1:-1, 1:-1] <= 100) and np.all(p[1:-1, 1:-1] >= 0), "Inside, the potential stays between the boundary values."
inner = p[1:-1, 1:-1]
avg = 0.25 * (p[:-2, 1:-1] + p[2:, 1:-1] + p[1:-1, :-2] + p[1:-1, 2:])
assert np.max(np.abs(inner - avg)) < 1e-3, "At convergence every point equals its neighbors' average."
```

::: right
Right. The center comes out at 25 V, the superposition answer, and no interior point exceeds the boundary values: averages can't make new maxima.
:::
:::

::: check
kind: python

## Step 3. Write `plate_C(a, N)` for a flat square plate of side a split into N × N patches: build the matrix P (point-charge terms off the diagonal, $\frac{\ln(1 + \sqrt2)}{\pi\varepsilon_0s}$ on it), solve $P\mathbf Q = \mathbf 1$, and return the capacitance. Then write `plate_C_extrapolated(a, N)` returning $2C_{2N} - C_N$. Use ε₀ = 8.854 × 10⁻¹².

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

def plate_C(a, N):
    return eps0 * a

def plate_C_extrapolated(a, N):
    return plate_C(a, N)

print(plate_C(1.0, 16), plate_C_extrapolated(1.0, 16))
```

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

def plate_C(a, N):
    s = a / N
    c = (np.arange(N) + 0.5) * s
    X, Y = np.meshgrid(c, c)
    x, y = X.ravel(), Y.ravel()
    r = np.sqrt((x[:, None] - x[None, :])**2 + (y[:, None] - y[None, :])**2)
    np.fill_diagonal(r, 1.0)
    P = 1 / (4 * np.pi * eps0 * r)
    np.fill_diagonal(P, math.log(1 + math.sqrt(2)) / (math.pi * eps0 * s))
    return float(np.linalg.solve(P, np.ones(N * N)).sum())

def plate_C_extrapolated(a, N):
    return 2 * plate_C(a, 2 * N) - plate_C(a, N)

print(plate_C(1.0, 16), plate_C_extrapolated(1.0, 16))
```

```python tests
C16 = plate_C(1.0, 16)
assert abs(C16 * 1e12 - 40.225) < 0.01, f"With 16 × 16 patches a 1 m plate gives 40.23 pF; got {C16 * 1e12:.3f}."
Ce = plate_C_extrapolated(1.0, 16)
assert abs(Ce * 1e12 - 40.81) < 0.08, f"Extrapolated, it should land within 0.2% of the accurate 40.81 pF; got {Ce * 1e12:.3f}."
assert abs(plate_C(0.1, 8) / plate_C(1.0, 8) - 0.1) < 1e-9, "Capacitance scales with size: a 10 cm plate has a tenth of it."
```

::: right
Right. The raw 32 × 32 answer is still 0.7% low because the edges' charge is poorly resolved, and the extrapolation removes most of that, landing within about 0.1% of the accurate 40.81 pF.
:::
:::

::: check
kind: python

## Step 4. Write `energy_within(Q, R, r_out, n)` that integrates the field energy density $\frac12\varepsilon_0E^2$ of a charged sphere (charge Q, radius R) over spherical shells from R out to `r_out`, using n steps evenly spaced in ln r with the midpoint rule.

```python starter
import numpy as np
import math
eps0 = 8.854e-12
k = 8.988e9

def energy_within(Q, R, r_out, n):
    return k * Q**2 / (2 * R)

print(energy_within(1e-9, 0.1, 0.2, 1000))
```

```python solution
import numpy as np
import math
eps0 = 8.854e-12
k = 8.988e9

def energy_within(Q, R, r_out, n):
    u = np.linspace(math.log(R), math.log(r_out), n + 1)
    um = (u[1:] + u[:-1]) / 2
    du = u[1] - u[0]
    r = np.exp(um)
    E = k * Q / r**2
    return float(np.sum(0.5 * eps0 * E**2 * 4 * np.pi * r**2 * r * du))

print(energy_within(1e-9, 0.1, 0.2, 1000))
```

```python tests
U = 8.988e9 * 1e-18 / 0.2
half = energy_within(1e-9, 0.1, 0.2, 1000)
assert abs(half / U - 0.5) < 1e-3, f"Half the energy lies within 2R; got a fraction {half / U:.5f}."
far = energy_within(1e-9, 0.1, 100, 10000)
assert abs(far / U - 0.999) < 1e-3, f"Out to 1000R the fraction is 1 - R/r = 0.999; got {far / U:.5f}."
```

::: right
Right. Stepping evenly in ln r puts as many points between R and 2R as between 100R and 200R, which suits a field that changes on every scale.
:::
:::

::: check
kind: python

## Step 5. Write `share(C1, V1, C2, V2)` for two capacitors joined plate to plate: return the common final voltage and the energy lost.

```python starter
def share(C1, V1, C2, V2):
    return (V1 + V2) / 2, 0.0

print(share(10e-6, 12, 10e-6, 0))
```

```python solution
def share(C1, V1, C2, V2):
    V = (C1 * V1 + C2 * V2) / (C1 + C2)
    before = 0.5 * C1 * V1**2 + 0.5 * C2 * V2**2
    after = 0.5 * (C1 + C2) * V**2
    return V, before - after

print(share(10e-6, 12, 10e-6, 0))
```

```python tests
V, L = share(10e-6, 12, 10e-6, 0)
assert abs(V - 6) < 1e-12 and abs(L - 0.36e-3) < 1e-12, f"Two 10 µF capacitors at 12 V and 0: 6 V and 0.36 mJ lost; got {V} and {L}."
V, L = share(4.7e-6, 5, 1e-6, -3)
assert abs(V - 3.5965) < 1e-4 and abs(L - 0.5 * 4.7e-6 * 1e-6 / 5.7e-6 * 64) < 1e-12, f"Expect 3.596 V and ½(C1C2/(C1 + C2))(V1 - V2)² lost; got {V} and {L}."
```

::: right
Right. The loss is always $\frac12\frac{C_1C_2}{C_1 + C_2}(V_1 - V_2)^2$, the same series-capacitance energy that appears in every charge-sharing circuit.
:::
:::

::: check
kind: parts
given: C1 = 1 | 10; C2 = 2 | 5; V = 5 | 12

## A {C1} µF capacitor at {V} V is joined to an uncharged {C2} µF capacitor.

::: part
answer: C1*V/(C1 + C2)
unit: V
tolerance: 0.5%

### What voltage do they share?

::: misses
- [V/2] Weight by capacitance: the charge spreads over $C_1 + C_2$.
- [any] Charge is conserved: $V = \frac{C_1V_0}{C_1 + C_2}$.
:::
:::

::: part
answer: C2/(C1 + C2)*100
unit: %
tolerance: 0.5%

### What percentage of the energy is lost?

::: misses
- [C1/(C1 + C2)*100] That's the fraction that remains.
- [any] $\frac{C_2}{C_1 + C_2}$ of it.
:::
:::
:::

::: check
given: n = 16 | 32 | 64
answer: ln(10)/(-ln(cos(pi/n)))
tolerance: 1%

## Jacobi iteration on a grid n = {n} cells across shrinks the error by about $\cos\frac{\pi}{n}$ per sweep. How many sweeps reduce it by a factor of ten?

::: right
Right. The count grows as $n^2$: doubling the resolution quadruples the sweeps, which is why serious solvers use faster methods such as multigrid.
:::

::: misses
- [ln(10)/(pi/n)] Use the factor per sweep, $\cos\frac{\pi}{n} \approx 1 - \frac{\pi^2}{2n^2}$.
- [any] Solve $\left(\cos\frac{\pi}{n}\right)^m = 0.1$.
:::
:::

::: explain
## Why does the energy lost when two capacitors share charge not depend on the resistance of the wire joining them?

::: model
The final state is fixed by charge conservation alone: the charge spreads until both capacitors are at the same voltage, and that common voltage, and so the final stored energy, doesn't involve the wire at all. The energy lost is the difference between the starting and final stored energies, so it's fixed too. The resistance only decides how the loss happens: a small resistance gives a large current for a short time, a large resistance a small current for a long time, and $\int I^2R\,dt$ comes out the same either way. With almost no resistance, the energy leaves as radiation and sparks instead of heat.
:::
:::

::: practice

::: check
given: V1 = 50 | 200; V2 = 0 | 100
answer: V1/4 + V2/4
unit: V
tolerance: 0.5%

## A square box has its top at {V1} V and its bottom at {V2} V, the sides grounded. What is the potential at its center?

::: misses
- [any] Each side contributes a quarter of its voltage.
:::
:::

::: check
given: r = 2 | 3 | 10
answer: (1 - 1/r)*100
unit: %
tolerance: 0.5%

## What percentage of a charged sphere's field energy lies within {r} times its radius?

::: misses
- [any] $1 - \frac Rr$ of it.
:::
:::
:::
