Green's, Stokes' and Divergence Theorems
The three big theorems of vector calculus all say the same thing in different dimensions: adding up a derivative over a region gives the same result as adding up the original quantity over the region's boundary. They connect the circulation around a loop with the curl inside it, and the flow out of a closed surface with the divergence inside it. This lesson checks each one numerically and shows where programmers meet them, from the shoelace formula for polygon area to fluid solvers.
The pattern: boundary versus interior
The Fundamental Theorem of Calculus already has this shape: . The integral of a derivative over an interval equals the function evaluated on the interval's boundary (its two endpoints). The theorems below repeat the pattern one dimension up:
| Theorem | Interior (derivative) | Boundary (original) |
|---|---|---|
| Fundamental Theorem | over | |
| Gradient theorem | along a curve | |
| Green's theorem | over a plane region | around its edge |
| Stokes' theorem | over a surface | around its edge |
| Divergence theorem | over a solid | through its closed surface |
Green's theorem
Let be a simple closed curve, traversed counter-clockwise, enclosing the region . For :
The left side is the circulation of around (line integrals); the integrand on the right is the scalar curl. Intuition: split into tiny squares. Each square's circulation is about . Neighbouring squares traverse their shared edge in opposite directions, so all interior edges cancel and only the outer boundary is left.
import numpy as np
import sympy as sp
# F = <x*y - y**2, x**2 + 3*x> around the unit circle
P = lambda x, y: x * y - y**2
Q = lambda x, y: x**2 + 3 * x
n = 200_000
t = (np.arange(n) + 0.5) * 2 * np.pi / n
x, y, dx, dy = np.cos(t), np.sin(t), -np.sin(t), np.cos(t)
circulation = np.sum(P(x, y) * dx + Q(x, y) * dy) * 2 * np.pi / n
print("boundary integral ≈", round(float(circulation), 6))
X, Y, r, th = sp.symbols('x y r theta')
curl = sp.diff(X**2 + 3*X, X) - sp.diff(X*Y - Y**2, Y) # Q_x - P_y
area_integral = sp.integrate(curl.subs({X: r*sp.cos(th), Y: r*sp.sin(th)}) * r, (r, 0, 1), (th, 0, 2*sp.pi))
print("curl =", curl, " double integral =", area_integral, "≈", round(float(area_integral), 6))boundary integral ≈ 9.424778
curl = x + 2*y + 3 double integral = 3*pi ≈ 9.424778
Both sides agree on . The part of the curl is positive on one half of the disc and negative on the other, so it cancels, leaving the constant 3 times the disc's area .
Area from the boundary: the shoelace formula
Choose and so that ; then the double integral is the area. With , :
For a polygon with vertices listed counter-clockwise, the integral over each straight edge is exact and the formula becomes the shoelace formula:
It is used in GIS software, in games for polygon areas and centroids, and in computational geometry to test winding order: a negative result means the vertices are listed clockwise.
import numpy as np
def shoelace(pts):
x, y = np.asarray(pts, dtype=float).T
return 0.5 * np.sum(x * np.roll(y, -1) - np.roll(x, -1) * y)
square = [(0, 0), (2, 0), (2, 2), (0, 2)]
L_shape = [(0, 0), (3, 0), (3, 1), (1, 1), (1, 3), (0, 3)]
print("square :", shoelace(square))
print("L-shape :", shoelace(L_shape), " (3 + 2 = 5 unit squares)")
print("L-shape, CW :", shoelace(L_shape[::-1]))
k = np.arange(1000)
circle = np.column_stack([np.cos(2 * np.pi * k / 1000), np.sin(2 * np.pi * k / 1000)])
print("1000-gon :", round(float(shoelace(circle)), 6), " vs π =", round(np.pi, 6))square : 4.0
L-shape : 5.0 (3 + 2 = 5 unit squares)
L-shape, CW : -5.0
1000-gon : 3.141572 vs π = 3.141593
Stokes' theorem
Stokes' theorem lifts Green's theorem off the plane. For an oriented surface with boundary curve (oriented by the right-hand rule: with your thumb along the normal , your fingers curl in the direction of ):
The circulation around the rim equals the total curl flowing through the surface, and it is the same for every surface with that rim: a flat disc or a soap bubble stretched over the same wire loop. Green's theorem is the case where is flat and .
The divergence theorem
For a solid with closed boundary surface and outward normal :
The flux, the total flow out through the surface, equals the sum of all the sources and sinks inside. For (divergence 3) and the unit sphere, the right side is . The surface side can be checked numerically with the sphere parametrisation from Surfaces in 3D, where and the outward normal at a point is the point itself:
import numpy as np
F = lambda x, y, z: np.array([x + y**2, y * z, z**2]) # div F = 1 + z + 2z = 1 + 3z
n = 800
u = (np.arange(n) + 0.5) * 2 * np.pi / n # longitude
v = (np.arange(n) + 0.5) * np.pi / n # angle from the north pole
U, V = np.meshgrid(u, v)
x, y, z = np.cos(U) * np.sin(V), np.sin(U) * np.sin(V), np.cos(V)
normal = np.array([x, y, z]) # unit sphere: outward normal = position
flux = np.sum(np.sum(F(x, y, z) * normal, axis=0) * np.sin(V)) * (2 * np.pi / n) * (np.pi / n)
print("flux through the sphere ≈", round(float(flux), 6))
print("volume integral of div =", round(4 * np.pi / 3, 6), " (the 3z term integrates to 0)")flux through the sphere ≈ 4.18879
volume integral of div = 4.18879 (the 3z term integrates to 0)
In software
- Geometry. The shoelace formula (polygon area, centroid, winding order) is Green's theorem. Its 3D version computes the volume of a closed triangle mesh as a sum over triangles, the divergence theorem with . Game engines and 3D printers use it to get mass properties of meshes.
- Fluid and physics simulation. Finite-volume solvers update each grid cell by the flux through its faces, the divergence theorem applied cell by cell; it guarantees that whatever leaves one cell enters its neighbour, so mass is conserved exactly.
- Graphics. Stokes' theorem underlies methods that compute solid angles and inside/outside tests for meshes (generalised winding numbers).
- Physics. Gauss's law (flux of the electric field = enclosed charge) is the divergence theorem; Faraday's law of induction is Stokes' theorem.
Exercises
Exercise 1: Green's theorem on a square
Use Green's theorem to compute around the square , , counter-clockwise.
Answer
Solution
Exercise 2: Area of an ellipse
Use with , to find the area of an ellipse.
Answer
.
Solution
Exercise 3: Flux of a simple field
Find the outward flux of through the surface of the box .
Answer
Solution
By the divergence theorem, with , and the box has volume , so the flux is . Doing all six faces by hand gives the same: the face contributes , the face , the face , and the three faces through the origin contribute 0; .
Exercise 4: Polygon centroid (code)
Green's theorem also gives the centroid of a polygon: with the signed area from the shoelace formula and ,
Compute the centroid of the L-shape from the lesson and check it by splitting the shape into two rectangles.
Answer
.
Solution
Split the L into a rectangle (centroid , area 3) and a rectangle above its left end (centroid , area 2). The weighted average is .
import numpy as np
pts = np.array([(0, 0), (3, 0), (3, 1), (1, 1), (1, 3), (0, 3)], dtype=float)
x, y = pts.T
x1, y1 = np.roll(x, -1), np.roll(y, -1)
c = x * y1 - x1 * y
A = c.sum() / 2
print(A, round(float(((x + x1) * c).sum() / (6 * A)), 6), round(float(((y + y1) * c).sum() / (6 * A)), 6))5.0 1.1 1.1
Test yourself
Green's theorem relates a line integral around a closed curve to…
- the length of the curve
- a double integral of over the enclosed region
- the divergence through a surface
- the gradient at the centre
The shoelace formula gives −12 for a polygon. What does that tell you?
- The polygon is invalid
- The area is 12 and the vertices are listed clockwise
- The area is −12
- The polygon is self-intersecting
has divergence 2 everywhere. What is the outward flux of through a sphere of radius 3?
- 2