TExcelTAD
Home › Calculus in Depth › Curl and Divergence

Curl and Divergence

Curl and divergence are the two derivatives of a vector field. The curl measures how much a field rotates around each point, the divergence how much it spreads out from it. They appear in every theorem that follows, in the equations of fluid flow and electromagnetism, and in the pressure-projection step that keeps simulated smoke and water from gaining or losing volume. This lesson defines both with the ∇\nabla operator, proves the two key identities, interprets them physically and builds a working incompressibility projection. For a gentler start, see Divergence and Curl.

Definitions

Treat the del operator ∇=⟨∂/∂x,∂/∂y,∂/∂z⟩\nabla = \langle \partial/\partial x, \partial/\partial y, \partial/\partial z \rangle as a formal vector. For F=⟨P,Q,R⟩\mathbf{F} = \langle P, Q, R \rangle on R3\mathbb{R}^3 with partial derivatives:

curl⁡F=∇×F=∣ijk∂∂x∂∂y∂∂zPQR∣=⟨Ry−Qz, Pz−Rx, Qx−Py⟩div⁡F=∇⋅F=Px+Qy+Rz\begin{aligned} \operatorname{curl} \mathbf{F} &= \nabla \times \mathbf{F} = \begin{vmatrix} \mathbf{i} & \mathbf{j} & \mathbf{k} \\ \frac{\partial}{\partial x} & \frac{\partial}{\partial y} & \frac{\partial}{\partial z} \\ P & Q & R \end{vmatrix} = \langle R_y - Q_z,\ P_z - R_x,\ Q_x - P_y \rangle \\[1ex] \operatorname{div} \mathbf{F} &= \nabla \cdot \mathbf{F} = P_x + Q_y + R_z \end{aligned}

The curl is a vector field, the divergence a scalar field. A 2D field ⟨P,Q⟩\langle P, Q \rangle is treated as ⟨P,Q,0⟩\langle P, Q, 0 \rangle, so its curl is ⟨0,0,Qx−Py⟩\langle 0, 0, Q_x - P_y \rangle, the integrand of Green's theorem. Combining them gives the Laplacian ∇2f=∇⋅∇f=fxx+fyy+fzz\nabla^2 f = \nabla \cdot \nabla f = f_{xx} + f_{yy} + f_{zz}.

Two identities

Theorem 1. If ff has continuous second partial derivatives, curl⁡(∇f)=0\operatorname{curl}(\nabla f) = \mathbf{0}.

Proof. The first component is ∂∂y(fz)−∂∂z(fy)=fzy−fyz=0\frac{\partial}{\partial y}(f_z) - \frac{\partial}{\partial z}(f_y) = f_{zy} - f_{yz} = 0 by Clairaut's theorem, and likewise for the other two. ∎

Theorem 2. If F=⟨P,Q,R⟩\mathbf{F} = \langle P, Q, R \rangle has continuous second partials, div⁡(curl⁡F)=0\operatorname{div}(\operatorname{curl} \mathbf{F}) = 0.

Proof. ∂∂x(Ry−Qz)+∂∂y(Pz−Rx)+∂∂z(Qx−Py)=Ryx−Qzx+Pzy−Rxy+Qxz−Pyz=0\frac{\partial}{\partial x}(R_y - Q_z) + \frac{\partial}{\partial y}(P_z - R_x) + \frac{\partial}{\partial z}(Q_x - P_y) = R_{yx} - Q_{zx} + P_{zy} - R_{xy} + Q_{xz} - P_{yz} = 0, the terms cancel in pairs by Clairaut. ∎

Theorem 1 gives a test: if curl⁡F≠0\operatorname{curl} \mathbf{F} \ne \mathbf{0} somewhere, F\mathbf{F} is not conservative. Conversely, if F\mathbf{F} is defined on all of R3\mathbb{R}^3 (or any simply connected region) and curl⁡F=0\operatorname{curl} \mathbf{F} = \mathbf{0}, then F\mathbf{F} is conservative; the proof uses Stokes' theorem. Theorem 2 gives another test: if div⁡G≠0\operatorname{div} \mathbf{G} \ne 0, then G\mathbf{G} is not the curl of any field.

python · try it yourself
import sympy as sp

x, y, z = sp.symbols('x y z')
def curl(F): return [sp.diff(F[2], y) - sp.diff(F[1], z), sp.diff(F[0], z) - sp.diff(F[2], x), sp.diff(F[1], x) - sp.diff(F[0], y)]
def div(F):  return sp.diff(F[0], x) + sp.diff(F[1], y) + sp.diff(F[2], z)
def grad(f): return [sp.diff(f, v) for v in (x, y, z)]

F = [y**2 * z, x * sp.sin(z), sp.exp(x) * y]
print("curl F =", curl(F))
print("div F  =", div(F))

f = sp.Function('f')(x, y, z)                       # an arbitrary smooth function
G = [sp.Function(n)(x, y, z) for n in 'PQR']        # an arbitrary smooth field
print("curl(grad f) =", [sp.simplify(c) for c in curl(grad(f))])
print("div(curl G)  =", sp.simplify(div(curl(G))))
curl F = [-x*cos(z) + exp(x), y**2 - y*exp(x), -2*y*z + sin(z)]
div F  = 0
curl(grad f) = [0, 0, 0]
div(curl G)  = 0

The identities hold for completely general ff and G\mathbf{G}, since SymPy treats mixed partials as equal. The example field F\mathbf{F} happens to be incompressible (div⁡F=0\operatorname{div} \mathbf{F} = 0) because each component does not depend on its own variable, yet its curl is not zero, so it is not conservative.

What they measure

Curl as rotation. Put a tiny paddle wheel in a fluid with velocity field F\mathbf{F}. It spins fastest when its axle points along curl⁡F\operatorname{curl} \mathbf{F}, and its angular speed is 12∣curl⁡F∣\frac{1}{2}\lvert \operatorname{curl} \mathbf{F} \rvert. For a rigid rotation F=ω⟨−y,x,0⟩\mathbf{F} = \omega\langle -y, x, 0 \rangle the curl is ⟨0,0,2ω⟩\langle 0, 0, 2\omega \rangle, twice the angular velocity. A field with curl⁡F=0\operatorname{curl} \mathbf{F} = \mathbf{0} is called irrotational.

Divergence as net outflow. div⁡F(P)\operatorname{div} \mathbf{F}(P) is the limit of (flux out of a small box around P)÷(volume of the box)(\text{flux out of a small box around } P) \div (\text{volume of the box}). Positive divergence is a source, negative a sink, zero means the flow is incompressible: as much enters every tiny region as leaves it. The next example checks this flux interpretation with a small cube and also computes divergence on a sampled grid, as a simulation would.

python · try it yourself
import numpy as np

F = lambda x, y, z: np.array([x**2 * y, np.sin(y) + z, x * z**2])
div_exact = lambda x, y, z: 2 * x * y + np.cos(y) + 2 * x * z

p, h = np.array([1.0, 0.5, -1.0]), 1e-3          # flux out of a cube of side h around p
flux = 0.0
for axis in range(3):
    e = np.zeros(3); e[axis] = h / 2
    flux += (F(*(p + e))[axis] - F(*(p - e))[axis]) * h**2    # face-centre values × face area
print("flux / volume ≈", round(flux / h**3, 6), "  exact div =", round(float(div_exact(*p)), 6))

g = np.linspace(-1, 1, 41)                        # the same field sampled on a grid
X, Y, Z = np.meshgrid(g, g, g, indexing="ij")
P, Q, R = F(X, Y, Z)
d = g[1] - g[0]
div_grid = np.gradient(P, d, axis=0) + np.gradient(Q, d, axis=1) + np.gradient(R, d, axis=2)
err = np.abs(div_grid - div_exact(X, Y, Z))[1:-1, 1:-1, 1:-1].max()
print("max interior error of np.gradient divergence:", round(float(err), 6))
flux / volume ≈ -0.122417   exact div = -0.122417
max interior error of np.gradient divergence: 0.000417

Green's theorem in vector form

With T\mathbf{T} the unit tangent and n\mathbf{n} the outward unit normal of the boundary curve CC of a plane region DD, Green's theorem can be written in two ways:

∮CF⋅T ds=∬D(curl⁡F)⋅k dA(circulation = total rotation)∮CF⋅n ds=∬Ddiv⁡F dA(outflow through C = total source strength)\begin{aligned} \oint_C \mathbf{F} \cdot \mathbf{T}\,ds &= \iint_D (\operatorname{curl} \mathbf{F}) \cdot \mathbf{k}\,dA && \text{(circulation = total rotation)} \\ \oint_C \mathbf{F} \cdot \mathbf{n}\,ds &= \iint_D \operatorname{div} \mathbf{F}\,dA && \text{(outflow through } C \text{ = total source strength)} \end{aligned}

The first lifts to Stokes' theorem on surfaces, the second to the Divergence Theorem on solids.

In software: pressure projection

Real-time fluid solvers (smoke, water and fire in games and film) store a velocity grid u\mathbf{u}. After moving and pushing the fluid, u\mathbf{u} is no longer divergence-free, so the solver projects it: by the Helmholtz decomposition u=w+∇p\mathbf{u} = \mathbf{w} + \nabla p with div⁡w=0\operatorname{div} \mathbf{w} = 0, and taking divergence gives a Poisson equation ∇2p=div⁡u\nabla^2 p = \operatorname{div} \mathbf{u}. Solve for the pressure pp, then subtract its gradient: w=u−∇p\mathbf{w} = \mathbf{u} - \nabla p. On a periodic grid the Poisson solve is one FFT, because each Fourier mode eik⋅xe^{i\mathbf{k} \cdot \mathbf{x}} turns ∇\nabla into multiplication by iki\mathbf{k}.

python · try it yourself
import numpy as np

N = 64
x = np.arange(N) * 2 * np.pi / N
X, Y = np.meshgrid(x, x, indexing="ij")
swirl = np.array([-np.sin(Y), np.sin(X)])                              # div = 0
source = np.array([-np.sin(X) * np.cos(2*Y), -2 * np.cos(X) * np.sin(2*Y)])  # = ∇(cos x cos 2y)
u = swirl + source

k = np.fft.fftfreq(N, d=1 / N)                                         # integer wavenumbers
KX, KY = np.meshgrid(k, k, indexing="ij")
K2 = KX**2 + KY**2; K2[0, 0] = 1
uh = np.fft.fft2(u)
div_hat = 1j * (KX * uh[0] + KY * uh[1])
print("max |div u| before:", round(float(np.abs(np.fft.ifft2(div_hat)).max()), 6))

ph = div_hat / -K2                                                     # ∇²p = div u
w = np.real(np.fft.ifft2(uh - np.array([1j * KX * ph, 1j * KY * ph])))  # w = u − ∇p
div_w = np.real(np.fft.ifft2(1j * (KX * np.fft.fft2(w[0]) + KY * np.fft.fft2(w[1]))))
print("max |div w| after below 1e-12:", bool(np.abs(div_w).max() < 1e-12))
print("w equals the swirl part:", bool(np.allclose(w, swirl)))
max |div u| before: 5.0
max |div w| after below 1e-12: True
w equals the swirl part: True

The source part ∇(cos⁡xcos⁡2y)\nabla(\cos x \cos 2y) has divergence −5cos⁡xcos⁡2y-5\cos x \cos 2y (maximum size 5); projection removes exactly that gradient part and leaves the swirl. Other places curl and divergence appear:

  • Electromagnetism and physics engines: Maxwell's equations are statements about div and curl of E\mathbf{E} and B\mathbf{B}.
  • Procedural animation: "curl noise" takes the curl of a noise field to get a divergence-free velocity field (Theorem 2), so particles swirl like smoke without bunching up or thinning out.
  • Game AI: a steering field built as a gradient has zero curl, so agents never circle endlessly; adding a curl component makes them orbit.
  • Image processing: divergence of an image gradient is the Laplacian, used for edge detection and Poisson image blending.

Exercises

Exercise 1: Compute curl and div

Find curl⁡F\operatorname{curl} \mathbf{F} and div⁡F\operatorname{div} \mathbf{F} for F=⟨xy,yz,zx⟩\mathbf{F} = \langle xy, yz, zx \rangle.

Answer

curl⁡F=⟨−y,−z,−x⟩\operatorname{curl} \mathbf{F} = \langle -y, -z, -x \rangle, div⁡F=x+y+z\operatorname{div} \mathbf{F} = x + y + z.

Solution
P=xy,Q=yz,R=zxcurl⁡F=⟨Ry−Qz,Pz−Rx,Qx−Py⟩=⟨0−y,0−z,0−x⟩=⟨−y,−z,−x⟩div⁡F=Px+Qy+Rz=y+z+x\begin{aligned} &P = xy,\quad Q = yz,\quad R = zx \\ \operatorname{curl} \mathbf{F} &= \langle R_y - Q_z, P_z - R_x, Q_x - P_y \rangle = \langle 0 - y, 0 - z, 0 - x \rangle = \langle -y, -z, -x \rangle \\ \operatorname{div} \mathbf{F} &= P_x + Q_y + R_z = y + z + x \end{aligned}

Exercise 2: Conservative?

Is F=⟨2xy,x2+2yz,y2⟩\mathbf{F} = \langle 2xy, x^2 + 2yz, y^2 \rangle conservative on R3\mathbb{R}^3? If so, find a potential.

Answer

Yes; f=x2y+y2z+Cf = x^2y + y^2z + C.

Solution
curl⁡F=⟨Ry−Qz,Pz−Rx,Qx−Py⟩=⟨2y−2y,0−0,2x−2x⟩=0R3 is simply connected, so F is conservative.fx=2xy⇒f=x2y+g(y,z);fy=x2+gy=x2+2yz⇒g=y2z+h(z);fz=y2⇒h constant\begin{aligned} \operatorname{curl} \mathbf{F} &= \langle R_y - Q_z, P_z - R_x, Q_x - P_y \rangle = \langle 2y - 2y, 0 - 0, 2x - 2x \rangle = \mathbf{0} \\ &\mathbb{R}^3 \text{ is simply connected, so } \mathbf{F} \text{ is conservative.} \\ f_x &= 2xy \Rightarrow f = x^2y + g(y, z); \quad f_y = x^2 + g_y = x^2 + 2yz \Rightarrow g = y^2z + h(z); \quad f_z = y^2 \Rightarrow h \text{ constant} \end{aligned}

Exercise 3: Not a curl

Show that G=⟨x,y,z⟩\mathbf{G} = \langle x, y, z \rangle is not the curl of any vector field.

Answer

div⁡G=3≠0\operatorname{div} \mathbf{G} = 3 \ne 0, but every curl has divergence 0.

Solution

If G=curl⁡F\mathbf{G} = \operatorname{curl} \mathbf{F}, then div⁡G=div⁡(curl⁡F)=0\operatorname{div} \mathbf{G} = \operatorname{div}(\operatorname{curl} \mathbf{F}) = 0 by Theorem 2. But div⁡G=1+1+1=3\operatorname{div} \mathbf{G} = 1 + 1 + 1 = 3, a contradiction.

Exercise 4: Paddle wheel speed (code)

A fluid flows with velocity F=⟨−3y,3x,0⟩+⟨z,0,0⟩\mathbf{F} = \langle -3y, 3x, 0 \rangle + \langle z, 0, 0 \rangle. Compute curl⁡F\operatorname{curl} \mathbf{F} by hand, then use SymPy to confirm it and print the angular speed 12∣curl⁡F∣\frac{1}{2}\lvert \operatorname{curl} \mathbf{F} \rvert of a small paddle wheel.

Answer

curl⁡F=⟨0,1,6⟩\operatorname{curl} \mathbf{F} = \langle 0, 1, 6 \rangle; angular speed 372≈3.041381\frac{\sqrt{37}}{2} \approx 3.041381.

Solution
P=−3y+z,Q=3x,R=0curl⁡F=⟨0−0,1−0,3−(−3)⟩=⟨0,1,6⟩,∣curl⁡F∣=37\begin{aligned} &P = -3y + z,\quad Q = 3x,\quad R = 0 \\ \operatorname{curl} \mathbf{F} &= \langle 0 - 0, 1 - 0, 3 - (-3) \rangle = \langle 0, 1, 6 \rangle, \qquad \lvert \operatorname{curl} \mathbf{F} \rvert = \sqrt{37} \end{aligned}
python · try it yourself
import sympy as sp

x, y, z = sp.symbols('x y z')
P, Q, R = -3*y + z, 3*x, 0
c = sp.Matrix([sp.diff(R, y) - sp.diff(Q, z), sp.diff(P, z) - sp.diff(R, x), sp.diff(Q, x) - sp.diff(P, y)])
print(list(c), c.norm() / 2, round(float(c.norm() / 2), 6))
[0, 1, 6] sqrt(37)/2 3.041381

Test yourself

Which statement is true for every smooth function ff?

  1. div⁡(∇f)=0\operatorname{div}(\nabla f) = 0
  2. curl⁡(∇f)=0\operatorname{curl}(\nabla f) = \mathbf{0}
  3. ∇(div⁡f)=0\nabla(\operatorname{div} f) = 0
  4. curl⁡f=∇f\operatorname{curl} f = \nabla f

A velocity field has div⁡F=0\operatorname{div} \mathbf{F} = 0 everywhere. The flow is…

  1. irrotational
  2. conservative
  3. incompressible
  4. constant

In a fluid solver's pressure projection, what is subtracted from the velocity field?

  1. The gradient of the pressure pp, where ∇2p=div⁡u\nabla^2 p = \operatorname{div} \mathbf{u}
  2. The curl of the velocity
  3. The divergence itself
  4. A constant vector