Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 68 ยท Multivariable Calculus

Vector Calculus: Divergence, Curl, Line Integrals

Learn vector fields, divergence, curl, line and surface integrals, and the theorems of Green, Stokes and Gauss, with sympy and numpy checks you can run.

So far each point in space carried a single number: a temperature, a height, a pressure. Many physical quantities carry a direction as well: wind, water flow, an electric field, the force on a satellite. A function that attaches a vector to every point is a vector field, and vector calculus is the toolkit for asking what such a field does: does it spread out, does it swirl, how much work does it do along a path, how much of it flows through a surface?

This lesson builds on vectors, partial derivatives, the gradient and multiple integrals. The surprising result is that a handful of theorems (Green, Stokes, Gauss) turn integrals over a region into integrals over its boundary, which is why they sit under Maxwell's equations and fluid dynamics.

Vector fields

A vector field in 3D is written F(x, y, z) = (P, Q, R), three component functions. In 2D it is (P, Q). Two small examples:

  • F = (-y, x) rotates every point counter-clockwise around the origin. Far from the origin the arrows are longer.
  • F = (x, y, z) points straight away from the origin, like a radial explosion.

You can picture a field by drawing arrows on a grid, which is what plt.quiver does. The gradient of a scalar function f is a vector field grad f = (f_x, f_y, f_z), and fields that arise this way are called conservative. Gravity near a mountain is the standard example: the work done depends only on start and end heights.

flowchart LR
    S["scalar field f"] -- "gradient" --> V["vector field F"]
    V -- "divergence" --> D["scalar: sources and sinks"]
    V -- "curl" --> C["vector: local rotation"]

Gradient, divergence and curl are the three basic differential operators of vector calculus.

Divergence and curl

Divergence measures how much a field spreads out from a point, as a single number:

div F = P_x + Q_y + R_z

Positive divergence means a source (fluid is being created or expanding); negative means a sink. A field with div F = 0 everywhere is called incompressible: whatever flows into a small box flows out again.

Curl measures local rotation, as a vector:

curl F = (R_y - Q_z, P_z - R_x, Q_x - P_y)

Imagine a tiny paddle wheel placed in the flow. The curl points along the axis the wheel spins about (right-hand rule), and its length is twice the spin rate. For 2D fields only the third component, Q_x - P_y, is non-zero.

A useful fact: the curl of any gradient is zero, curl(grad f) = 0, and the divergence of any curl is zero, div(curl F) = 0. A field with zero curl on a simple region is conservative.

Python Divergence and curl with sympy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice what the first two fields teach: pure rotation has zero divergence but non-zero curl, while the radial field has divergence 3 and zero curl.

Line integrals and work

A line integral adds up a field along a curve. If a particle moves along a path r(t) for t from a to b in a force field F, the work done is

W = integral from a to b of F(r(t)) . r'(t) dt

You parametrize the path, substitute it into the field, take the dot product with the velocity r'(t), and integrate an ordinary single-variable integral. The result does not depend on how fast you travel, but it does depend on the direction: reversing the path flips the sign.

For a conservative field F = grad f, the fundamental theorem for line integrals says the work equals f(end) - f(start), whatever the path. That is the same idea as the fundamental theorem of calculus in higher dimensions.

Python Path dependence of work

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The conservative field gives 1 along both paths (it equals xy at (1, 1) minus xy at the origin), but the swirling field gives different answers for different paths. Around a closed loop, a conservative field always gives 0; the swirling field gives 2*pi around the unit circle. Non-zero circulation around a loop is the signature of curl.

Surface integrals and flux

A surface integral of a scalar adds up a quantity over the area of a surface. The more important one is flux: the amount of a field passing through a surface, per unit time, if F is a flow velocity:

flux = double integral of F . n dS

where n is the unit normal vector pointing in the direction you count as positive. For a closed surface such as a sphere or box, n conventionally points outward, so positive flux means net outflow. To compute it for a flat face, F . n is just one component of F, and dS is a small piece of area. For a general parametrized surface r(u, v) you integrate F . (r_u x r_v) du dv.

The big theorems

Each theorem relates an integral over a region to an integral over its boundary.

Theorem Region Boundary Statement
Green flat region D closed curve C circulation of F around C equals the integral of Q_x - P_y over D
Stokes surface S in 3D curve C bounding it circulation of F around C equals the flux of curl F through S
Divergence (Gauss) solid E closed surface S outward flux of F through S equals the integral of div F over E

Green's theorem is Stokes' theorem restricted to the plane. All three say the same thing as the fundamental theorem of calculus: the total of a derivative over the inside equals the value of the thing on the boundary. Physically, the divergence theorem says that net outflow through the wall equals the total of all sources inside, which is exactly the conservation-law reasoning behind the continuity equation and Gauss's law.

flowchart TB
    FTC["FTC: integral of f' over [a, b] = f(b) - f(a)"]
    FTC --> G["Green: curl over region = loop integral"]
    G --> S["Stokes: curl flux = loop integral in 3D"]
    FTC --> D["Divergence: div over solid = outward flux"]

The same pattern, "inside derivative equals boundary value", appears in every dimension.

Checking the theorems numerically

Verifying a theorem both ways is the best defence against sign errors and a nice way to build trust. Below, Green's theorem is checked on the unit disk using polar coordinates, and the divergence theorem on the unit cube for F = (x**2, y**2, z**2) by computing the six face fluxes. A finite-difference curl and divergence at one point shows that the formulas also work with plain numbers.

Python Green, Gauss and finite differences

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both sides agree in each theorem: 2*pi for Green and 3 for the cube. At the point (1, 2, 3) the exact divergence of (xy, yz, zx) is y + z + x = 6, and the curl is (-y, -z, -x) = (-2, -3, -1); the finite differences land on those values to about six decimals. In the cube, only the faces at x = 1, y = 1 and z = 1 contribute flux, 1 each.

When a surface or boundary integral looks painful, ask whether the theorems let you swap it for the other side. Flux through a messy closed surface of a field with constant divergence is just that constant times the enclosed volume.

Where you meet these in engineering

  • Electromagnetism: Gauss's law is the divergence theorem applied to the electric field, and Faraday's and Ampere's laws are Stokes' theorem applied to E and B.
  • Fluid mechanics: incompressible flow means div v = 0; the curl of the velocity is the vorticity.
  • Heat flow: the heat equation comes from the divergence theorem applied to heat flux, as you will see in partial differential equations.
  • Machine learning: gradients of losses are vector fields, and the Jacobian trace is a divergence.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. The vector field F = (-y, x, 0) has which divergence and curl?

    div F = 0 + 0 + 0 = 0. The z-component of the curl is Q_x - P_y = 1 - (-1) = 2, so the curl is (0, 0, 2).
  2. 2. What does it mean if curl F = 0 on a simple region?

    On a simply connected region, zero curl means F is the gradient of some potential, so the work around any closed loop is 0.
  3. 3. The divergence theorem says outward flux through a closed surface equals which quantity?

    Gauss's theorem converts a surface flux integral into a volume integral of the divergence.
  4. 4. You reverse the direction of travel along a path. What happens to the line integral of a vector field?

    F . r' changes sign when the direction flips, so the line integral is negated. A scalar line integral over arc length would not change.
  5. 5. Which identity is always true for a smooth scalar function f?

    The curl of a gradient is the zero vector because mixed partial derivatives commute. div(grad f) is the Laplacian, which is generally not zero.

Summary

  • A vector field attaches a vector to every point; grad f gives a conservative one.
  • Divergence P_x + Q_y + R_z measures spreading (sources and sinks); curl measures local rotation.
  • A line integral F . dr is the work done along a path; for conservative fields it depends only on the endpoints.
  • Flux is the surface integral of F . n.
  • Green, Stokes and the divergence theorem convert an integral over a region into one over its boundary, extending the fundamental theorem of calculus.
  • sympy and finite differences let you verify both sides of each theorem.

Next: Ordinary Differential Equations: First Steps.