Topic

Green, Stokes and the divergence theorem

Each of the three theorems says that an integral over a region equals an integral over its boundary. greens, stokes and divergenceTheorem compute both sides, separately and with certified enclosures, and report whether the enclosures meet. The boundary is built from the description of the region, piece by piece, and each piece is returned.

const g = greens(["-y^3", "x^3"], ["x", "y"], { x: [0, 1], y: [0, 1] });
g.statement;      // "∮ P dx + Q dy = ∬ (∂Q/∂x − ∂P/∂y) dA"
g.left.value;     // [2, 2]   the circulation, four sides added
g.left.pieces;    // [{ side: "y = 0", curve: { x: "t", y: "0" }, value }, …]
g.right.value;    // [2, 2]   the double integral of 3x² + 3y²
g.agree;          // true

Green's theorem

For a region DD in the plane with boundary CC traversed counter-clockwise,

∮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 region is given the way a double integral is written: { x: [a, b], y: [lo, hi] }, where the bounds for y may be formulas in x. Its boundary is then four curves: the bottom y=lo(x)y = lo(x) from left to right, the right side upward, the top y=hi(x)y = hi(x) from right to left and the left side downward. A side of no length, such as the corner of a triangle, is left out.

// the triangle 0 ≤ y ≤ x ≤ 1
greens(["x*y", "x^2"], ["x", "y"], { x: [0, 1], y: [0, "x"] });

// the area of a region as a line integral: P = -y/2, Q = x/2 makes the right side ∬ 1 dA
greens(["-y/2", "x/2"], ["x", "y"], { x: [0, 1], y: ["x^2", "x"] }).left.value;   // 1/6

// the flux form  ∮ F·n ds = ∬ div F dA
greens(["x^2", "y^2"], ["x", "y"], { x: [0, 1], y: [0, 2] }, { flux: true });

Stokes' theorem

For a surface SS with boundary curve CC,

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

The surface is a parametrisation r(u,v)\mathbf r(u, v) over a region of the (u,v)(u, v) plane, and n dS=ru×rv du dv\mathbf n\,dS = \mathbf r_u \times \mathbf r_v\,du\,dv. The boundary is the image of the boundary of the parameter region, taken counter-clockwise there, which is the orientation that matches this normal.

// the paraboloid z = u² over the unit disc, in polar parameters
const s = stokes(["z", "x", "y"], ["x", "y", "z"],
  { x: "u*cos(v)", y: "u*sin(v)", z: "u^2" }, ["u", "v"], { u: [0, 1], v: [0, "2*pi"] });
s.left.value;     // [3.14159247…, 3.14159283…]
s.right.value;    // [3.14158913…, 3.14159623…]   both hold π
s.right.normal;   // the components of r_u × r_v

In that example two of the four sides are the same segment traversed in opposite directions (v=0v = 0 and v=2πv = 2\pi) and one is the single point u=0u = 0. Their contributions cancel or vanish, and the pieces show it.

The divergence theorem

For a solid VV with closed surface SS and outward normal n\mathbf n,

∯SF⋅n dS  =  ∭V∇⋅F dV. \oiint_S \mathbf F\cdot\mathbf n\,dS \;=\; \iiint_V \nabla\cdot\mathbf F\,dV .

The solid is { x: [a, b], y: [c, d], z: [e, f] } with the bounds for y formulas in x and those for z formulas in x and y. Its surface is a top, a bottom and up to four walls. On the top z=f(x,y)z = f(x, y) the outward normal times the area element is (−fx,−fy,1) dx dy(-f_x, -f_y, 1)\,dx\,dy, on a wall y=d(x)y = d(x) it is (−d′(x),1,0) dx dz(-d'(x), 1, 0)\,dx\,dz, and so on; each face is one double integral.

const d = divergenceTheorem(["x", "y^2", "z"], ["x", "y", "z"],
  { x: [0, 1], y: [0, "1 - x"], z: [0, "1 - x - y"] });      // a tetrahedron
d.left.pieces.map((p) => p.face);   // top, bottom, and the walls; two walls have no height
d.left.value;                       // 5/12
d.right.value;                      // 5/12

For a surface that is easier to parametrise than to cut into graphs, pass the pieces yourself as { surface: [{ r, parameters, region, orientation }] }, with orientation: -1 where ru×rv\mathbf r_u\times\mathbf r_v points inward.

What "agree" means

The two sides are computed by different integrals over different sets, so their enclosures are independent. The theorem says the two numbers are equal, hence the enclosures must have a point in common, and overlap is their intersection, which contains the common value. If they did not meet, either the hypotheses would be violated (a field that is not smooth on the region, a wrong orientation) or the library would have a bug. That makes these functions useful as checks of a hand calculation: compute one side yourself and see whether it lies in overlap.

callreturns
greens([P, Q], [x, y], region, { digits = 8, flux }){ statement, left, right, agree, overlap }
stokes(F, [x, y, z], surface, [u, v], region, { digits = 6 })the same shape; right also has curl, normal, integrand
divergenceTheorem(F, [x, y, z], solid, { digits = 5, surface })the same shape; left.pieces lists each face
left.value, right.value, overlapRationalInterval

See also change of variables, potentials and Lagrange multipliers, and the tools these are built on in Several variables.

Try itEdit the code and run it with @ratmath/analytic and Core's R, Rational, RationalInterval in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs