Chapter 19
25 min read
Section 171 of 353

The Divergence Theorem

Vector Calculus

Learning Objectives

By the end of this section, you will be able to:

  1. State and apply the Divergence Theorem to convert surface integrals over closed surfaces into volume integrals
  2. Understand the physical meaning of the theorem: total outward flux equals the sum of all interior sources minus sinks
  3. Compute flux through complicated surfaces by instead evaluating simpler volume integrals of divergence
  4. Connect the Divergence Theorem to conservation laws in physics and the broader family of Stokes-type theorems
  5. Apply the theorem to problems in electromagnetism, fluid dynamics, heat transfer, and understand its relevance to diffusion models in machine learning
Why This Matters: The Divergence Theorem is one of the crown jewels of vector calculus. It provides the mathematical foundation for conservation laws throughout physics—conservation of mass, charge, energy, and momentum all rely on this theorem. In electromagnetism, it transforms Gauss's Law from an integral statement to Maxwell's first equation. In machine learning, the underlying mathematics governs how probability flows in diffusion models and normalizing flows.

The Big Picture

Imagine a closed surface—like a sphere or a cube—enclosing a region of space where fluid is flowing. The Divergence Theorem answers a fundamental question: How is the net outward flux through the boundary related to what's happening inside?

🌊 The Surface Perspective

The flux oiintSmathbfFcdotdmathbfS\\oiint_S \\mathbf{F} \\cdot d\\mathbf{S} measures how much of the vector field passes outward through the surface. Positive flux means net outward flow; negative means net inward flow.

🔮 The Volume Perspective

The divergence nablacdotmathbfF\\nabla \\cdot \\mathbf{F} at each point measures local expansion (positive) or contraction (negative). Integrating divergence over the volume counts all sources and sinks inside.

The Core Insight

The Divergence Theorem says these two perspectives are equivalent:

oiintSmathbfFcdotdmathbfS=iiintVnablacdotmathbfF,dV\\oiint_S \\mathbf{F} \\cdot d\\mathbf{S} = \\iiint_V \\nabla \\cdot \\mathbf{F} \\, dV

Net outward flux through the boundary = Total sources minus sinks inside.

This is the 3D generalization of the 2D Divergence Theorem (the flux form of Green's Theorem) that we studied earlier. Just as Green's Theorem relates a line integral around a closed curve to a double integral over the enclosed region, the Divergence Theorem relates a surface integral over a closed surface to a triple integral over the enclosed volume.

DimensionTheoremRelates
1DFundamental Theorem of Calculus[a,b]f(x)dx=f(b)f(a)\int_{[a,b]} f'(x)\,dx = f(b) - f(a)
2DGreen's Theorem (flux form)CFnds=RFdA\oint_C \mathbf{F} \cdot \mathbf{n}\,ds = \iint_R \nabla \cdot \mathbf{F}\,dA
3DDivergence TheoremSFdS=VFdV\oiint_S \mathbf{F} \cdot d\mathbf{S} = \iiint_V \nabla \cdot \mathbf{F}\,dV

Historical Context

The Divergence Theorem is also known as Gauss's Theorem, Ostrogradsky's Theorem, or the Gauss-Ostrogradsky Theorem, reflecting its independent discovery by several mathematicians.

Carl Friedrich Gauss (1777-1855)

Gauss used forms of this theorem in his work on electromagnetism and gravitation. His name is most associated with it in physics, where Gauss's Law for electric fields is a direct application:

oiintSmathbfEcdotdmathbfS=fracQtextenclosedvarepsilon0\\oiint_S \\mathbf{E} \\cdot d\\mathbf{S} = \\frac{Q_{\\text{enclosed}}}{\\varepsilon_0}

This says the electric flux through any closed surface equals the enclosed charge divided by the permittivity of free space.

Mikhail Ostrogradsky (1801-1862)

Ostrogradsky, a Russian mathematician, provided one of the first rigorous proofs of the theorem in 1826, three years before Green published his theorem. His work established the mathematical foundations that connect surface integrals to volume integrals.

George Green (1793-1841)

Green independently developed related results in his 1828 essay. The 2D version we studied earlier (Green's Theorem) bears his name, and the 3D generalization follows naturally from the same principles.

Unified Mathematical Framework

All these theorems—the Fundamental Theorem of Calculus, Green's Theorem, Stokes' Theorem, and the Divergence Theorem—are special cases of the Generalized Stokes Theorem from differential geometry. They all express the same deep principle: integrating a derivative over a region equals integrating the original function over the boundary.


Statement of the Divergence Theorem

The Divergence Theorem (Gauss's Theorem)

Let VV be a bounded solid region in mathbbR3\\mathbb{R}^3 with piecewise smooth boundary surface SS oriented by outward-pointing unit normals. If mathbfF\\mathbf{F} is a vector field with continuous first partial derivatives on an open region containing VV, then:

oiintSmathbfFcdotdmathbfS=iiintVnablacdotmathbfF,dV\\oiint_S \\mathbf{F} \\cdot d\\mathbf{S} = \\iiint_V \\nabla \\cdot \\mathbf{F} \\, dV

Or equivalently, using component notation:

oiintS(P,dydz+Q,dzdx+R,dxdy)=iiintVleft(fracpartialPpartialx+fracpartialQpartialy+fracpartialRpartialzright)dV\\oiint_S (P\\,dydz + Q\\,dzdx + R\\,dxdy) = \\iiint_V \\left( \\frac{\\partial P}{\\partial x} + \\frac{\\partial Q}{\\partial y} + \\frac{\\partial R}{\\partial z} \\right) dV

Key Components

SymbolMeaning
VVThe solid region (volume) enclosed by the surface
SSThe closed boundary surface of V (oriented outward)
F=(P,Q,R)\mathbf{F} = (P, Q, R)The vector field defined on V and S
F=Px+Qy+Rz\nabla \cdot \mathbf{F} = \frac{\partial P}{\partial x} + \frac{\partial Q}{\partial y} + \frac{\partial R}{\partial z}The divergence of F
dS=ndSd\mathbf{S} = \mathbf{n}\,dSOutward normal vector times surface area element
FdS\mathbf{F} \cdot d\mathbf{S}The flux through an infinitesimal surface patch

Orientation Matters

The surface SS must be oriented with outward-pointing normals. If normals point inward, the surface integral changes sign. The theorem requires the "outward" convention for the equality to hold.

Interactive: Flux Through Closed Surfaces

Explore how the flux of a vector field through a closed surface relates to the divergence inside. Watch how sources (positive divergence) contribute positive flux and sinks (negative divergence) contribute negative flux:

Loading flux surface integral visualization...

Deep Intuition: Why the Theorem Works

The key insight is telescoping cancellation—the same principle that powers all the Stokes-type theorems.

The Brick Decomposition Argument

Imagine dividing the volume VV into many tiny rectangular boxes (like 3D pixels). For each tiny box, the flux through its six faces approximately equals the divergence times the volume of the box (by the definition of divergence as flux per unit volume).

Now sum over all boxes:

  1. For interior faces (shared between adjacent boxes), the flux contributions cancel: what flows out of one box flows into its neighbor with opposite sign
  2. Only the boundary faces (on surface SS) survive the cancellation
  3. The sum of all divergences times tiny volumes approaches iiintVnablacdotmathbfF,dV\\iiint_V \\nabla \\cdot \\mathbf{F} \\, dV
  4. The surviving boundary contributions become oiintSmathbfFcdotdmathbfS\\oiint_S \\mathbf{F} \\cdot d\\mathbf{S}

Visual Analogy: Money Flow in a Building

Imagine each room in a building has people passing money to adjacent rooms. The divergence at each room is the net money created (positive) or destroyed (negative) there. The flux through the building's exterior walls is the net money leaving the building.

All the internal transfers between rooms cancel out—what one room gives, the neighbor receives. Only the money created inside (sources) and money destroyed inside (sinks) affect what crosses the boundary. This is exactly what the Divergence Theorem says!

Divergence as Source Density

Recall that the divergence measures the local rate of expansion (or contraction) of the vector field:

nablacdotmathbfF=limtextvolumeto0fractextfluxoutoftinyvolumetextvolume\\nabla \\cdot \\mathbf{F} = \\lim_{\\text{volume} \\to 0} \\frac{\\text{flux out of tiny volume}}{\\text{volume}}
  • nablacdotmathbfF>0\\nabla \\cdot \\mathbf{F} > 0: Point acts as a source (field expands outward)
  • nablacdotmathbfF<0\\nabla \\cdot \\mathbf{F} < 0: Point acts as a sink (field contracts inward)
  • nablacdotmathbfF=0\\nabla \\cdot \\mathbf{F} = 0: Incompressible at that point (what flows in must flow out)

Interactive: Divergence as Source Density

Visualize how divergence acts as a source/sink density. See how positive divergence creates outward flow and negative divergence creates inward flow:

Loading divergence source/sink demo...

The Divergence Operator Revisited

The divergence of a vector field mathbfF=(P,Q,R)\\mathbf{F} = (P, Q, R) is the scalar field:

nablacdotmathbfF=fracpartialPpartialx+fracpartialQpartialy+fracpartialRpartialz\\nabla \\cdot \\mathbf{F} = \\frac{\\partial P}{\\partial x} + \\frac{\\partial Q}{\\partial y} + \\frac{\\partial R}{\\partial z}

Important Examples

Vector Field F\mathbf{F}Divergence F\nabla \cdot \mathbf{F}Interpretation
(x,y,z)(x, y, z)33Uniform source everywhere
(x2,0,0)(x^2, 0, 0)2x2xSource increasing with x
(y,x,0)(-y, x, 0)00Pure rotation, no sources
r/r3\mathbf{r}/|\mathbf{r}|^300 (except origin)Inverse-square, point source at origin
(xy,yz,zx)(x-y, y-z, z-x)1+1+1=31+1+1=3Constant divergence 3

Geometric Interpretation

Think of the divergence as measuring how much a vector field "spreads out" at each point. If you place a small balloon at a point:

  • Positive divergence: The balloon would expand (field pushing outward)
  • Negative divergence: The balloon would shrink (field pushing inward)
  • Zero divergence: The balloon might deform but maintains constant volume

Worked Examples

Example 1: Radial Field and Sphere

Problem: Verify the Divergence Theorem for mathbfF=(x,y,z)\\mathbf{F} = (x, y, z) and the sphere x2+y2+z2=a2x^2 + y^2 + z^2 = a^2.

Step 1: Compute the surface integral directly

On the sphere, the outward unit normal is mathbfn=frac1a(x,y,z)\\mathbf{n} = \\frac{1}{a}(x, y, z).

mathbfFcdotmathbfn=frac1a(x2+y2+z2)=fraca2a=a\\mathbf{F} \\cdot \\mathbf{n} = \\frac{1}{a}(x^2 + y^2 + z^2) = \\frac{a^2}{a} = a

The flux is oiintSmathbfFcdotdmathbfS=acdot(textsurfacearea)=acdot4pia2=4pia3\\oiint_S \\mathbf{F} \\cdot d\\mathbf{S} = a \\cdot (\\text{surface area}) = a \\cdot 4\\pi a^2 = 4\\pi a^3

Step 2: Compute the volume integral

nablacdotmathbfF=fracpartialxpartialx+fracpartialypartialy+fracpartialzpartialz=1+1+1=3\\nabla \\cdot \\mathbf{F} = \\frac{\\partial x}{\\partial x} + \\frac{\\partial y}{\\partial y} + \\frac{\\partial z}{\\partial z} = 1 + 1 + 1 = 3

iiintV3,dV=3cdotfrac43pia3=4pia3\\iiint_V 3 \\, dV = 3 \\cdot \\frac{4}{3}\\pi a^3 = 4\\pi a^3

Result: Both sides equal 4pia34\\pi a^3. ✓

Example 2: Simplifying a Complicated Surface

Problem: Find the flux of mathbfF=(x3,y3,z3)\\mathbf{F} = (x^3, y^3, z^3) through the surface of the cube 0lex,y,zle10 \\le x, y, z \\le 1.

Direct approach (hard): Compute six surface integrals, one for each face.

Divergence Theorem approach (easier):

nablacdotmathbfF=3x2+3y2+3z2\\nabla \\cdot \\mathbf{F} = 3x^2 + 3y^2 + 3z^2

iiintV(3x2+3y2+3z2),dV=3int01int01int01(x2+y2+z2),dxdydz\\iiint_V (3x^2 + 3y^2 + 3z^2) \\, dV = 3\\int_0^1\\int_0^1\\int_0^1 (x^2 + y^2 + z^2) \\, dxdydz

=3left[frac13+frac13+frac13right]=3= 3\\left[\\frac{1}{3} + \\frac{1}{3} + \\frac{1}{3}\\right] = 3

The Divergence Theorem reduced six surface integrals to one simple volume integral!

Example 3: Zero Divergence Field

Problem: Show that the flux of mathbfF=(y2,x2,z2)\\mathbf{F} = (y^2, x^2, z^2) through any closed surface is determined only by the enclosed volume.

nablacdotmathbfF=fracpartialpartialx(y2)+fracpartialpartialy(x2)+fracpartialpartialz(z2)=0+0+2z\\nabla \\cdot \\mathbf{F} = \\frac{\\partial}{\\partial x}(y^2) + \\frac{\\partial}{\\partial y}(x^2) + \\frac{\\partial}{\\partial z}(z^2) = 0 + 0 + 2z

The flux is iiintV2z,dV\\iiint_V 2z \\, dV, which depends on the distribution of volume relative to the z-axis.

Interactive: Verify the Theorem

Explore different vector fields and regions. Verify that the surface integral and volume integral give the same result:

Loading Divergence Theorem visualization...

Real-World Applications

Physics: Maxwell's Equations and Fluid Dynamics

Gauss's Law for Electricity: The Divergence Theorem transforms the integral form of Gauss's Law into its differential form.

Integral Form:

oiintSmathbfEcdotdmathbfS=fracQtextenclosedvarepsilon0=frac1varepsilon0iiintVrho,dV\\oiint_S \\mathbf{E} \\cdot d\\mathbf{S} = \\frac{Q_{\\text{enclosed}}}{\\varepsilon_0} = \\frac{1}{\\varepsilon_0}\\iiint_V \\rho \\, dV

Applying Divergence Theorem:

iiintVnablacdotmathbfE,dV=frac1varepsilon0iiintVrho,dV\\iiint_V \\nabla \\cdot \\mathbf{E} \\, dV = \\frac{1}{\\varepsilon_0}\\iiint_V \\rho \\, dV

Differential Form (Maxwell's 1st Equation):

nablacdotmathbfE=fracrhovarepsilon0\\nabla \\cdot \\mathbf{E} = \\frac{\\rho}{\\varepsilon_0}

Gauss's Law for Magnetism: Since there are no magnetic monopoles, magnetic field lines always form closed loops:

oiintSmathbfBcdotdmathbfS=0quadRightarrowquadnablacdotmathbfB=0\\oiint_S \\mathbf{B} \\cdot d\\mathbf{S} = 0 \\quad \\Rightarrow \\quad \\nabla \\cdot \\mathbf{B} = 0

Continuity Equation (Fluid Dynamics): Conservation of mass states that the rate of change of density plus the divergence of mass flux equals zero:

fracpartialrhopartialt+nablacdot(rhomathbfv)=0\\frac{\\partial \\rho}{\\partial t} + \\nabla \\cdot (\\rho \\mathbf{v}) = 0

For incompressible flow (rho\\rho constant), this simplifies to nablacdotmathbfv=0\\nabla \\cdot \\mathbf{v} = 0.

Engineering: Conservation Laws

The Divergence Theorem provides the mathematical foundation for all conservation laws in engineering:

FieldConservation LawDivergence Interpretation
Heat TransferTt=α2T\frac{\partial T}{\partial t} = \alpha \nabla^2 THeat flux divergence = rate of temperature change
Mass Transportct+(cv)=D2c\frac{\partial c}{\partial t} + \nabla \cdot (c\mathbf{v}) = D \nabla^2 cAdvection-diffusion equation
Stress Analysisσ+b=0\nabla \cdot \boldsymbol{\sigma} + \mathbf{b} = 0Force balance from stress divergence
ElectrostaticsE=ρ/ε0\nabla \cdot \mathbf{E} = \rho / \varepsilon_0Electric field divergence = charge density

Connection to Machine Learning

The Divergence Theorem and the concept of divergence appear in several important areas of modern machine learning:

1. Diffusion Models

Score-based diffusion models (like DDPM, Score SDE) work by learning the "score function" nablalogp(x)\\nabla \\log p(x). The training objective involves divergence:

The score matching objective uses the trace of the Hessian, which is exactly the divergence of the gradient:

texttr(nabla2logp)=nablacdot(nablalogp)\\text{tr}(\\nabla^2 \\log p) = \\nabla \\cdot (\\nabla \\log p)

2. Normalizing Flows

In continuous normalizing flows, probability density transforms according to:

fracdlogp(z(t))dt=nablacdotf(z(t),t)\\frac{d \\log p(z(t))}{dt} = -\\nabla \\cdot f(z(t), t)

The divergence of the flow field determines how probability mass concentrates or spreads as the flow evolves.

3. Physics-Informed Neural Networks (PINNs)

When training neural networks to satisfy PDEs involving conservation laws (like the Navier-Stokes equations), the divergence appears explicitly. The network must learn to satisfy nablacdotmathbfv=0\\nabla \\cdot \\mathbf{v} = 0 for incompressible flows.

4. The Connection to Probability

Conservation of Probability

Probability must sum to 1. If we think of probability as a "fluid," the Divergence Theorem guarantees that probability is conserved as it flows through parameter space.

Fokker-Planck Equation

The evolution of probability distributions in SDEs is governed by the Fokker-Planck equation, which involves the divergence of the probability current.


The Unified View: FTC, Green, Stokes, Divergence

All the major theorems of vector calculus share the same structure: they relate an integral over a boundary to an integral of a derivative over the enclosed region.

The Generalized Stokes Theorem

\\int_{\\partial \\Omega} \\omega = \\int_\\Omega d\\omega

"The integral of a differential form over the boundary of a region equals the integral of its exterior derivative over the region."

TheoremDimensionBoundary Ω\partial \OmegaRegion Ω\OmegaDerivative
FTC1DPoints {a,b}\text{Points } \{a, b\}Interval [a,b]\text{Interval } [a, b]ff' (ordinary)
Green's (circ.)2DCurve C\text{Curve } CRegion R\text{Region } RQxPy\frac{\partial Q}{\partial x} - \frac{\partial P}{\partial y} (curl)
Stokes'3DCurve C\text{Curve } CSurface S\text{Surface } S×F\nabla \times \mathbf{F} (curl)
Divergence3DSurface S\text{Surface } SVolume V\text{Volume } VF\nabla \cdot \mathbf{F} (divergence)
The Deep Unity: All these theorems say that local information (derivatives) integrated over a region gives global information about the boundary. The total change inside determines what crosses the boundary.

Python Implementation

Here's a complete Python implementation that numerically verifies the Divergence Theorem and visualizes vector fields with their divergence:

Divergence Theorem Verification and Visualization
🐍python
5The Divergence Theorem Statement

The theorem equates the total outward flux through a closed surface S to the volume integral of the divergence inside. It transforms a 2D surface integral to a 3D volume integral.

22Surface Integral Setup

For a sphere, we use spherical parameterization. The outward normal n at any point equals the radial unit vector. The flux integral becomes ∯ F · n dS.

37Surface Element

The surface element dS = r² sin(φ) dθ dφ comes from the Jacobian of the spherical parameterization. Combined with F · n, this gives the integrand for flux.

58Volume Integral Setup

We integrate div(F) over the enclosed volume using spherical coordinates. The volume element dV = r² sin(φ) dr dθ dφ accounts for the geometry.

110Radial Field Example

F = (x, y, z) is a source field with constant divergence 3. The flux through a sphere of radius R is 4πR³ × 3 = 4πR³, matching the volume × div(F).

125Inverse-Square Field

The inverse-square field F = r/|r|³ has zero divergence away from the origin. But the singularity at r=0 creates a delta function, which is Gauss&apos;s Law in electrostatics!

195 lines without explanation
1import numpy as np
2from scipy import integrate
3import matplotlib.pyplot as plt
4from mpl_toolkits.mplot3d import Axes3D
5
6def verify_divergence_theorem_sphere(F, div_F, radius=1.0, n_points=50):
7    """
8    Verify the Divergence Theorem for a vector field over a sphere.
9
10    The theorem states:
11        ∯_S F · dS = ∭_V div(F) dV
12
13    Left side: Flux through the surface (surface integral)
14    Right side: Total divergence inside (volume integral)
15
16    Args:
17        F: Vector field function (x, y, z) -> (Fx, Fy, Fz)
18        div_F: Divergence function (x, y, z) -> scalar
19        radius: Radius of the sphere
20        n_points: Number of integration points
21
22    Returns:
23        surface_integral, volume_integral: Both sides of the theorem
24    """
25    # SURFACE INTEGRAL: ∯_S F · n dS
26    # For a sphere, the outward normal at (x, y, z) is (x, y, z) / r
27
28    def surface_integrand(theta, phi):
29        """
30        Compute F · n * |dS| for spherical parameterization.
31
32        Sphere parameterization:
33            x = r sin(φ) cos(θ)
34            y = r sin(φ) sin(θ)
35            z = r cos(φ)
36
37        Surface element: dS = r² sin(φ) dθ dφ
38        Outward normal: n = (sin(φ)cos(θ), sin(φ)sin(θ), cos(φ))
39        """
40        r = radius
41        x = r * np.sin(phi) * np.cos(theta)
42        y = r * np.sin(phi) * np.sin(theta)
43        z = r * np.cos(phi)
44
45        # Evaluate vector field
46        Fx, Fy, Fz = F(x, y, z)
47
48        # Outward normal (unit vector on sphere)
49        nx = np.sin(phi) * np.cos(theta)
50        ny = np.sin(phi) * np.sin(theta)
51        nz = np.cos(phi)
52
53        # F · n * surface element
54        F_dot_n = Fx * nx + Fy * ny + Fz * nz
55        dS = r**2 * np.sin(phi)  # Surface area element
56
57        return F_dot_n * dS
58
59    # Integrate over θ ∈ [0, 2π] and φ ∈ [0, π]
60    surface_result, _ = integrate.dblquad(
61        surface_integrand,
62        0, np.pi,           # φ limits
63        lambda phi: 0,      # θ lower
64        lambda phi: 2*np.pi # θ upper
65    )
66
67    # VOLUME INTEGRAL: ∭_V div(F) dV
68    # Using spherical coordinates: dV = r² sin(φ) dr dθ dφ
69
70    def volume_integrand(r_val, theta, phi):
71        """Compute div(F) * dV in spherical coordinates."""
72        x = r_val * np.sin(phi) * np.cos(theta)
73        y = r_val * np.sin(phi) * np.sin(theta)
74        z = r_val * np.cos(phi)
75
76        divergence = div_F(x, y, z)
77        dV = r_val**2 * np.sin(phi)  # Volume element
78
79        return divergence * dV
80
81    # Integrate over r ∈ [0, radius], θ ∈ [0, 2π], φ ∈ [0, π]
82    volume_result, _ = integrate.tplquad(
83        volume_integrand,
84        0, np.pi,                    # φ limits
85        lambda phi: 0,               # θ lower
86        lambda phi: 2 * np.pi,       # θ upper
87        lambda phi, theta: 0,        # r lower
88        lambda phi, theta: radius    # r upper
89    )
90
91    return surface_result, volume_result
92
93def visualize_divergence_field(F, div_F, extent=2):
94    """
95    Visualize a 3D vector field and its divergence.
96    Color indicates divergence: red = positive (source), blue = negative (sink).
97    """
98    # Create grid
99    x = np.linspace(-extent, extent, 8)
100    y = np.linspace(-extent, extent, 8)
101    z = np.linspace(-extent, extent, 8)
102    X, Y, Z = np.meshgrid(x, y, z)
103
104    # Flatten for quiver
105    X_flat, Y_flat, Z_flat = X.flatten(), Y.flatten(), Z.flatten()
106
107    # Evaluate field and divergence
108    vectors = np.array([F(xi, yi, zi) for xi, yi, zi in
109                       zip(X_flat, Y_flat, Z_flat)])
110    U, V, W = vectors[:, 0], vectors[:, 1], vectors[:, 2]
111
112    divergences = np.array([div_F(xi, yi, zi) for xi, yi, zi in
113                           zip(X_flat, Y_flat, Z_flat)])
114
115    # Normalize for visualization
116    magnitudes = np.sqrt(U**2 + V**2 + W**2)
117    U_norm = U / (magnitudes + 0.001)
118    V_norm = V / (magnitudes + 0.001)
119    W_norm = W / (magnitudes + 0.001)
120
121    # Plot
122    fig = plt.figure(figsize=(12, 5))
123
124    # Left: Vector field colored by divergence
125    ax1 = fig.add_subplot(121, projection='3d')
126    colors = plt.cm.coolwarm((divergences - divergences.min()) /
127                             (divergences.max() - divergences.min() + 0.001))
128    ax1.quiver(X_flat, Y_flat, Z_flat, U_norm, V_norm, W_norm,
129               colors=colors, length=0.3, normalize=True, alpha=0.8)
130    ax1.set_xlabel('x')
131    ax1.set_ylabel('y')
132    ax1.set_zlabel('z')
133    ax1.set_title('Vector Field F\n(colored by divergence)')
134
135    # Right: Divergence scalar field (slice at z=0)
136    ax2 = fig.add_subplot(122)
137    x_2d = np.linspace(-extent, extent, 50)
138    y_2d = np.linspace(-extent, extent, 50)
139    X_2d, Y_2d = np.meshgrid(x_2d, y_2d)
140
141    div_2d = np.array([[div_F(xi, yi, 0) for xi in x_2d] for yi in y_2d])
142
143    im = ax2.contourf(X_2d, Y_2d, div_2d, levels=20, cmap='coolwarm')
144    ax2.set_xlabel('x')
145    ax2.set_ylabel('y')
146    ax2.set_title('Divergence at z=0 slice\n(red = source, blue = sink)')
147    plt.colorbar(im, ax=ax2, label='div(F)')
148    ax2.set_aspect('equal')
149
150    plt.tight_layout()
151    plt.show()
152
153# EXAMPLE 1: Radial field F = (x, y, z)
154# This is the 3D analogue of the 2D radial field
155def F_radial(x, y, z):
156    return (x, y, z)
157
158def div_radial(x, y, z):
159    # div(F) = ∂x/∂x + ∂y/∂y + ∂z/∂z = 1 + 1 + 1 = 3
160    return 3.0
161
162print("Example 1: Radial field F = (x, y, z)")
163print("div(F) = 3 (constant everywhere)")
164print()
165
166surface, volume = verify_divergence_theorem_sphere(F_radial, div_radial, radius=2.0)
167print(f"Surface integral ∯_S F · dS = {surface:.6f}")
168print(f"Volume integral ∭_V div(F) dV = {volume:.6f}")
169print(f"Expected: 4πr³ = {4/3 * np.pi * 2**3 * 3:.6f}")
170print()
171
172# EXAMPLE 2: Inverse-square field (like gravity/electrostatics)
173# F = r/|r|³ has div(F) = 0 everywhere except origin
174def F_inverse_square(x, y, z):
175    r = np.sqrt(x**2 + y**2 + z**2)
176    if r < 0.01:  # Avoid singularity
177        return (0, 0, 0)
178    return (x/r**3, y/r**3, z/r**3)
179
180def div_inverse_square(x, y, z):
181    # div(r/|r|³) = 0 for r ≠ 0
182    # But there's a delta function at origin!
183    return 0.0
184
185print("Example 2: Inverse-square field F = r/|r|³")
186print("div(F) = 0 away from origin (but singularity at r=0)")
187print()
188
189# For a sphere NOT containing origin, flux should be 0
190surface2, volume2 = verify_divergence_theorem_sphere(
191    F_inverse_square, div_inverse_square, radius=1.0
192)
193print(f"Sphere centered at origin, r=1:")
194print(f"  Surface integral = {surface2:.6f}")
195print(f"  Volume integral = {volume2:.6f}")
196print("  (Disagreement due to singularity at origin!)")
197print("  This is Gauss's Law: ∯ E·dS = Q/ε₀")
198
199# EXAMPLE 3: Visualize the divergence field
200print("\nVisualization: Radial field F = (x, y, z)")
201visualize_divergence_field(F_radial, div_radial, extent=1.5)

Common Mistakes to Avoid

Watch Out For These Errors:
  1. Non-closed surfaces: The theorem only applies to closed surfaces (surfaces with no boundary). An open surface like a hemisphere requires additional boundary integrals.
  2. Wrong orientation: Normals must point outward. Inward normals give the negative of the correct answer.
  3. Ignoring singularities: If the vector field has singularities inside the volume (like mathbfr/mathbfr3\\mathbf{r}/|\\mathbf{r}|^3 at the origin), the theorem may not apply directly. Use a limiting process or exclude the singularity.
  4. Non-smooth boundaries: The surface must be piecewise smooth. Sharp corners and edges are allowed, but the surface cannot be fractal or infinitely rough.
  5. Regions with holes: For regions with cavities, the boundary consists of multiple surfaces. The outer surface has outward normals; inner (cavity) surfaces have inward-pointing normals (outward from the solid region).
Pro Tip: When in doubt about orientation, remember: the normal should point from the solid region toward empty space. For a filled sphere, normals point outward. For a hollow sphere (cavity inside a solid), the inner surface's normals point toward the cavity.

Test Your Understanding


Summary

In this section, we explored the Divergence Theorem, one of the most powerful results in vector calculus:

  1. The Theorem: oiintSmathbfFcdotdmathbfS=iiintVnablacdotmathbfF,dV\\oiint_S \\mathbf{F} \\cdot d\\mathbf{S} = \\iiint_V \\nabla \\cdot \\mathbf{F} \\, dV
    The flux of a vector field through a closed surface equals the integral of its divergence over the enclosed volume.
  2. Physical Meaning: Net outward flux through a boundary equals the total sources minus sinks inside. This is the mathematical statement of conservation laws.
  3. Computational Power: The theorem often simplifies calculations by converting complicated surface integrals into simpler volume integrals (or vice versa).
  4. Applications: Gauss's Law in electromagnetism, continuity equations in fluid dynamics, heat transfer, and conservation principles throughout physics.
  5. Unified View: The Divergence Theorem is part of a family of theorems (including FTC, Green's, and Stokes') that all express the same principle: boundary integrals equal interior derivative integrals.

Key Formulas

ConceptFormula
Divergence TheoremSFdS=VFdV\oiint_S \mathbf{F} \cdot d\mathbf{S} = \iiint_V \nabla \cdot \mathbf{F}\,dV
DivergenceF=Px+Qy+Rz\nabla \cdot \mathbf{F} = \frac{\partial P}{\partial x} + \frac{\partial Q}{\partial y} + \frac{\partial R}{\partial z}
Gauss's Law (Integral)SEdS=Q/ε0\oiint_S \mathbf{E} \cdot d\mathbf{S} = Q / \varepsilon_0
Gauss's Law (Differential)E=ρ/ε0\nabla \cdot \mathbf{E} = \rho / \varepsilon_0
Continuity Equationρt+(ρv)=0\frac{\partial \rho}{\partial t} + \nabla \cdot (\rho \mathbf{v}) = 0
The Essence of the Divergence Theorem:
"What flows through a closed surface tells you everything about the sources and sinks inside—you can measure the interior by watching the boundary."
Congratulations! You have now completed your study of the major integral theorems of vector calculus: Green's Theorem, Stokes' Theorem, and the Divergence Theorem. Together, these form the mathematical foundation for understanding how fields, flows, and forces behave in the physical world. These tools appear throughout physics, engineering, and increasingly in machine learning for modeling continuous dynamics.
Loading comments...