TExcelTAD
Home › Calculus Essentials › Green's, Stokes' and Divergence Theorems

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: ∫abf′(x) dx=f(b)−f(a)\int_a^b f'(x)\,dx = f(b) - f(a). 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 ∫abf′ dx\int_a^b f'\,dx over [a,b][a, b] f(b)−f(a)f(b) - f(a)
Gradient theorem ∫C∇φ⋅dr\int_C \nabla\varphi \cdot dr along a curve φ(end)−φ(start)\varphi(\text{end}) - \varphi(\text{start})
Green's theorem ∬D(Qx−Py) dA\iint_D (Q_x - P_y)\,dA over a plane region ∮CP dx+Q dy\oint_C P\,dx + Q\,dy around its edge
Stokes' theorem ∬S(∇×F)⋅n dS\iint_S (\nabla \times F) \cdot n\,dS over a surface ∮CF⋅dr\oint_C F \cdot dr around its edge
Divergence theorem ∭E∇⋅F dV\iiint_E \nabla \cdot F\,dV over a solid ∯SF⋅n dS\oiint_S F \cdot n\,dS through its closed surface

Green's theorem

Let CC be a simple closed curve, traversed counter-clockwise, enclosing the region DD. For F=⟨P,Q⟩F = \langle P, Q \rangle:

∮CP dx+Q dy=∬D(∂Q∂x−∂P∂y)dA\oint_C P\,dx + Q\,dy = \iint_D \left(\frac{\partial Q}{\partial x} - \frac{\partial P}{\partial y}\right) dA

The left side is the circulation of FF around CC (line integrals); the integrand on the right is the scalar curl. Intuition: split DD into tiny squares. Each square's circulation is about curl×area\text{curl} \times \text{area}. Neighbouring squares traverse their shared edge in opposite directions, so all interior edges cancel and only the outer boundary is left.

python · try it yourself
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 3π3\pi. The x+2yx + 2y 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 π\pi.

Area from the boundary: the shoelace formula

Choose PP and QQ so that Qx−Py=1Q_x - P_y = 1; then the double integral is the area. With P=−y/2P = -y/2, Q=x/2Q = x/2:

Area(D)=12∮C(x dy−y dx)\text{Area}(D) = \frac{1}{2} \oint_C (x\,dy - y\,dx)

For a polygon with vertices (x1,y1),…,(xn,yn)(x_1, y_1), \ldots, (x_n, y_n) listed counter-clockwise, the integral over each straight edge is exact and the formula becomes the shoelace formula:

Area=12∑(xiyi+1−xi+1yi)(with xn+1=x1, yn+1=y1)\text{Area} = \frac{1}{2} \sum (x_i y_{i+1} - x_{i+1} y_i) \qquad (\text{with } x_{n+1} = x_1,\ y_{n+1} = y_1)

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.

python · try it yourself
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 SS with boundary curve CC (oriented by the right-hand rule: with your thumb along the normal nn, your fingers curl in the direction of CC):

∮CF⋅dr=∬S(∇×F)⋅n dS\oint_C F \cdot dr = \iint_S (\nabla \times F) \cdot n\,dS

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 SS is flat and n=⟨0,0,1⟩n = \langle 0, 0, 1 \rangle.

The divergence theorem

For a solid EE with closed boundary surface SS and outward normal nn:

∯SF⋅n dS=∭E(∇⋅F) dV\oiint_S F \cdot n\,dS = \iiint_E (\nabla \cdot F)\,dV

The flux, the total flow out through the surface, equals the sum of all the sources and sinks inside. For F=⟨x,y,z⟩F = \langle x, y, z \rangle (divergence 3) and the unit sphere, the right side is 3⋅(4π/3)=4π3 \cdot (4\pi/3) = 4\pi. The surface side can be checked numerically with the sphere parametrisation from Surfaces in 3D, where dS=sin⁡v du dvdS = \sin v\,du\,dv and the outward normal at a point is the point itself:

python · try it yourself
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 F=⟨x,y,z⟩/3F = \langle x, y, z \rangle/3. 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 ∮C(3y dx+5x dy)\oint_C (3y\,dx + 5x\,dy) around the square 0≤x≤20 \le x \le 2, 0≤y≤20 \le y \le 2, counter-clockwise.

Answer
Solution
P=3y,Q=5x,Qx−Py=5−3=2∬D2 dA=2⋅(area 4)=8\begin{aligned} &P = 3y,\quad Q = 5x, \qquad Q_x - P_y = 5 - 3 = 2 \\ &\iint_D 2\,dA = 2 \cdot (\text{area } 4) = 8 \end{aligned}

Exercise 2: Area of an ellipse

Use Area=12∮(x dy−y dx)\text{Area} = \frac{1}{2} \oint (x\,dy - y\,dx) with x=acos⁡tx = a\cos t, y=bsin⁡ty = b\sin t to find the area of an ellipse.

Answer

πab\pi ab.

Solution
x dy−y dx=(acos⁡t)(bcos⁡t) dt−(bsin⁡t)(−asin⁡t) dt=ab dtArea=12∫02πab dt=πab\begin{aligned} x\,dy - y\,dx &= (a\cos t)(b\cos t)\,dt - (b\sin t)(-a\sin t)\,dt = ab\,dt \\ \text{Area} &= \frac{1}{2} \int_0^{2\pi} ab\,dt = \pi ab \end{aligned}

Exercise 3: Flux of a simple field

Find the outward flux of F=⟨2x,−y,4z⟩F = \langle 2x, -y, 4z \rangle through the surface of the box [0,1]×[0,2]×[0,3][0, 1] \times [0, 2] \times [0, 3].

Answer
Solution

By the divergence theorem, flux=∭div⁡F dV\text{flux} = \iiint \operatorname{div} F\,dV with div⁡F=2−1+4=5\operatorname{div} F = 2 - 1 + 4 = 5, and the box has volume 1⋅2⋅3=61 \cdot 2 \cdot 3 = 6, so the flux is 5⋅6=305 \cdot 6 = 30. Doing all six faces by hand gives the same: the x=1x = 1 face contributes 2⋅6=122 \cdot 6 = 12, the y=2y = 2 face −2⋅3=−6-2 \cdot 3 = -6, the z=3z = 3 face 12⋅2=2412 \cdot 2 = 24, and the three faces through the origin contribute 0; 12−6+24=3012 - 6 + 24 = 30.

Exercise 4: Polygon centroid (code)

Green's theorem also gives the centroid of a polygon: with the signed area AA from the shoelace formula and ci=xiyi+1−xi+1yic_i = x_i y_{i+1} - x_{i+1} y_i,

xˉ=16A∑(xi+xi+1) ci,yˉ=16A∑(yi+yi+1) ci\bar{x} = \frac{1}{6A} \sum (x_i + x_{i+1})\,c_i, \qquad \bar{y} = \frac{1}{6A} \sum (y_i + y_{i+1})\,c_i

Compute the centroid of the L-shape from the lesson and check it by splitting the shape into two rectangles.

Answer

(1.1,1.1)(1.1, 1.1).

Solution

Split the L into a 3×13 \times 1 rectangle (centroid (1.5,0.5)(1.5, 0.5), area 3) and a 1×21 \times 2 rectangle above its left end (centroid (0.5,2)(0.5, 2), area 2). The weighted average is ((3⋅1.5+2⋅0.5)/5,(3⋅0.5+2⋅2)/5)=(5.5/5,5.5/5)=(1.1,1.1)((3 \cdot 1.5 + 2 \cdot 0.5)/5, (3 \cdot 0.5 + 2 \cdot 2)/5) = (5.5/5, 5.5/5) = (1.1, 1.1).

python · try it yourself
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…

  1. the length of the curve
  2. a double integral of Qx−PyQ_x - P_y over the enclosed region
  3. the divergence through a surface
  4. the gradient at the centre

The shoelace formula gives −12 for a polygon. What does that tell you?

  1. The polygon is invalid
  2. The area is 12 and the vertices are listed clockwise
  3. The area is −12
  4. The polygon is self-intersecting

FF has divergence 2 everywhere. What is the outward flux of FF through a sphere of radius 3?

  1. 2
  2. 36π36\pi
  3. 72π72\pi
  4. 18π18\pi