Vector Fields and Line Integrals
A vector field attaches an arrow to every point: wind velocity on a weather map, the force of gravity around a planet, the flow direction in a crowd simulation. A line integral adds up a quantity along a curve, and its most important use is the work a force field does on an object that moves along a path.
Vector fields
A vector field in the plane is a function that returns a vector at each point (in 3D, ).
| Field | Picture |
|---|---|
| uniform wind blowing east | |
| arrows pointing straight out from the origin, longer farther out | |
| arrows circling the origin counter-clockwise (a vortex) | |
| inverse-square attraction to the origin (gravity) | |
| a gradient field: arrows point uphill on |
import numpy as np
import matplotlib.pyplot as plt
X, Y = np.meshgrid(np.linspace(-2, 2, 15), np.linspace(-2, 2, 15))
fig, axes = plt.subplots(1, 2, figsize=(8, 4))
axes[0].quiver(X, Y, X, Y, color="tab:blue")
axes[0].set_title("F = ⟨x, y⟩ (source)")
axes[1].quiver(X, Y, -Y, X, color="tab:red")
axes[1].set_title("F = ⟨−y, x⟩ (vortex)")
for ax in axes:
ax.set_aspect("equal")
print("F = <-y, x> at (1, 0):", (-0, 1), " at (0, 1):", (-1, 0))F = <-y, x> at (1, 0): (0, 1) at (0, 1): (-1, 0)
Line integrals of a scalar function
For a curve traced by , , and a scalar function (say, the density of a wire), the line integral with respect to arc length is
With it is the length of , the arc length formula. With it is the mass of a wire shaped like .
Work: the line integral of a vector field
A constant force moving an object along a straight displacement does work (only the component along the motion counts, as in the dot product). For a varying force along a curve, add up over tiny steps:
This also covers circulation (work around a closed loop, written ). Reversing the direction of the curve flips the sign.
Example. around the unit circle , :
The vortex pushes the object along the whole way round, so the work is positive.
import numpy as np
def work(F, r, dr, a, b, n=100_000):
t = np.linspace(a, b, n + 1)
tm = (t[:-1] + t[1:]) / 2 # midpoint rule
integrand = np.sum(F(*r(tm)) * dr(tm), axis=0)
return float(np.sum(integrand) * (b - a) / n)
vortex = lambda x, y: np.array([-y, x])
source = lambda x, y: np.array([x, y])
circle = lambda t: (np.cos(t), np.sin(t))
dcircle = lambda t: np.array([-np.sin(t), np.cos(t)])
print("vortex around circle:", round(work(vortex, circle, dcircle, 0, 2 * np.pi), 6), " (2π =", round(2 * np.pi, 6), ")")
print("source around circle:", round(work(source, circle, dcircle, 0, 2 * np.pi), 6))vortex around circle: 6.283185 (2π = 6.283185 )
source around circle: 0.0
The source field points straight out, perpendicular to the circle at every point, so it does no work.
Conservative fields and path independence
A field is conservative if it is a gradient, , for some potential . Then the chain rule gives , and the line integral collapses, like the Fundamental Theorem:
So in a conservative field the work depends only on the endpoints, not on the path, and around every closed loop it is zero. Gravity and spring forces are conservative, which is why potential energy makes sense for them. Friction is not.
Test (2D, on a region without holes): is conservative exactly when . For the vortex, but , so it is not conservative, matching its non-zero circulation.
import numpy as np
F = lambda x, y: np.array([2 * x * y, x**2 + 3 * y**2]) # = grad of phi = x^2 y + y^3
phi = lambda x, y: x**2 * y + y**3
def work(r, dr, n=200_000):
t = (np.arange(n) + 0.5) / n # midpoints of [0, 1]
return float(np.sum(np.sum(F(*r(t)) * dr(t), axis=0)) / n)
straight = (lambda t: (2 * t, t), lambda t: np.array([2 + 0 * t, 1 + 0 * t]))
wiggly = (lambda t: (2 * t, t + np.sin(np.pi * t)), lambda t: np.array([2 + 0 * t, 1 + np.pi * np.cos(np.pi * t)]))
print("straight path (0,0)->(2,1):", round(work(*straight), 6))
print("wiggly path (0,0)->(2,1):", round(work(*wiggly), 6))
print("phi(2,1) - phi(0,0) :", phi(2, 1) - phi(0, 0))straight path (0,0)->(2,1): 5.0
wiggly path (0,0)->(2,1): 5.0
phi(2,1) - phi(0,0) : 5
In software
- Games and physics engines. The work done by forces along a path equals the change in kinetic energy. Checking that total energy stays constant in a conservative system (no friction) is a standard test of a physics integrator: drift means numerical error. See Integrators.
- Flow fields for AI movement. Crowd and RTS pathfinding often precompute a vector field pointing toward the goal and let thousands of units simply follow it. If the field is the negative gradient of a distance-to-goal map, it is conservative: no unit can circle forever.
- Robotics and planning. Potential-field planners steer along , where is low at the goal and high near obstacles.
- Electromagnetism in simulations. Voltage is the line integral of the electric field; induced currents come from the circulation of a field.
Exercises
Exercise 1: Scalar line integral
Compute where is the straight segment from (0, 0) to (3, 4).
Answer
.
Solution
Exercise 2: Work along a parabola
Find the work done by along from (0, 0) to (1, 1).
Answer
5/6.
Solution
Exercise 3: Conservative?
Is conservative? If so, find a potential and use it to compute the work from (1, 0) to (2, 3).
Answer
Yes; , and the work is 31.
Solution
Exercise 4: Circulation from samples (code)
A game samples a wind field and moves a balloon around the square with corners (0, 0), (1, 0), (1, 1), (0, 1), counter-clockwise. Approximate the circulation by summing over 1000 steps per side.
Answer
The circulation is 2 (exactly; the sum gives 2.0).
Solution
On each small step the work is about . The part of the field is a gradient (of ) and contributes nothing around a closed loop; the rest gives per unit area (that is Green's theorem).
import numpy as np
F = lambda p: np.array([-p[1] + 0.5 * p[0], p[0]])
corners = [np.array(c, dtype=float) for c in [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]]
total = 0.0
for A, B in zip(corners[:-1], corners[1:]):
pts = A + np.linspace(0, 1, 1001)[:, None] * (B - A)
for p, q in zip(pts[:-1], pts[1:]):
total += F((p + q) / 2) @ (q - p)
print(round(total, 6))2.0
Test yourself
The work done by a force field along a curve is…
For a conservative field , the work around a closed loop is…
- always positive
- zero
- the length of the loop
Which test shows that is NOT conservative?
- and are polynomials
- is zero at the origin