Flow Lab

Syllabus / Module 2: Multivariable & Vector Calculus

Lesson 2.10

Green, Stokes & the Divergence Theorem

The big three integral theorems: what's happening on the boundary equals what's happening inside. The bridge from integral conservation laws to PDEs.

Written byClaude Opus

🌊
Why this matters for simulation: The divergence theorem converts 'mass in a box changes by what flows through its walls' into the continuity equation.

By the end of this lesson you can…

  • Explain all three integral theorems with one picture: tiny interior circulations or fluxes cancel in pairs, leaving only the boundary.
  • State Green's, Stokes' and the divergence theorem precisely, including the orientation rules, and verify each one by computing both sides.
  • Choose the easier side of a theorem to evaluate a line, surface or volume integral, and use it to show surface-independence of curl flux.
  • Derive the continuity equation from the integral law of mass conservation using the divergence theorem.
  • Implement a conservative finite-volume update and explain why it conserves mass exactly.
On this page
·
  1. The one idea: interior pieces cancel
  2. Green’s theorem
  3. Circulation around a tiny rectangle
  4. Gluing squares together
  5. Worked example: both sides of Green’s theorem
  6. Area from the boundary
  7. The flux form of Green’s theorem
  8. Stokes’ theorem
  9. The rim is all that matters
  10. Worked example: hemisphere versus disk
  11. The divergence theorem
  12. Worked example: a cube
  13. Verifying it numerically
  14. From an integral law to a PDE: the continuity equation
  15. Finite-volume preview: update cells by boundary fluxes
  16. Worked example: one finite-volume step
  17. Key Takeaways

Every simulator you will ever trust has some bookkeeping it refuses to get wrong. A fluid solver must not create mass out of nothing, a heat solver must not leak energy, a traffic model must not spawn cars. All of these rules share one sentence: the amount of stuff inside a region changes only because of what crosses its boundary. In the last few lessons you built the pieces to say this in mathematics: the divergence measures outflow per unit volume (2.7 Divergence: Sources & Sinks), the curl measures circulation per unit area (2.8 Curl & Vorticity), and line and surface integrals add things up along curves and across surfaces (2.9 Line Integrals, Circulation & Flux). This lesson glues those pieces together into the three great integral theorems of vector calculus, and then uses them to turn a statement about boxes into a partial differential equation: the continuity equation that sits at the top of every fluid solver.

▶ Video Interior pieces cancel, only the boundary survivescaptions only
Transcript
  1. Green, Stokes, and the divergence theorem. Three famous theorems, and one idea underneath all of them.
  2. Every conservation law in physics has the same shape. The amount of stuff inside a region changes only because of what crosses its boundary.
  3. Today we turn that sentence into mathematics. The trick is to chop the region into tiny pieces and watch almost everything cancel.
  4. Start with one small square in a swirling flow. Walk counterclockwise around its four edges, adding up how much the flow pushes you along. That total is the circulation of the square.
  5. For a small square, the circulation is the curl at its center times its area. That is exactly what curl means: circulation per unit area.
  6. Now glue a second square beside it. Both are walked counterclockwise, so on the edge they share, the left square walks up while the right square walks down.
  7. The flow on that shared edge is the same for both squares, so their two contributions are equal and opposite. They cancel, and what's left is the circulation around the outer rectangle.
  8. Now chop a whole region into tiny squares, and give every one of them its own little counterclockwise loop.
  9. Every interior edge is shared by two squares, walked once in each direction. All of those cancel. Only the edges on the outside survive.
  10. Adding curl times area over all the squares builds the double integral of the curl. The surviving edges build the line integral around the boundary. That is Green's theorem.
  11. As the squares shrink, the staircase of surviving edges hugs the true boundary curve, and the approximation becomes exact.
  12. Nothing in that argument needed the squares to be flat. Tile a curved surface, like a net billowing in the wind, with tiny patches, and the same cancellation happens on every shared seam.
  13. That is Stokes' theorem. The flux of the curl through the surface equals the circulation around its rim.
  14. And here is a surprise. Puff the surface up or flatten it, but keep the same rim, and the answer never changes. Only the boundary matters.
  15. Now count flux instead of circulation. For a tiny box, the net flow out through its walls is the divergence times its volume.
  16. Stack a second box against it. Whatever leaves the left box through the shared wall enters the right box. Outflow for one is inflow for the other, so the shared wall cancels again.
  17. Fill a whole volume with boxes. Each interior wall is counted once as outflow and once as inflow, and only the outer walls remain.
  18. The total divergence inside equals the total flux out through the surface. That is the divergence theorem, also called Gauss's theorem.
  19. Here is why fluid people care. Take a fixed region of fluid. Its mass changes only by what flows out through its walls.
  20. The divergence theorem turns the wall integral into a volume integral. Now everything sits inside one integral, and it must vanish for every region we could pick.
  21. If an integral is zero over every region, however tiny, the integrand itself is zero. That is the continuity equation: a partial differential equation, derived from a statement about boxes. For constant density it says the velocity has zero divergence.
  22. Numerical methods can skip the last step and work with the boxes directly. A finite volume solver stores the amount of stuff in each cell, and moves it across the faces.
  23. Whatever leaves one cell enters its neighbor. So the total mass is conserved exactly, to the last digit, no matter how coarse the grid.
  24. Green, Stokes, and Gauss are one idea in three costumes. Add up tiny pieces, the interior cancels, and only the boundary survives. That's how integral laws become differential equations, and how finite volume solvers stay conservative.
  25. Next time: the divergence of the gradient, called the Laplacian. It hides inside diffusion, viscosity, and the pressure solve of every fluid simulator.

The one idea: interior pieces cancel

Recall the fundamental theorem of calculus:

∫abf′(x) dx=f(b)−f(a).\int_a^b f'(x)\,dx = f(b) - f(a).

The left side adds up a derivative over the inside of an interval. The right side only looks at the boundary, the two endpoints. Here’s a way to see why it works that will generalize. Chop [a,b][a, b] into little pieces [xk,xk+1][x_{k}, x_{k+1}]. On each piece, ∫xkxk+1f′ dx=f(xk+1)−f(xk)\int_{x_k}^{x_{k+1}} f'\,dx = f(x_{k+1}) - f(x_k). Now add them up:

(f(x1)−f(x0))+(f(x2)−f(x1))+⋯+(f(xN)−f(xN−1))=f(xN)−f(x0).\big(f(x_1) - f(x_0)\big) + \big(f(x_2) - f(x_1)\big) + \cdots + \big(f(x_N) - f(x_{N-1})\big) = f(x_N) - f(x_0).

Every interior point appears twice, once with a plus sign (as the right end of one piece) and once with a minus sign (as the left end of the next). They cancel. Only the outer endpoints survive. This is called a telescoping sum, and the three theorems in this lesson are exactly this telescope in two and three dimensions:

Theorem“Inside” (derivative)“Boundary”Pieces
Fundamental theorem of calculus∫f′ dx\int f'\,dxendpoint valuesintervals
Fundamental theorem for line integrals (2.9)∫C∇f⋅dr⃗\int_C \nabla f\cdot d\vec rff at the two ends of CCcurve segments
Green’s theorem∬(∇×F⃗)⋅k^ dA\iint (\nabla\times\vec F)\cdot\hat k\,dAcirculation around ∂D\partial Dlittle squares
Stokes’ theorem∬S(∇×F⃗)⋅n^ dS\iint_S (\nabla\times\vec F)\cdot\hat n\,dScirculation around ∂S\partial Slittle patches
Divergence theorem∭V∇⋅F⃗ dV\iiint_V \nabla\cdot\vec F\,dVflux out through ∂V\partial Vlittle boxes

The notation ∂D\partial D means “the boundary of DD”. Keep this table in mind: every theorem below is one row of it.

Green’s theorem

Circulation around a tiny rectangle

Take a 2D field F⃗=(P,Q)\vec F = (P, Q) and a small rectangle [x,x+Δx]×[y,y+Δy][x, x+\Delta x]\times[y, y+\Delta y]. Walk around it counterclockwise and compute the circulation ∮F⃗⋅dr⃗=∮P dx+Q dy\oint \vec F\cdot d\vec r = \oint P\,dx + Q\,dy, one edge at a time:

  • Bottom (going right, dy=0dy = 0): about P(x,y) ΔxP(x, y)\,\Delta x.
  • Top (going left): about −P(x,y+Δy) Δx-P(x, y+\Delta y)\,\Delta x.
  • Right (going up, dx=0dx = 0): about Q(x+Δx,y) ΔyQ(x+\Delta x, y)\,\Delta y.
  • Left (going down): about −Q(x,y) Δy-Q(x, y)\,\Delta y.

Group the top with the bottom and the right with the left:

∮□F⃗⋅dr⃗≈[Q(x+Δx,y)−Q(x,y)]Δy−[P(x,y+Δy)−P(x,y)]Δx≈(∂Q∂x−∂P∂y)Δx Δy.\oint_{\square} \vec F\cdot d\vec r \approx \big[Q(x+\Delta x, y) - Q(x,y)\big]\Delta y - \big[P(x, y+\Delta y) - P(x,y)\big]\Delta x \approx \left(\frac{\partial Q}{\partial x} - \frac{\partial P}{\partial y}\right)\Delta x\,\Delta y .

The bracket is exactly the scalar curl (∇×F⃗)⋅k^(\nabla\times\vec F)\cdot\hat k from 2.8 Curl & Vorticity. So, as promised there, the curl is circulation per unit area:

∮□F⃗⋅dr⃗≈(∇×F⃗)⋅k^  ΔA.\oint_{\square} \vec F\cdot d\vec r \approx (\nabla\times\vec F)\cdot\hat k\;\Delta A .

Gluing squares together

Now cover a region DD with a grid of tiny squares, each walked counterclockwise, and add up all their circulations. Look at an edge shared by two neighbouring squares. The left square walks it upward, the right square walks it downward. The field on that edge is the same for both, so the two line integrals are equal and opposite and cancel. Every interior edge cancels this way. What survives are the edges on the outside, and those, traversed in each square’s counterclockwise sense, trace out the boundary of DD counterclockwise. Meanwhile, the sum of (curl × area) over all the squares is a Riemann sum for a double integral (2.5 Multiple Integrals). Shrinking the squares gives:

“Region on your left” means counterclockwise around the outer edge. If DD has holes, the inner boundary curves are walked clockwise. That’s not a new rule: the little squares next to a hole still walk counterclockwise, and their surviving edges go clockwise around the hole.

◆ Interactive Chop a region into cells and watch the interior cancel

Loading simulation…

Things to try in the explorer:

  1. With Pull cells apart above zero, look at any shared edge. It carries a green arrow (positive contribution) from one cell and a red arrow of the same size (negative contribution) from its neighbour. The readout shows the interior edges summing to zero, up to round-off around 10−1610^{-16}.
  2. Slide Cells per side from 1 to 24. The sum of all cell circulations always equals the boundary circulation, to the last digit, because the cancellation is exact algebra. The double integral of the curl matches too, up to quadrature error.
  3. Pick the Vortex with a core on the Ring region. The curl is nearly zero everywhere in the ring, so the cells contribute almost nothing. The outer boundary has circulation about +2π+2\pi, the clockwise inner boundary about −2π-2\pi, and they cancel. The circulation is trapped in the vortex core inside the hole.
  4. Switch to the flux form. Now each cell’s arrows show outward flux. Neighbours share each face’s flux with opposite signs, because what leaves one cell enters the next.

Worked example: both sides of Green’s theorem

Verify Green’s theorem for F⃗=(−y3, x3)\vec F = (-y^3,\ x^3) on the unit disk.

Inside. P=−y3P = -y^3 and Q=x3Q = x^3, so Qx−Py=3x2+3y2=3r2Q_x - P_y = 3x^2 + 3y^2 = 3r^2. In polar coordinates (dA=r dr dθdA = r\,dr\,d\theta):

∬D3r2 dA=∫02π ⁣ ⁣∫013r2⋅r dr dθ=2π⋅34=3π2.\iint_D 3r^2\,dA = \int_0^{2\pi}\!\!\int_0^1 3r^2\cdot r\,dr\,d\theta = 2\pi\cdot\frac34 = \frac{3\pi}{2}.

Boundary. Parametrize the circle counterclockwise: x=cos⁡tx = \cos t, y=sin⁡ty = \sin t, so dx=−sin⁡t dtdx = -\sin t\,dt and dy=cos⁡t dtdy = \cos t\,dt. Then

P dx+Q dy=(−sin⁡3t)(−sin⁡t dt)+(cos⁡3t)(cos⁡t dt)=(sin⁡4t+cos⁡4t) dt.P\,dx + Q\,dy = (-\sin^3 t)(-\sin t\,dt) + (\cos^3 t)(\cos t\,dt) = (\sin^4 t + \cos^4 t)\,dt.

Using ∫02πsin⁡4t dt=∫02πcos⁡4t dt=3π4\int_0^{2\pi}\sin^4 t\,dt = \int_0^{2\pi}\cos^4 t\,dt = \tfrac{3\pi}{4}, the line integral is 3π4+3π4=3π2\tfrac{3\pi}{4} + \tfrac{3\pi}{4} = \tfrac{3\pi}{2}. ✓ Both sides agree.

Area from the boundary

Choose PP and QQ with Qx−Py=1Q_x - P_y = 1. Then the right side of Green’s theorem is just the area of DD. The symmetric choice P=−y/2P = -y/2, Q=x/2Q = x/2 gives

Area(D)=12∮∂D(x dy−y dx).\text{Area}(D) = \frac12 \oint_{\partial D} (x\,dy - y\,dx).

You can measure an area by walking around it. For a polygon with vertices (xk,yk)(x_k, y_k) this becomes the shoelace formula 12∑k(xkyk+1−xk+1yk)\tfrac12\sum_k (x_k y_{k+1} - x_{k+1} y_k), which graphics and mesh codes use constantly to compute cell areas and test orientation.

The flux form of Green’s theorem

Green’s theorem has a second personality. Apply it to the rotated field (−Q,P)(-Q, P) and you get a statement about flux across the boundary curve, with n^\hat n the outward unit normal:

∮∂DF⃗⋅n^ ds=∬D(∂P∂x+∂Q∂y)dA=∬D∇⋅F⃗ dA.\oint_{\partial D} \vec F\cdot\hat n\,ds = \iint_D \left(\frac{\partial P}{\partial x} + \frac{\partial Q}{\partial y}\right) dA = \iint_D \nabla\cdot\vec F\,dA .

This is the 2D divergence theorem. The tiny-box picture is the same: outflow from a small rectangle is about (∇⋅F⃗) ΔA(\nabla\cdot\vec F)\,\Delta A, and what leaves one cell through a shared face enters its neighbour.

✅ Concept check

In the proof of Green's theorem by little squares, why do the interior edges contribute nothing?

✅ Concept check

Use Green's theorem: what is the circulation of $\vec F = (-y, x)$ counterclockwise around the circle of radius 2? (Give a decimal.)

✏️ Pen & paperGreen's theorem on a square●○○

Let F⃗=(xy, x2)\vec F = (xy,\ x^2) and let DD be the unit square [0,1]×[0,1][0,1]\times[0,1], with boundary walked counterclockwise.

  1. Compute ∮∂DP dx+Q dy\oint_{\partial D} P\,dx + Q\,dy directly, edge by edge.
  2. Compute ∬D(Qx−Py) dA\iint_D (Q_x - P_y)\,dA and check that it agrees.
Show worked solution
  1. Walk counterclockwise starting at the origin.

    • Bottom, y=0y = 0, x:0→1x: 0\to1: dy=0dy = 0 and P=x⋅0=0P = x\cdot 0 = 0, so the contribution is 00.
    • Right, x=1x = 1, y:0→1y: 0\to1: dx=0dx = 0 and Q=1Q = 1, so the contribution is ∫011 dy=1\int_0^1 1\,dy = 1.
    • Top, y=1y = 1, x:1→0x: 1\to0: dy=0dy = 0 and P=xP = x, so the contribution is ∫10x dx=−12\int_1^0 x\,dx = -\tfrac12.
    • Left, x=0x = 0, y:1→0y: 1\to0: dx=0dx = 0 and Q=0Q = 0, so the contribution is 00.

    Total: 1−12=121 - \tfrac12 = \tfrac12.

  2. Qx−Py=2x−x=xQ_x - P_y = 2x - x = x, and ∬Dx dA=∫01 ⁣∫01x dx dy=12\iint_D x\,dA = \int_0^1\!\int_0^1 x\,dx\,dy = \tfrac12. ✓

✏️ Pen & paperArea of an ellipse by walking around it●○○

Use Area=12∮(x dy−y dx)\text{Area} = \tfrac12\oint (x\,dy - y\,dx) to find the area of the ellipse x2/a2+y2/b2≤1x^2/a^2 + y^2/b^2 \le 1.

Show worked solution

Parametrize the boundary counterclockwise: x=acos⁡tx = a\cos t, y=bsin⁡ty = b\sin t, 0≤t≤2π0 \le t \le 2\pi. Then dx=−asin⁡t dtdx = -a\sin t\,dt and dy=bcos⁡t dtdy = b\cos t\,dt, so

x dy−y dx=abcos⁡2t dt+absin⁡2t dt=ab dt.x\,dy - y\,dx = ab\cos^2 t\,dt + ab\sin^2 t\,dt = ab\,dt .

The area is 12∫02πab dt=πab\tfrac12\int_0^{2\pi} ab\,dt = \pi a b. With a=b=ra = b = r it reduces to πr2\pi r^2, as it should.

Stokes’ theorem

Nothing in the little-square argument needed the squares to be flat. Take an oriented surface SS in 3D, such as a soap film, a parachute, or a net billowing in the wind, and tile it with tiny curved patches. On each patch the circulation is about (∇×F⃗)⋅n^ ΔS(\nabla\times\vec F)\cdot\hat n\,\Delta S: the component of the curl along the patch’s normal, times its area. Neighbouring patches walk their shared seam in opposite directions, so the seams cancel and only the rim of the surface survives.

Green’s theorem is the special case of a flat surface in the xyxy-plane with n^=k^\hat n = \hat k.

The rim is all that matters

Here is the surprising consequence. The left side of Stokes’ theorem depends only on the boundary curve. So any two surfaces with the same rim, oriented the same way, have the same flux of curl through them. In the video, the net balloons up and flattens out while the rim stays fixed, and the answer never changes. In practice, this means you can often swap an awkward surface for a flat disk.

Worked example: hemisphere versus disk

Let F⃗=(−y, x, z)\vec F = (-y,\ x,\ z) and let SS be the upper unit hemisphere z=1−x2−y2z = \sqrt{1 - x^2 - y^2} with upward normal. Verify Stokes’ theorem.

Curl.

∇×F⃗=(∂z∂y−∂x∂z, ∂(−y)∂z−∂z∂x, ∂x∂x−∂(−y)∂y)=(0, 0, 2).\nabla\times\vec F = \left(\frac{\partial z}{\partial y} - \frac{\partial x}{\partial z},\ \frac{\partial(-y)}{\partial z} - \frac{\partial z}{\partial x},\ \frac{\partial x}{\partial x} - \frac{\partial(-y)}{\partial y}\right) = (0,\ 0,\ 2).

Boundary. The rim is the unit circle in the plane z=0z = 0. Upward normal plus the right-hand rule means we go counterclockwise seen from above: r⃗(t)=(cos⁡t,sin⁡t,0)\vec r(t) = (\cos t, \sin t, 0). Then F⃗=(−sin⁡t,cos⁡t,0)\vec F = (-\sin t, \cos t, 0) and r⃗ ′(t)=(−sin⁡t,cos⁡t,0)\vec r\,'(t) = (-\sin t, \cos t, 0), so F⃗⋅r⃗ ′=1\vec F\cdot\vec r\,' = 1 and the circulation is 2π2\pi.

Surface, the easy way. By surface-independence, replace the hemisphere with the flat unit disk, whose normal is k^\hat k. The flux of (0,0,2)(0,0,2) through it is 2×π=2π2\times\pi = 2\pi. ✓ (The hard way, with the hemisphere’s parametrization, also gives 2π2\pi: ∬2 nz dS\iint 2\,n_z\,dS is twice the area of the shadow the hemisphere casts on the xyxy-plane.)

✅ Concept check

A hemisphere $S_1$ and a flat disk $S_2$ share the same boundary circle and are oriented consistently (both normals pointing up). The flux of $\nabla\times\vec F$ through $S_1$ is $5$. What is the flux of $\nabla\times\vec F$ through $S_2$?

✏️ Pen & paperStokes on a paraboloid cap●●○

Let SS be the part of the paraboloid z=4−x2−y2z = 4 - x^2 - y^2 with z≥0z \ge 0, normal pointing upward (outward from the dome). Let F⃗=(yz, −xz, x2+y2)\vec F = (yz,\ -xz,\ x^2 + y^2).

  1. Compute ∇×F⃗\nabla\times\vec F.
  2. Use Stokes’ theorem to find ∬S(∇×F⃗)⋅n^ dS\iint_S (\nabla\times\vec F)\cdot\hat n\,dS by computing a line integral instead.
  3. Check your answer by integrating over the flat disk x2+y2≤4x^2 + y^2 \le 4, z=0z = 0.
Show worked solution
  1. With P=yzP = yz, Q=−xzQ = -xz, R=x2+y2R = x^2 + y^2: ∇×F⃗=(Ry−Qz, Pz−Rx, Qx−Py)=(2y+x, y−2x, −z−z)=(x+2y, y−2x, −2z).\nabla\times\vec F = (R_y - Q_z,\ P_z - R_x,\ Q_x - P_y) = (2y + x,\ y - 2x,\ -z - z) = (x + 2y,\ y - 2x,\ -2z).
  2. The rim is the circle x2+y2=4x^2 + y^2 = 4 in the plane z=0z = 0, traversed counterclockwise seen from above (right-hand rule with upward normal): r⃗(t)=(2cos⁡t,2sin⁡t,0)\vec r(t) = (2\cos t, 2\sin t, 0). On the rim z=0z = 0, so F⃗=(0,0,4)\vec F = (0, 0, 4), while r⃗ ′=(−2sin⁡t,2cos⁡t,0)\vec r\,' = (-2\sin t, 2\cos t, 0) has no zz-component. So F⃗⋅r⃗ ′=0\vec F\cdot\vec r\,' = 0 and the circulation is 00. The flux of the curl through the paraboloid is 00.
  3. On the disk, n^=k^\hat n = \hat k and z=0z = 0, so (∇×F⃗)⋅k^=−2z=0(\nabla\times\vec F)\cdot\hat k = -2z = 0. The flux is 00. ✓ Two very different surfaces give the same answer because they share a rim.

The divergence theorem

Now count flux instead of circulation, and use boxes instead of patches. For a tiny box Δx×Δy×Δz\Delta x\times\Delta y\times\Delta z, the same pairing argument as the rectangle above works face by face. The flux out of the right face minus the flux in through the left face is about ∂F1∂xΔx⋅Δy Δz\frac{\partial F_1}{\partial x}\Delta x\cdot\Delta y\,\Delta z, and similarly in yy and zz. So

net outward flux of a tiny box≈(∂F1∂x+∂F2∂y+∂F3∂z)ΔV=(∇⋅F⃗) ΔV.\text{net outward flux of a tiny box} \approx \left(\frac{\partial F_1}{\partial x} + \frac{\partial F_2}{\partial y} + \frac{\partial F_3}{\partial z}\right)\Delta V = (\nabla\cdot\vec F)\,\Delta V .

That is the definition of divergence from 2.7 Divergence: Sources & Sinks, read backwards. Fill a solid VV with boxes. Every interior wall is shared by two boxes, and whatever leaves one enters the other, so those fluxes cancel. Only the outer walls remain.

In words: total source strength inside = net flux out through the skin. An incompressible flow (∇⋅u⃗=0\nabla\cdot\vec u = 0) has zero net flux through every closed surface. Whatever flows into a region must flow out.

Worked example: a cube

Verify the divergence theorem for F⃗=(xy, yz, zx)\vec F = (xy,\ yz,\ zx) on the unit cube [0,1]3[0,1]^3.

Inside. ∇⋅F⃗=y+z+x\nabla\cdot\vec F = y + z + x, and

∭[0,1]3(x+y+z) dV=12+12+12=32.\iiint_{[0,1]^3} (x + y + z)\,dV = \tfrac12 + \tfrac12 + \tfrac12 = \tfrac32 .

Skin. There are six faces, with outward normals ±ı^,±ȷ^,±k^\pm\hat\imath, \pm\hat\jmath, \pm\hat k.

  • x=1x = 1, n^=ı^\hat n = \hat\imath: F⃗⋅n^=xy=y\vec F\cdot\hat n = xy = y, and ∬y dy dz=12\iint y\,dy\,dz = \tfrac12.
  • x=0x = 0, n^=−ı^\hat n = -\hat\imath: F⃗⋅n^=−xy=0\vec F\cdot\hat n = -xy = 0.
  • y=1y = 1: F⃗⋅n^=yz=z\vec F\cdot\hat n = yz = z, giving 12\tfrac12. And y=0y = 0 gives 00.
  • z=1z = 1: F⃗⋅n^=zx=x\vec F\cdot\hat n = zx = x, giving 12\tfrac12. And z=0z = 0 gives 00.

The total is 32\tfrac32. ✓

✏️ Pen & paperFlux through a sphere●●○

Let F⃗=(x3, y3, z3)\vec F = (x^3,\ y^3,\ z^3) and let SS be the unit sphere, oriented outward. Find ∯SF⃗⋅n^ dS\oiint_S \vec F\cdot\hat n\,dS.

Show worked solution

The surface integral directly is messy, so use the divergence theorem. ∇⋅F⃗=3x2+3y2+3z2=3r2\nabla\cdot\vec F = 3x^2 + 3y^2 + 3z^2 = 3r^2. In spherical shells (volume element 4πr2 dr4\pi r^2\,dr):

∭r≤13r2 dV=∫013r2⋅4πr2 dr=12π⋅15=12π5.\iiint_{r\le1} 3r^2\,dV = \int_0^1 3r^2\cdot 4\pi r^2\,dr = 12\pi\cdot\frac15 = \frac{12\pi}{5}.

Check: on the unit sphere n^=(x,y,z)\hat n = (x,y,z), so F⃗⋅n^=x4+y4+z4\vec F\cdot\hat n = x^4 + y^4 + z^4. By symmetry each term has the same integral, and the average of x4x^4 over the unit sphere is 15\tfrac15, so ∯x4 dS=4π5\oiint x^4\,dS = \tfrac{4\pi}{5}. Three of them give 12π5\tfrac{12\pi}{5}. ✓

✏️ Pen & paperThe point source and the fine print●●●

Let F⃗=r⃗4π∥r⃗∥3\vec F = \dfrac{\vec r}{4\pi\|\vec r\|^3} with r⃗=(x,y,z)\vec r = (x, y, z). This is the field of a point source of unit strength (and, up to constants, gravity or the electric field of a point charge).

  1. Show that ∇⋅F⃗=0\nabla\cdot\vec F = 0 everywhere except the origin.
  2. Compute the flux of F⃗\vec F out of the sphere of radius RR.
  3. The divergence is zero, yet the flux is not. Explain why this does not contradict the divergence theorem, and find the flux out of any closed surface enclosing the origin.
Show worked solution
  1. Write r=∥r⃗∥r = \|\vec r\|, so F1=x/(4πr3)F_1 = x/(4\pi r^3). Using ∂r/∂x=x/r\partial r/\partial x = x/r: ∂∂xxr3=1r3−3x2r5.\frac{\partial}{\partial x}\frac{x}{r^3} = \frac{1}{r^3} - \frac{3x^2}{r^5}. Adding the yy and zz versions gives 3r3−3(x2+y2+z2)r5=0\frac{3}{r^3} - \frac{3(x^2 + y^2 + z^2)}{r^5} = 0 for r≠0r \ne 0.
  2. On the sphere, n^=r⃗/R\hat n = \vec r/R, so F⃗⋅n^=R24πR3⋅R=14πR2\vec F\cdot\hat n = \dfrac{R^2}{4\pi R^3\cdot R} = \dfrac{1}{4\pi R^2}. Multiplying by the area 4πR24\pi R^2 gives a flux of 11, for every RR.
  3. The theorem needs F⃗\vec F to be continuously differentiable on the whole solid. This F⃗\vec F blows up at the origin, which lies inside the ball, so the theorem doesn’t apply there. All the “source” is concentrated at a single point (a Dirac delta, an infinitely sharp spike of total weight 11: ∇⋅F⃗=δ(r⃗)\nabla\cdot\vec F = \delta(\vec r)). For any closed surface SS around the origin, apply the theorem to the region between SS and a tiny sphere around the origin. F⃗\vec F is smooth there with zero divergence, so the outward flux through SS equals the outward flux through the small sphere, which is 11. This is Gauss’s law: the flux through a closed surface measures the total source enclosed, no matter the shape.

Verifying it numerically

Theorems like these are also great test oracles for code. If your discrete divergence and your boundary fluxes disagree, you have a bug. Here’s Green’s theorem checked numerically. Change the field and see that the two sides still match.

🐍 PythonGreen's theorem, numerically
⌘/Ctrl + Enter

Now your turn, in 3D. Implement both sides of the divergence theorem for a rectangular box using the midpoint rule.

🧪 Coding exerciseExercise: the divergence theorem on a box
⌘/Ctrl + Enter

From an integral law to a PDE: the continuity equation

Here’s the payoff for fluids. Let ρ(x⃗,t)\rho(\vec x, t) be the density of a fluid moving with velocity u⃗(x⃗,t)\vec u(\vec x, t). Fix a region VV in space, a control volume. Its mass is M(t)=∭Vρ dVM(t) = \iiint_V \rho\,dV. The mass flux ρu⃗\rho\vec u is mass per unit area per unit time, so ρ u⃗⋅n^ dS\rho\,\vec u\cdot\hat n\,dS is the rate at which mass leaves through a patch of the wall. With no sources or sinks, mass only changes by crossing the wall:

ddt∭Vρ dV=−∯∂Vρ u⃗⋅n^ dS.\frac{d}{dt}\iiint_V \rho\,dV = -\oiint_{\partial V} \rho\,\vec u\cdot\hat n\,dS .

This integral conservation law is the physics. It’s true for any region, smooth or not, and it doesn’t need any derivatives of ρ\rho or u⃗\vec u. That will matter later, for shocks.

Now do two things.

  1. VV is fixed, so the time derivative moves inside: ddt∭Vρ dV=∭V∂ρ∂t dV\frac{d}{dt}\iiint_V\rho\,dV = \iiint_V \frac{\partial\rho}{\partial t}\,dV.
  2. Apply the divergence theorem to the wall integral: ∯∂Vρu⃗⋅n^ dS=∭V∇⋅(ρu⃗) dV\oiint_{\partial V}\rho\vec u\cdot\hat n\,dS = \iiint_V \nabla\cdot(\rho\vec u)\,dV.

Now everything is one volume integral:

∭V(∂ρ∂t+∇⋅(ρu⃗))dV=0for every region V.\iiint_V \left(\frac{\partial\rho}{\partial t} + \nabla\cdot(\rho\vec u)\right) dV = 0 \qquad \text{for every region } V.

If an integral vanishes over every region, the (continuous) integrand vanishes. Suppose it were positive at some point. By continuity it would be positive on a small ball around that point, and the integral over that ball would be positive, a contradiction. Therefore:

The same recipe derives almost every PDE in this course. Pick a conserved quantity with density qq and flux J⃗\vec J, write “rate of change inside = −(flux out) + sources”, and apply the divergence theorem:

∂q∂t+∇⋅J⃗=s.\frac{\partial q}{\partial t} + \nabla\cdot\vec J = s .

Then supply a constitutive law for the flux. Heat flowing down temperature gradients, J⃗=−k∇T\vec J = -k\nabla T (Fourier’s law), turns this into the heat equation Tt=α∇2TT_t = \alpha\nabla^2 T, the subject of 9.2 The Heat (Diffusion) Equation. The operator ∇⋅∇=∇2\nabla\cdot\nabla = \nabla^2 that appears is the Laplacian, coming up next in 2.11 The Laplacian & Vector Identities. Momentum, with flux ρu⃗u⃗+pI−(viscous stress)\rho\vec u\vec u + p I - (\text{viscous stress}), gives the Navier–Stokes equations.

✅ Concept check

A steady 3D flow has velocity $\vec u = (x,\ -2y,\ z)$. What is the net volume flux $\oiint \vec u\cdot\hat n\,dS$ out of the sphere of radius 3?

✏️ Pen & paperDeriving a 1D conservation law●●○

Cars on a one-lane road have density ρ(x,t)\rho(x,t) (cars per km) and flux q(x,t)q(x,t) (cars per hour passing point xx). There are no on- or off-ramps.

  1. Write the integral conservation law for the stretch a≤x≤ba \le x \le b.
  2. Use the fundamental theorem of calculus (the 1D divergence theorem) to derive a PDE.
  3. If an on-ramp adds cars at rate s(x,t)s(x,t) (cars per km per hour), how does the PDE change?
  4. In a diffusion problem the flux is q=−k ρxq = -k\,\rho_x. What PDE results?
Show worked solution
  1. The number of cars in [a,b][a,b] changes only by cars entering at aa and leaving at bb: ddt∫abρ dx=q(a,t)−q(b,t).\frac{d}{dt}\int_a^b \rho\,dx = q(a,t) - q(b,t).
  2. q(a)−q(b)=−∫abqx dxq(a) - q(b) = -\int_a^b q_x\,dx, and since aa and bb are fixed, ddt∫abρ dx=∫abρt dx\frac{d}{dt}\int_a^b \rho\,dx = \int_a^b \rho_t\,dx. So ∫ab(ρt+qx) dx=0\int_a^b(\rho_t + q_x)\,dx = 0 for every interval, which gives ρt+qx=0\rho_t + q_x = 0.
  3. Add ∫abs dx\int_a^b s\,dx to the right side of the integral law. Then ρt+qx=s\rho_t + q_x = s.
  4. ρt+(−kρx)x=0\rho_t + (-k\rho_x)_x = 0, i.e. ρt=k ρxx\rho_t = k\,\rho_{xx}: the heat (diffusion) equation. The boundary-flux picture explains why the second derivative appears: the net flux into a short interval is the difference of the gradients at its two ends.

Finite-volume preview: update cells by boundary fluxes

Numerical methods can skip the last step of the derivation and discretize the integral law directly. Chop space into cells, and store the cell average ρˉi\bar\rho_i rather than point values. Applied to cell i=[xi−1/2,xi+1/2]i = [x_{i-1/2}, x_{i+1/2}] in 1D, the integral law is exact:

ddt(ρˉi Δx)=Fi−12−Fi+12,\frac{d}{dt}\big(\bar\rho_i\,\Delta x\big) = F_{i-\frac12} - F_{i+\frac12},

where Fi+1/2F_{i+1/2} is the flux through the face between cells ii and i+1i+1. The only approximation is how you estimate the face flux from nearby cell averages. With forward Euler in time:

ρˉi n+1=ρˉi n−ΔtΔx(Fi+12−Fi−12).\bar\rho_i^{\,n+1} = \bar\rho_i^{\,n} - \frac{\Delta t}{\Delta x}\left(F_{i+\frac12} - F_{i-\frac12}\right).

For advection at speed uu, the simplest face flux is upwind: take density from the side the flow comes from, so Fi+1/2=u ρˉiF_{i+1/2} = u\,\bar\rho_i if u>0u \gt 0 and u ρˉi+1u\,\bar\rho_{i+1} if u<0u \lt 0.

Now add up the update over all cells. Each face flux appears exactly twice: subtracted from the cell on its left and added to the cell on its right. The sum telescopes, just like the interior edges in Green’s theorem:

∑iρˉi n+1Δx=∑iρˉi nΔx−Δt(Fright end−Fleft end).\sum_i \bar\rho_i^{\,n+1}\Delta x = \sum_i \bar\rho_i^{\,n}\Delta x - \Delta t\left(F_{\text{right end}} - F_{\text{left end}}\right).

Total mass changes only by the fluxes at the domain’s ends. With periodic boundaries those match too, and mass is conserved to round-off, whatever the flux formula, grid size or time step. That’s why the method is called conservative. Accuracy is a separate question.

Worked example: one finite-volume step

Four periodic cells with Δx=1\Delta x = 1 hold ρˉ=(0,4,2,0)\bar\rho = (0, 4, 2, 0). Take u=1u = 1, Δt=0.5\Delta t = 0.5, and the upwind flux.

The face fluxes are Fi+1/2=ρˉiF_{i+1/2} = \bar\rho_i: F1/2=0F_{1/2} = 0, F3/2=4F_{3/2} = 4, F5/2=2F_{5/2} = 2, F7/2=0F_{7/2} = 0. By periodicity F−1/2=F7/2=0F_{-1/2} = F_{7/2} = 0. With Δt/Δx=0.5\Delta t/\Delta x = 0.5:

  • cell 0: 0−0.5(0−0)=00 - 0.5(0 - 0) = 0
  • cell 1: 4−0.5(4−0)=24 - 0.5(4 - 0) = 2
  • cell 2: 2−0.5(2−4)=32 - 0.5(2 - 4) = 3
  • cell 3: 0−0.5(0−2)=10 - 0.5(0 - 2) = 1

The new state is (0,2,3,1)(0, 2, 3, 1), and the total is still 66. The bump moved right and smeared a little. That smearing is the numerical diffusion of the upwind scheme. The method is stable when the CFL number ∣u∣Δt/Δx≤1|u|\Delta t/\Delta x \le 1 (10.5 Wave Equation Numerics & the CFL Condition). That’s the same kind of step-size limit you met for forward Euler on stiff ODEs.

Implement it yourself:

🧪 Coding exerciseExercise: a conservative 1D finite-volume step
⌘/Ctrl + Enter

Conservation is not automatic. It comes from writing the update in flux form. Here’s a flow whose speed varies in space, so fluid bunches up where it slows down. Mathematically, (uρ)x=uρx+uxρ(u\rho)_x = u\rho_x + u_x\rho, but a scheme that discretizes only u ρxu\,\rho_x (the “advective form”) quietly drops the compression term, so it does not conserve mass in general (and here it misses the pile-up entirely):

🐍 PythonFlux form vs. advective form in a compressing flow
⌘/Ctrl + Enter

The flux-form density piles up where the flow slows (near x=0.75x = 0.75), exactly as real traffic or gas would, and the total stays at 11. The advective form keeps ρ≡1\rho \equiv 1 forever. Its total happens to stay at 11 because a uniform start is a fixed point of that form, but it misses the pile-up and transports ρ\rho as a passive label, not as mass. Both are “correct” discretizations of some PDE. Only one of them is the conservation law.

Key Takeaways

  • One idea, three theorems: chop a region into tiny pieces, and the contributions from shared interior edges or faces cancel in pairs, leaving only the boundary. That’s the fundamental theorem of calculus in higher dimensions.
  • Green: ∮∂DP dx+Q dy=∬D(Qx−Py) dA\oint_{\partial D} P\,dx + Q\,dy = \iint_D (Q_x - P_y)\,dA, with the region on your left. Its flux form, ∮F⃗⋅n^ ds=∬∇⋅F⃗ dA\oint \vec F\cdot\hat n\,ds = \iint \nabla\cdot\vec F\,dA, is the 2D divergence theorem.
  • Stokes: circulation around the rim equals the flux of curl through any surface spanning it (right-hand-rule orientation). Only the boundary matters.
  • Divergence (Gauss): net outward flux through a closed surface equals the integral of the divergence inside. Incompressible flows have zero net flux through every closed surface.
  • Use whichever side is easier. Watch the hypotheses: singularities inside the region (like a point source) break the theorems, and that is where concentrated sources hide.
  • Integral law → PDE: “change inside = −(flux out)” plus the divergence theorem, for every region, gives the continuity equation ρt+∇⋅(ρu⃗)=0\rho_t + \nabla\cdot(\rho\vec u) = 0, and the same recipe gives heat, diffusion and Navier–Stokes.
  • Finite volume discretizes the integral law directly: update cell averages by face-flux differences. The sum telescopes, so mass is conserved exactly, and this is the discrete twin of the cancellation in Green’s theorem.
Finished this lesson?

Mark it complete to track your progress (saved in this browser). Reading every section does this for you.