Calculus 10: Line & Surface Integrals — Work, Flux, and Streamplots in Machine Learning

2025‑05‑28 · Artintellica

“A line integral adds up little bits of work as you move along a path in a vector field.”


1 · What Are Line and Surface Integrals?

  • Line Integral (Vector Field): For a path r(t)\mathbf{r}(t) in a vector field F\mathbf{F}:

    W=CFdr=abF(r(t))r(t)  dtW = \int_C \mathbf{F} \cdot d\mathbf{r} = \int_a^b \mathbf{F}(\mathbf{r}(t)) \cdot \mathbf{r}'(t)\;dt

    Physically: work done by the field along the path.

  • Surface Integral (brief): For a vector field over a surface SS,

    SFdS\iint_S \mathbf{F} \cdot d\mathbf{S}

    measures flux through the surface (think: how much “stuff” passes through).


2 · Why ML Engineers Care

ConceptML Example
Work/Path IntegralEnergy cost along optimization or RL trajectory
Surface/FluxPolicy flow, divergence in density estimation
VisualizationStreamlines in policy-gradient methods, or GAN dynamics

3 · Demo ① — Work Along a Path in a Vector Field

Let’s use the field:

F(x,y)=[y,x](a simple “rotation” field)\mathbf{F}(x, y) = [-y, x] \qquad \text{(a simple “rotation” field)}

and the path: a unit circle, r(t)=[cost,sint],  t[0,2π]\mathbf{r}(t) = [\cos t, \sin t],\; t \in [0, 2\pi].

The analytic result for the work is 2π2\pi.

# calc-10-line-surface/work_circle.py
import numpy as np
import matplotlib.pyplot as plt

# --- Vector field F(x, y) = [-y, x]
def F(x, y):
    return np.array([-y, x])

# --- Path: unit circle
N = 400
t = np.linspace(0, 2 * np.pi, N)
r = np.stack([np.cos(t), np.sin(t)], axis=1)
drdt = np.stack([-np.sin(t), np.cos(t)], axis=1)
Fs = np.stack([F(x, y) for x, y in r])

# Compute dot(F, dr/dt) at each t
dots = np.sum(Fs * drdt, axis=1)
work = np.trapz(dots, t)
print(f"Work along circle: {work:.5f} (analytic = {2 * np.pi:.5f})")

# --- Plot field, path, streamplot
xv, yv = np.meshgrid(np.linspace(-1.3, 1.3, 24), np.linspace(-1.3, 1.3, 24))
U, V = F(xv, yv)
plt.figure(figsize=(6, 6))
plt.streamplot(xv, yv, U, V, color="gray", density=1.1, linewidth=0.7, arrowsize=1)
plt.plot(r[:, 0], r[:, 1], "r", label="Path (unit circle)")
plt.scatter([0], [0], color="k", s=35, label="Origin")
plt.title("Vector field $[-y, x]$ and circular path")
plt.xlabel("x"); plt.ylabel("y")
plt.legend()
plt.axis("equal")
plt.tight_layout()
plt.show()

4 · Demo ② — Streamplot of a Gradient Field

Let’s look at the field F(x,y)=f\mathbf{F}(x, y) = -\nabla f, where f(x,y)=x2+y2f(x, y) = x^2 + y^2:

F(x,y)=[2x,2y]\mathbf{F}(x, y) = [-2x, -2y]

This is a classic “downhill to the origin” field.

# calc-10-line-surface/streamplot_grad.py
import numpy as np
import matplotlib.pyplot as plt

def gradF(x, y):
    return -2 * x, -2 * y

xv, yv = np.meshgrid(np.linspace(-2, 2, 28), np.linspace(-2, 2, 28))
U, V = gradF(xv, yv)
plt.figure(figsize=(6, 6))
plt.streamplot(xv, yv, U, V, color="blue", density=1.4, linewidth=1)
plt.title(r"Streamplot: $-\nabla f$, $f(x,y)=x^2+y^2$")
plt.xlabel("x"); plt.ylabel("y")
plt.scatter([0], [0], color="k", s=30, label="Minimum")
plt.legend()
plt.axis("equal")
plt.tight_layout()
plt.show()

5 · Why Streamplots?

Streamplots show how “agents” or “probability mass” flow under a vector field.

  • In RL, this is the policy flow.
  • In gradient descent, it’s the path loss would take if following steepest descent.
  • For GANs, dynamics are often viewed as flows in parameter space.

6 · Exercises

  1. Elliptical Path: Compute the work done by F(x,y)=[y,x]\mathbf{F}(x, y) = [-y, x] along the ellipse x=2cost,y=sintx=2\cos t, y=\sin t. Compare to the analytic result.
  2. Radial Field: Compute the work along the unit circle for F(x,y)=[x,y]\mathbf{F}(x, y) = [x, y]. What should the work be? Why?
  3. Custom Field Streamplot: Visualize the streamplot for F(x,y)=[siny,cosx]\mathbf{F}(x, y) = [\sin y, \cos x] over [2,2]2[-2, 2]^2.
  4. RL Connection: For a simple reward landscape f(x,y)=((x1)2+(y+1)2)f(x, y) = -((x-1)^2 + (y+1)^2), plot the gradient field, and show the optimal trajectory starting from (0,0)(0, 0).

Put solutions in calc-10-line-surface/ and tag v0.1.


Next: Calculus 11 — Divergence, Curl, and the Geometry of Probability Flows.



Next Blog Posts

Earlier Blog Posts


Back to Blog

Copyright © 2025 Identellica LLC
Home · Blog · Source Code