---
title: Gradient, divergence and curl in curvilinear coordinates
heading: Derivatives that fit wires, cables and charges
eyebrow: A9 · Lesson 38 · Curvilinear coordinates
xp: 40
---

::: hook
The fields engineers meet most often have round symmetry: the field around a wire or inside a coaxial cable, the field of a charge or a charged sphere. In Cartesian coordinates their formulas are tangles of $x$, $y$ and $z$; in cylindrical or spherical coordinates they depend on a single variable, $r$. Taking their divergence, curl or Laplacian there needs the right formulas, which carry extra factors of $r$ because the coordinate grid itself spreads out as $r$ grows.
:::

::: play
- The curve is the field of a long rod of uniform charge density and radius $a$, against distance from its axis.
- Inside, the field grows linearly with $r$; outside, it falls as $1/r$. The two meet at the surface.
- The readouts give the divergence inside (constant, $\rho/\varepsilon_0$) and outside (zero), computed with the cylindrical formula.
:::

```sim plot
view: graph
x: r = 0..5 cm
y: E = 0..1.2
slider: a = 0.5..3 step 0.1 = 1.5 cm
plot: (r/a)*step(a - r) + (a/r)*step(r - a) | label: E / E(a) | samples: 600
marker: r = a | label: surface
readout: divergence inside, in units of ρ/ε₀ = 1 | digits: 1
readout: divergence outside = 0 | digits: 1
```

::: predict
## A point charge's field is $\frac{kq}{r^2}\hat{\mathbf r}$. What is its divergence away from the charge?

- [ ] $\frac{2kq}{r^3}$, from differentiating $\frac{1}{r^2}$
- [x] Zero
- [ ] $-\frac{2kq}{r^3}$
- [ ] Infinite

::: reveal
Zero. In spherical coordinates, $\nabla\cdot(F_r\hat{\mathbf r}) = \frac{1}{r^2}\frac{d}{dr}(r^2F_r)$, and $r^2 \cdot \frac{kq}{r^2} = kq$ is constant. The field weakens as $\frac{1}{r^2}$ exactly as fast as the spheres it crosses grow, so no field lines start or end in empty space. Differentiating only $F_r$ forgets that spreading.
:::
:::

::: nameit
## Cylindrical coordinates $(r, \theta, z)$

$$\nabla f = \frac{\partial f}{\partial r}\hat{\mathbf r} + \frac1r\frac{\partial f}{\partial\theta}\hat{\boldsymbol\theta} + \frac{\partial f}{\partial z}\hat{\mathbf z}$$

$$\nabla\cdot\mathbf F = \frac1r\frac{\partial(rF_r)}{\partial r} + \frac1r\frac{\partial F_\theta}{\partial\theta} + \frac{\partial F_z}{\partial z}, \qquad (\nabla\times\mathbf F)_z = \frac1r\left(\frac{\partial(rF_\theta)}{\partial r} - \frac{\partial F_r}{\partial\theta}\right)$$

$$\nabla^2f = \frac1r\frac{\partial}{\partial r}\left(r\frac{\partial f}{\partial r}\right) + \frac{1}{r^2}\frac{\partial^2f}{\partial\theta^2} + \frac{\partial^2f}{\partial z^2}$$

## Spherical coordinates $(r, \theta, \varphi)$, for fields that depend on $r$ only

$$\nabla f = f'(r)\hat{\mathbf r}, \qquad \nabla\cdot(F_r\hat{\mathbf r}) = \frac{1}{r^2}\frac{d(r^2F_r)}{dr}, \qquad \nabla^2f = \frac{1}{r^2}\frac{d}{dr}\left(r^2\frac{df}{dr}\right)$$

## Where the factors come from

The divergence counts net flux out of a small box. In cylindrical coordinates the box's outer face is larger than its inner face by a factor $\frac{r + dr}{r}$, so a field with constant $F_r$ still has positive divergence ($\frac{F_r}{r}$): its lines spread out. The $\frac1r$ in the $\theta$ terms converts an angle into a distance.

## Fields to know

| Field | Divergence | Curl |
|---|---|---|
| $\frac{1}{r}\hat{\mathbf r}$ (cylindrical: line charge) | 0 | 0 |
| $\frac1r\hat{\boldsymbol\theta}$ (around a wire) | 0 | 0 |
| $r\hat{\boldsymbol\theta}$ (rigid rotation) | 0 | $2\hat{\mathbf z}$ |
| $\frac{1}{r^2}\hat{\mathbf r}$ (spherical: point charge) | 0 | 0 |
| $r\hat{\mathbf r}$ (spherical: uniform charge) | 3 | 0 |
:::

::: example
## A long rod of radius $a$ carries uniform charge density $\rho$. Find $\mathbf E$ inside and check its divergence.

::: solution
1. **Gauss's law on a cylinder.** Take radius $r < a$ and length $\ell$:
   $$\begin{aligned} E \cdot 2\pi r\ell &= \frac{\rho\pi r^2\ell}{\varepsilon_0} \\ E_r &= \frac{\rho r}{2\varepsilon_0} \end{aligned}$$
2. **Its divergence.**
   $$\begin{aligned} &\frac1r\frac{d}{dr}\left(r \cdot \frac{\rho r}{2\varepsilon_0}\right) \\ &= \frac1r \cdot \frac{\rho r}{\varepsilon_0} \\ &= \frac{\rho}{\varepsilon_0} \end{aligned}$$
   as Gauss's law in differential form requires.
3. **A number.** With $\rho = 1$ µC/m³, at $r = 1$ cm:
   $$\begin{aligned} E &= \frac{10^{-6} \times 0.01}{2 \times 8.854 \times 10^{-12}} \\ &= 565\ \text{V/m} \end{aligned}$$

**Answer:** inside, $E_r = \frac{\rho r}{2\varepsilon_0}$, whose divergence is $\frac{\rho}{\varepsilon_0}$; with 1 µC/m³ it's 565 V/m at 1 cm.
:::
:::

::: example
## Check that the field around a wire, $\mathbf B = \frac{\mu_0I}{2\pi r}\hat{\boldsymbol\theta}$, has no curl outside the wire.

::: solution
1. **Curl in cylindrical coordinates.**
   $$\begin{aligned} (\nabla\times\mathbf B)_z &= \frac1r\frac{d}{dr}(rB_\theta) \\ &= \frac1r\frac{d}{dr}\left(\frac{\mu_0I}{2\pi}\right) \\ &= 0 \end{aligned}$$
2. **Compare with Ampère's law.** Outside the wire, where $\mathbf J = 0$, Ampère's law $\nabla\times\mathbf B = \mu_0\mathbf J$ holds with zero on both sides, even though the field circles.
3. **Where the curl lives.** All of its curl is concentrated in the wire, which is why every loop around the wire has the same circulation, $\mu_0I$.

**Answer:** outside the wire the curl is 0, so Ampère's law holds there with zero on both sides.
:::
:::

::: example
## Find the Laplacian of $V = \frac{1}{r}$ in spherical coordinates, away from the origin.

::: solution
1. **Differentiate.**
   $$\frac{dV}{dr} = -\frac{1}{r^2}$$
   so $r^2\frac{dV}{dr} = -1$, a constant.
2. **Laplacian.**
   $$\begin{aligned} \nabla^2V &= \frac{1}{r^2}\frac{d}{dr}(-1) \\ &= 0 \end{aligned}$$
3. **Interpret.** The potential of a point charge is harmonic everywhere except at the charge. In two dimensions the same role is played by $\ln r$, the potential of a line charge.

**Answer:** $\nabla^2\frac1r = 0$ everywhere away from the origin.
:::
:::

::: check
given: c = 1 | 2; n = 2 | 3; r = 0.5 | 2
answer: (n + 1)*c*r^(n - 1)
tolerance: 0.5%

## In cylindrical coordinates, what is the divergence of $\mathbf F = \given{c}r^{\given{n}}\hat{\mathbf r}$ at $r$ = {r}?

::: right
Right. $\frac1r\frac{d}{dr}(cr^{n+1}) = (n + 1)cr^{n-1}$ = {answer}.
:::

::: misses
- [n*c*r^(n - 1)] That's only $\frac{dF_r}{dr}$; the spreading adds $\frac{F_r}{r}$.
- [(n + 2)*c*r^(n - 1)] That's the spherical formula.
- [any] Not yet. Use $\frac1r\frac{\partial(rF_r)}{\partial r}$.
:::
:::

::: check
given: c = 1 | 2; n = 2 | 3; r = 0.5 | 2
answer: (n + 2)*c*r^(n - 1)
tolerance: 0.5%

## In spherical coordinates, what is the divergence of $\mathbf F = \given{c}r^{\given{n}}\hat{\mathbf r}$ at $r$ = {r}?

::: right
Right. $\frac{1}{r^2}\frac{d}{dr}(cr^{n+2}) = (n + 2)cr^{n-1}$ = {answer}.
:::

::: misses
- [(n + 1)*c*r^(n - 1)] That's the cylindrical formula; spheres spread in two directions.
- [any] Not yet. Use $\frac{1}{r^2}\frac{d(r^2F_r)}{dr}$.
:::
:::

::: check
given: rho = 1 | 5; r = 0.5 | 2
answer: rho*1e-6*r/100/(2*8.854e-12)
unit: V/m
tolerance: 0.5%

## A long rod with $\rho$ = {rho} µC/m³. What is the field {r} cm from its axis, inside the rod?

::: right
Right. $\frac{\rho r}{2\varepsilon_0}$ = {answer}.
:::

::: misses
- [rho*1e-6*r/100/(3*8.854e-12)] That's the inside field of a charged sphere.
- [any] Not yet. Gauss's law on a cylinder: $E \cdot 2\pi r\ell = \frac{\rho\pi r^2\ell}{\varepsilon_0}$.
:::
:::

::: check
given: w = 2 | 5; r = 0.5 | 3
answer: 2*w
unit: s⁻¹
tolerance: 0.5%

## A rigid rotation has $\mathbf v = \given{w}r\hat{\boldsymbol\theta}$. What is the $z$ component of its curl at $r$ = {r}?

::: right
Right. $\frac1r\frac{d}{dr}(r \cdot \omega r) = 2\omega$ = {answer}, the same everywhere.
:::

::: misses
- [w] The curl is twice the angular velocity.
- [any] Not yet. Use $\frac1r\frac{\partial(rF_\theta)}{\partial r}$.
:::
:::

::: check
kind: parts
given: a = 1 | 2; b = 3 | 6

## A coaxial cable has inner radius {a} mm and outer radius {b} mm, with the inner conductor at 1 V and the outer at 0. Inside, $V = A\ln r + B$.

::: part
answer: -1/ln(b/a)
tolerance: 0.5%

### What is $A$?

::: misses
- [1/ln(b/a)] Check the sign: $V$ falls from 1 at $a$ to 0 at $b$.
- [any] $A\ln a + B = 1$ and $A\ln b + B = 0$.
:::
:::

::: part
answer: 1/(ln(b/a)*a/1000)
unit: V/m
tolerance: 0.5%

### What is the field at the inner conductor's surface?

::: misses
- [any] $E = -\frac{dV}{dr} = -\frac Ar$ at $r = a$.
:::
:::
:::

::: check
kind: derivation
vars: Q = 0.5..5, R = 0.5..3, r = 0.1..0.4

## A ball of radius $R$ holds charge uniformly, so inside $E_r = \frac{kQr}{R^3}$. Take $k = 1$.

::: step
answer: Q*r^3/R^3

### What is $r^2E_r$?
:::

::: step
answer: 3*Q/R^3

### What is $\frac{1}{r^2}\frac{d}{dr}(r^2E_r)$?
:::

::: right
Right. $\frac{3Q}{R^3}$ is $4\pi$ times the charge density $\frac{Q}{\frac43\pi R^3}$, as $\nabla\cdot\mathbf E = 4\pi k\rho$ requires.
:::
:::

::: check
kind: python

## On a 201 × 201 grid over $[0.5, 2.5]^2$, compute the divergence of $\frac{(x, y)}{x^2 + y^2}$ (a line charge's field) and of $\frac{(x, y)}{\sqrt{x^2 + y^2}}$ (a constant-size radial field) with `np.gradient`. Set `d1` and `d2` to their values at the grid point nearest $(1.5, 1.5)$, and `formula2` to $\frac1r$ there.

```python starter
import numpy as np

d1 = d2 = formula2 = 1.0
print(d1, d2, formula2)
```

```python solution
import numpy as np

x = np.linspace(0.5, 2.5, 201)
h = x[1] - x[0]
X, Y = np.meshgrid(x, x, indexing="ij")
R2 = X**2 + Y**2

def div(Fx, Fy):
    return np.gradient(Fx, h, axis=0) + np.gradient(Fy, h, axis=1)

k = 100
d1 = div(X / R2, Y / R2)[k, k]
d2 = div(X / np.sqrt(R2), Y / np.sqrt(R2))[k, k]
formula2 = 1 / np.sqrt(R2[k, k])
print(d1, d2, formula2)
```

```python tests
assert abs(d1) < 1e-4, f"A line charge's field has no divergence away from the line; got {d1}."
assert abs(d2 - formula2) < 1e-4, f"A constant radial field has divergence 1/r = {formula2:.5f}; got {d2}."
```

::: right
Right. The Cartesian computation agrees with the cylindrical formula $\frac1r\frac{d(rF_r)}{dr}$: zero for $\frac1r$, and $\frac1r$ for a constant.
:::
:::

::: explain
## Why does a field that points straight out with constant strength, $\mathbf F = \hat{\mathbf r}$, have positive divergence even though its size never changes?

::: model
Divergence measures net flux out of a small volume, not whether the field changes along a line. Take a thin slab of a cylindrical shell between $r$ and $r + dr$: flux enters through the inner surface and leaves through the outer one, which is larger by the factor $\frac{r + dr}{r}$. With the same field strength on both, more leaves than enters, so there's net outflow, $\frac1r$ per unit volume in cylindrical coordinates and $\frac2r$ in spherical. A field that keeps its divergence zero must weaken as $\frac1r$ or $\frac{1}{r^2}$, which is exactly how the fields of line and point charges behave.
:::
:::

::: practice

::: check
given: c = 0.5..3; n = 0..4 int; r = 0.1..3
answer: (n + 1)*c*r^(n - 1)
tolerance: 0.5%

## Cylindrical divergence of $\given{c}r^{\given{n}}\hat{\mathbf r}$ at $r$ = {r}?

::: misses
- [any] $(n + 1)cr^{n-1}$.
:::
:::

::: check
given: c = 0.5..3; n = -1..4 int; r = 0.1..3
answer: (n + 2)*c*r^(n - 1)
tolerance: 0.5%

## Spherical divergence of $\given{c}r^{\given{n}}\hat{\mathbf r}$ at $r$ = {r}?

::: misses
- [any] $(n + 2)cr^{n-1}$.
:::
:::
:::
