PHYS-381 · Lesson 2 of 12 · 23 min

Vector calculus for electromagnetism: gradient, divergence, curl and the theorems

Electromagnetic theory is written in the language of vector calculus. The multivariable calculus course introduced the operators and the integral theorems; this lesson collects them in the form physicists use, explains what each one measures, proves the key identities and checks the two great theorems numerically. Everything here will be used repeatedly: the gradient connects the potential to the field, the divergence measures sources, the curl measures circulation, and the divergence and Stokes theorems turn local differential laws into the integral laws you can apply to Gaussian surfaces and Amperian loops.

The del operator

In Cartesian coordinates the vector differential operator del is

\[\nabla = \hat{\mathbf{x}}\frac{\partial}{\partial x} + \hat{\mathbf{y}}\frac{\partial}{\partial y} + \hat{\mathbf{z}}\frac{\partial}{\partial z}.\]

It is not a vector in the ordinary sense, because it has no value until it acts on something, but it behaves like one in many formulas. It can act in three ways. On a scalar function \(T\) it gives the gradient \(\nabla T\), a vector. Dotted into a vector function \(\mathbf{v}\) it gives the divergence \(\nabla\cdot\mathbf{v}\), a scalar. Crossed with \(\mathbf{v}\) it gives the curl \(\nabla\times\mathbf{v}\), a vector. There is no fourth first-derivative operation: these three exhaust the ways of combining a vector operator with a scalar or a vector.

The gradient

For a scalar function \(T(x, y, z)\), the change in \(T\) when we move by a small displacement \(d\mathbf{l} = (dx, dy, dz)\) is

\[dT = \frac{\partial T}{\partial x}dx + \frac{\partial T}{\partial y}dy + \frac{\partial T}{\partial z}dz = \nabla T \cdot d\mathbf{l} = |\nabla T|\,|d\mathbf{l}|\cos\theta.\]

For a step of fixed length, \(dT\) is largest when \(d\mathbf{l}\) points along \(\nabla T\). So the gradient points in the direction of steepest increase, and its magnitude is the rate of increase in that direction. It is perpendicular to the level surfaces of \(T\), because moving along a level surface does not change \(T\). Integrating \(dT\) along any path gives the fundamental theorem for gradients,

\[\int_{\mathbf{a}}^{\mathbf{b}} \nabla T\cdot d\mathbf{l} = T(\mathbf{b}) - T(\mathbf{a}),\]

so the line integral of a gradient depends only on the end points, and around any closed loop it is zero. In electrostatics the field is minus the gradient of the potential, \(\mathbf{E} = -\nabla V\), so the field points from high potential to low, crosses equipotential surfaces at right angles, and does no net work on a charge taken around a closed path.

The divergence

The divergence of \(\mathbf{v} = (v_x, v_y, v_z)\) is

\[\nabla\cdot\mathbf{v} = \frac{\partial v_x}{\partial x} + \frac{\partial v_y}{\partial y} + \frac{\partial v_z}{\partial z}.\]

It measures how much the field spreads out from a point. Picture \(\mathbf{v}\) as the velocity of a fluid. If more fluid leaves a small box than enters it, there must be a source inside, and the divergence is positive. To see this, take a tiny box with sides \(dx\), \(dy\), \(dz\). The net outward flow through the two faces perpendicular to \(x\) is \([v_x(x + dx) - v_x(x)]\,dy\,dz = (\partial v_x/\partial x)\,d\tau\), where \(d\tau = dx\,dy\,dz\). Adding the other two pairs of faces gives a net outward flux of \((\nabla\cdot\mathbf{v})\,d\tau\). The divergence is outward flux per unit volume. In electrostatics, \(\nabla\cdot\mathbf{E} = \rho/\varepsilon_0\): electric field spreads out from positive charge and converges on negative charge, and in empty space it has zero divergence.

The curl

The curl of \(\mathbf{v}\) is

\[\nabla\times\mathbf{v} = \hat{\mathbf{x}}\Big(\frac{\partial v_z}{\partial y} - \frac{\partial v_y}{\partial z}\Big) + \hat{\mathbf{y}}\Big(\frac{\partial v_x}{\partial z} - \frac{\partial v_z}{\partial x}\Big) + \hat{\mathbf{z}}\Big(\frac{\partial v_y}{\partial x} - \frac{\partial v_x}{\partial y}\Big).\]

It measures how much the field circulates around a point. If you placed a tiny paddle wheel in the fluid, the curl would point along the axis about which the wheel turns fastest, with a magnitude equal to twice its angular velocity. Applying the same box argument to a small rectangle in the xy plane, the circulation \(\oint \mathbf{v}\cdot d\mathbf{l}\) around it is \((\partial v_y/\partial x - \partial v_x/\partial y)\,dx\,dy\), the z component of the curl times the area. The curl is circulation per unit area. A field can circulate without visibly swirling: a shear flow such as \(\mathbf{v} = (y, 0, 0)\), where the fluid moves faster higher up, turns a paddle wheel even though every streamline is straight.

Three panels of arrows on a grid. In the first, arrows point outward from the middle and grow longer with distance: the field (x, y) with divergence 2 and no curl. In the second, arrows circle the middle counterclockwise and grow with distance: the field (-y, x) with no divergence and curl 2 along z. In the third, horizontal arrows point right above the middle row and left below it, growing with height: the shear flow (y, 0), with no divergence and curl -1 along z.
Three plane fields. A spreading field has divergence but no curl, a rotating field has curl but no divergence, and a shear flow has curl even though its arrows are all parallel

Product rules and second derivatives

Because \(\nabla\) is a differential operator, products obey versions of the product rule, for example

\[\nabla\cdot(f\mathbf{A}) = f\,(\nabla\cdot\mathbf{A}) + \mathbf{A}\cdot(\nabla f), \qquad \nabla\times(f\mathbf{A}) = f\,(\nabla\times\mathbf{A}) - \mathbf{A}\times(\nabla f),\]
\[\nabla\cdot(\mathbf{A}\times\mathbf{B}) = \mathbf{B}\cdot(\nabla\times\mathbf{A}) - \mathbf{A}\cdot(\nabla\times\mathbf{B}).\]

Each can be verified by writing out components. Applying \(\nabla\) twice gives five combinations, of which two always vanish. The curl of a gradient is zero, \(\nabla\times(\nabla T) = \mathbf{0}\), because mixed partial derivatives are equal: the z component is \(\partial^2 T/\partial x\partial y - \partial^2 T/\partial y\partial x = 0\). The divergence of a curl is zero, \(\nabla\cdot(\nabla\times\mathbf{v}) = 0\), for the same reason. The divergence of a gradient is the Laplacian,

\[\nabla^2 T = \nabla\cdot\nabla T = \frac{\partial^2 T}{\partial x^2} + \frac{\partial^2 T}{\partial y^2} + \frac{\partial^2 T}{\partial z^2},\]

which will dominate lessons 7 and 8. The remaining combination is the curl of a curl, \(\nabla\times(\nabla\times\mathbf{v}) = \nabla(\nabla\cdot\mathbf{v}) - \nabla^2\mathbf{v}\), which is how the wave equation emerges from Maxwell's equations. The two vanishing identities have important converses, proved in more advanced courses: a field with zero curl everywhere is the gradient of a scalar, and a field with zero divergence everywhere is the curl of a vector. The first gives us the electric potential; the second gives us the magnetic vector potential of lesson 11.

The divergence theorem

The divergence theorem, associated with Gauss, Ostrogradsky and Green, states that for a volume \(\mathcal{V}\) bounded by a closed surface \(\mathcal{S}\) with outward normal,

\[\int_{\mathcal{V}} (\nabla\cdot\mathbf{v})\,d\tau = \oint_{\mathcal{S}} \mathbf{v}\cdot d\mathbf{a}.\]

The proof is the box argument extended: divide the volume into tiny cubes. For each cube, the divergence times its volume equals its net outward flux. When the cubes are added, the flux through every interior face is counted twice with opposite signs and cancels, leaving only the flux through the outer surface. The total of the sources inside equals the net flow out through the boundary. Applied to the electric field, it converts \(\nabla\cdot\mathbf{E} = \rho/\varepsilon_0\) into Gauss's law, \(\oint \mathbf{E}\cdot d\mathbf{a} = Q_{\text{enc}}/\varepsilon_0\).

Stokes' theorem

Stokes' theorem relates the curl over a surface \(\mathcal{S}\) to the circulation around its boundary curve \(\mathcal{P}\):

\[\int_{\mathcal{S}} (\nabla\times\mathbf{v})\cdot d\mathbf{a} = \oint_{\mathcal{P}} \mathbf{v}\cdot d\mathbf{l}.\]

The directions are linked by the right-hand rule: if your fingers follow the boundary, your thumb gives the direction of \(d\mathbf{a}\). The proof again tiles the surface with small patches; the circulation around each patch is its curl times its area, and interior edges cancel. Two consequences are worth noting. The left side depends only on the boundary, not on which surface you choose, so the flux of a curl through any surface bounded by the same loop is the same. And the flux of a curl through a closed surface, which has no boundary, is zero. Applied to the magnetic field, Stokes' theorem turns \(\nabla\times\mathbf{B} = \mu_0\mathbf{J}\) into Ampère's law, \(\oint\mathbf{B}\cdot d\mathbf{l} = \mu_0 I_{\text{enc}}\).

Checking the theorems numerically

The program computes divergence and curl by central differences, \(\partial f/\partial x \approx [f(x + h) - f(x - h)]/2h\), for three fields at the point (1, 2, 0.5). The first is \(\mathbf{F}_1 = (y^2,\ 2xy + z^2,\ 2yz)\), the second a pure rotation and the third a pure spreading. It then checks the divergence theorem for \(\mathbf{F}_1\) on the unit cube by computing the volume integral of the divergence with a 40 by 40 by 40 grid of midpoints and, separately, the flux through the six faces. Finally it checks Stokes' theorem for the rotating field around a circle of radius 2.

Divergence and curl by finite differences, and numerical checks of the divergence theorem and Stokes' theorem
import math
# Numerical divergence and curl on a grid by central differences, and checks of the
# divergence theorem and Stokes' theorem for sample fields.
h = 1e-4
def div(F, x, y, z):
    return ((F(x+h, y, z)[0] - F(x-h, y, z)[0]) + (F(x, y+h, z)[1] - F(x, y-h, z)[1])
            + (F(x, y, z+h)[2] - F(x, y, z-h)[2])) / (2*h)
def curl(F, x, y, z):
    d = lambda i, j: (F(*[c + (h if n == j else 0) for n, c in enumerate((x, y, z))])[i]
                     - F(*[c - (h if n == j else 0) for n, c in enumerate((x, y, z))])[i]) / (2*h)
    return (d(2, 1) - d(1, 2), d(0, 2) - d(2, 0), d(1, 0) - d(0, 1))

F1 = lambda x, y, z: (y*y, 2*x*y + z*z, 2*y*z)    # a textbook field
F2 = lambda x, y, z: (-y, x, 0.0)                  # pure rotation
F3 = lambda x, y, z: (x, y, z)                     # pure spreading
p = (1.0, 2.0, 0.5)
for name, F in (("F1", F1), ("F2", F2), ("F3", F3)):
    c = curl(F, *p)
    print(f"{name}: div = {div(F, *p)+0:7.3f}   curl = ({c[0]+0:6.3f}, {c[1]+0:6.3f}, {c[2]+0:6.3f})".replace("-0.000", " 0.000"))

# Divergence theorem for F1 on the unit cube [0,1]^3 (midpoint rule)
n = 40; dA = 1.0 / n**2; dV = 1.0 / n**3
vol = sum(div(F1, (i+.5)/n, (j+.5)/n, (l+.5)/n) * dV
          for i in range(n) for j in range(n) for l in range(n))
flux = 0.0
for i in range(n):
    for j in range(n):
        u, w = (i+.5)/n, (j+.5)/n
        flux += (F1(1, u, w)[0] - F1(0, u, w)[0]) * dA      # x faces
        flux += (F1(u, 1, w)[1] - F1(u, 0, w)[1]) * dA      # y faces
        flux += (F1(u, w, 1)[2] - F1(u, w, 0)[2]) * dA      # z faces
print(f"cube: volume integral of div F1 = {vol:.5f}, outward flux = {flux:.5f}")

# Stokes' theorem for F2 around a circle of radius 2 in the xy plane
N = 2000; circ = 0.0
for k in range(N):
    t = 2*math.pi*(k + .5)/N
    x, y = 2*math.cos(t), 2*math.sin(t)
    dx, dy = -2*math.sin(t)*2*math.pi/N, 2*math.cos(t)*2*math.pi/N
    Fx, Fy, _ = F2(x, y, 0)
    circ += Fx*dx + Fy*dy
print(f"circle r=2: circulation = {circ:.5f}, (curl F2)_z times area = {2*math.pi*4:.5f}")
Output
F1: div =   6.000   curl = ( 0.000,  0.000,  0.000)
F2: div =   0.000   curl = ( 0.000,  0.000,  2.000)
F3: div =   3.000   curl = ( 0.000,  0.000,  0.000)
cube: volume integral of div F1 = 2.00000, outward flux = 2.00000
circle r=2: circulation = 25.13274, (curl F2)_z times area = 25.13274

For \(\mathbf{F}_1\), the divergence is \(2x + 2y = 6\) at the test point and its curl is zero everywhere: it is the gradient of \(xy^2 + yz^2\), so the identity \(\nabla\times\nabla T = \mathbf{0}\) shows up numerically. The volume integral of \(2x + 2y\) over the unit cube is exactly 2, and the six face fluxes add to the same 2 to five decimal places. The midpoint rule happens to be exact here because the integrand is linear. The rotating field \((-y, x, 0)\) has curl 2 in the z direction everywhere, so its flux through a disc of radius 2 is \(2 \times 4\pi = 25.13\), and the circulation around the boundary circle agrees. The pure spreading field \((x, y, z)\) has divergence 3, the number of dimensions, and no curl.

Worked problem

Divergence theorem for a radial field

Verify the divergence theorem for \(\mathbf{v} = r^2\hat{\mathbf{r}}\) over a sphere of radius \(R\) centered at the origin, using the spherical divergence formula \(\nabla\cdot\mathbf{v} = \frac{1}{r^2}\frac{\partial}{\partial r}(r^2 v_r)\) for a radial field.

Show solution
  1. Divergence: \(\nabla\cdot\mathbf{v} = \frac{1}{r^2}\frac{d}{dr}(r^4) = 4r\).
  2. Volume integral: \(\int_0^R 4r\,\cdot 4\pi r^2\,dr = 16\pi R^4/4 = 4\pi R^4\).
  3. Surface integral: on the sphere, \(\mathbf{v}\cdot d\mathbf{a} = R^2 \cdot R^2\sin\theta\,d\theta\,d\phi\), so the flux is \(R^4 \cdot 4\pi\).
  4. The two sides agree.

Answer: Both sides equal \(4\pi R^4\).

Common mistakes

  • MistakeTreating \(\nabla\) as an ordinary vector so that \(\mathbf{A}\cdot\nabla\) and \(\nabla\cdot\mathbf{A}\) seem equal.

    Instead\(\nabla\cdot\mathbf{A}\) is a scalar function; \(\mathbf{A}\cdot\nabla\) is an operator waiting for something to act on.

  • MistakeConcluding that a field with parallel arrows has no curl.

    InsteadShear flows have curl; what matters is how the field changes across the flow.

  • MistakeForgetting the orientation in Stokes' theorem.

    InsteadThe boundary direction and the normal are linked by the right-hand rule.

  • MistakeUsing an inward normal in the divergence theorem.

    Instead\(d\mathbf{a}\) points out of the closed surface.

  • MistakeApplying the Cartesian divergence formula to spherical components.

    InsteadIn curvilinear coordinates the formulas contain scale factors; lesson 3 gives them.

Summary and key terms

  • The gradient points up the steepest slope; its line integral depends only on the end points.
  • The divergence is outward flux per unit volume; the curl is circulation per unit area.
  • \(\nabla\times\nabla T = \mathbf{0}\) and \(\nabla\cdot(\nabla\times\mathbf{v}) = 0\) for every smooth \(T\) and \(\mathbf{v}\).
  • The Laplacian \(\nabla^2 T\) is the divergence of the gradient.
  • Divergence theorem: \(\int (\nabla\cdot\mathbf{v})\,d\tau = \oint \mathbf{v}\cdot d\mathbf{a}\).
  • Stokes' theorem: \(\int (\nabla\times\mathbf{v})\cdot d\mathbf{a} = \oint \mathbf{v}\cdot d\mathbf{l}\).
gradient
The vector of partial derivatives of a scalar, pointing up the steepest slope.
divergence
The scalar measuring outward flux per unit volume of a vector field.
curl
The vector measuring circulation per unit area of a vector field.
Laplacian
The divergence of the gradient, the sum of second partial derivatives.
flux
The surface integral of the normal component of a vector field.
circulation
The line integral of a vector field around a closed loop.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Find the gradient of \(T = x^2y + e^{z}\) at \((1, 2, 0)\).

    Show answer

    \(\nabla T = (2xy,\ x^2,\ e^z) = (4, 1, 1)\).

  2. Compute the divergence and curl of \(\mathbf{v} = (xy,\ 2yz,\ 3zx)\).

    Show answer

    \(\nabla\cdot\mathbf{v} = y + 2z + 3x\); \(\nabla\times\mathbf{v} = (-2y,\ -3z,\ -x)\).

  3. Show that \(\nabla\times(\nabla T) = \mathbf{0}\) for \(T = x^2 y z\).

    Show answer

    \(\nabla T = (2xyz, x^2z, x^2y)\). Curl x component: \(\partial(x^2y)/\partial y - \partial(x^2z)/\partial z = x^2 - x^2 = 0\); the others vanish likewise.

  4. Find the Laplacian of \(T = x^2 + 2y^2 - 3z^2\).

    Show answer

    \(2 + 4 - 6 = 0\); this function satisfies Laplace's equation.

  5. Use Stokes' theorem to find the circulation of \(\mathbf{v} = (0, x^2, 0)\) around the unit square in the xy plane with corners (0,0) and (1,1), traversed counterclockwise.

    Show answer

    \((\nabla\times\mathbf{v})_z = 2x\); the flux is \(\int_0^1\int_0^1 2x\,dx\,dy = 1\).

  6. Explain why the flux of \(\nabla\times\mathbf{A}\) through a closed surface is zero, using the divergence theorem.

    Show answer

    It equals \(\int \nabla\cdot(\nabla\times\mathbf{A})\,d\tau = 0\), because the divergence of a curl vanishes.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz