Line Integrals
A line integral adds up a quantity along a curve instead of along an interval: the mass of a bent wire with varying density, or the work a force field does on an object that moves along a path. This lesson defines line integrals with respect to arc length and with respect to , and , shows how orientation matters for one kind and not the other, and computes several of them exactly with SymPy and numerically with NumPy. For a first look, see Line Integrals in the survey course.
Line integrals with respect to arc length
Let be a smooth curve given by , , and a function defined on . Cut the parameter interval into pieces; this cuts into subarcs of lengths . Pick a sample point on each. The line integral of along is
whenever the limit exists. Because (the arc length element), it is computed as an ordinary integral:
Two sanity checks: if , the integral is the length of ; and the value does not depend on the parametrisation, as long as each point is traced once. If it is the area of a "curtain" standing on with height . In three dimensions add and under the root.
A piecewise-smooth curve is handled one piece at a time: .
import sympy as sp
t = sp.symbols('t', nonnegative=True)
x, y = t, t**2 # the parabola y = x² from (0, 0) to (1, 1)
speed = sp.sqrt(sp.diff(x, t)**2 + sp.diff(y, t)**2)
I = sp.integrate(x * speed, (t, 0, 1)) # ∫_C x ds
L = sp.integrate(speed, (t, 0, 1)) # ∫_C 1 ds = arc length
print("∫ x ds =", sp.simplify(I), "≈", round(float(I), 6))
print("length =", sp.simplify(L), "≈", round(float(L), 6))
print("average x along the curve ≈", round(float(I / L), 6))∫ x ds = -1/12 + 5*sqrt(5)/12 ≈ 0.848362
length = asinh(2)/4 + sqrt(5)/2 ≈ 1.478943
average x along the curve ≈ 0.573627
The average of along the arc is above because the parabola gets steeper on the right, so more of its length sits at large . (asinh(2) is the inverse hyperbolic sine, .)
Line integrals with respect to x, y and z
Replacing by or gives
and the two usually appear together as . These integrals depend on orientation: if is the same curve traversed backwards, then , because every changes sign. The arc-length integral does not change, since is always positive.
Line integrals of vector fields: work
Let be a continuous vector field and a smooth curve with unit tangent . Moving a particle a short distance along , the force does work . Summing and taking the limit defines the line integral of along :
In components, with and , this is the same object as . Only the tangential part of counts: a force perpendicular to the motion, such as the normal force on a roller-coaster car, does no work.
import sympy as sp
t = sp.symbols('t')
def work(F, r, a, b):
rp = [sp.diff(c, t) for c in r]
integrand = sum(Fi.subs({X: r[0], Y: r[1]}) * di for Fi, di in zip(F, rp))
return sp.integrate(sp.expand(integrand), (t, a, b))
X, Y = sp.symbols('x y')
F = [Y**2, 3 * X] # F = <y², 3x>
print("along y = x :", work(F, [t, t], 0, 1))
print("along y = x² :", work(F, [t, t**2], 0, 1))
print("along y = x³ :", work(F, [t, t**3], 0, 1))
print("y = x, backwards :", work(F, [1 - t, 1 - t], 0, 1))along y = x : 11/6
along y = x² : 11/5
along y = x³ : 67/28
y = x, backwards : -11/6
Three paths from to give three different amounts of work, so for this the work depends on the path, not just on the endpoints. Reversing the path flips the sign. The next lesson explains which fields are path-independent.
Numerical line integrals
When there is no formula, sample the curve at many points and add over the little segments. This is exactly how a game or a simulation would compute the work of wind or a current on a recorded path.
import numpy as np
def line_integral(F, pts):
pts = np.asarray(pts, dtype=float)
mid = (pts[1:] + pts[:-1]) / 2
step = pts[1:] - pts[:-1]
return float(np.sum(np.sum(F(mid.T).T * step, axis=1)))
wind = lambda p: np.array([0.5 + 0 * p[0], np.cos(p[0])]) # wind <0.5, cos x>
t = np.linspace(0, 2 * np.pi, 20_001)
wavy = np.column_stack([t, np.sin(t)]) # the path y = sin x
straight = np.column_stack([t, 0 * t])
print("wavy path :", round(line_integral(wind, wavy), 6))
print("straight path:", round(line_integral(wind, straight), 6))
print("exact : 2π =", round(2 * np.pi, 6), " π =", round(np.pi, 6))wavy path : 6.283185
straight path: 3.141593
exact : 2π = 6.283185 π = 3.141593
Along the straight path only the -part of the wind helps: . The wavy path also moves in , with , exactly where the -part of the wind is , so it picks up an extra . Same start, same end, twice the work.
In software
- Physics and games. The work done by a force along a path is a line integral; for a path sampled at frames it is the sum of , which is exactly what an energy-accounting debugger in a physics engine computes.
- Wire and cable meshes. Mass, centre of mass and moments of thin wires, hair strands and cables use .
- Path costs. In robot path planning, the cost of a route through a terrain with cost density is , discretised along the path.
- Graphics. Line integrals along rays () give the optical depth used for fog, smoke and volume rendering.
Exercises
Exercise 1: A straight wire
Compute where is the segment from to .
Answer
.
Solution
Exercise 2: Circulation around a circle
Compute for around the circle of radius 2 centred at the origin, counter-clockwise.
Answer
.
Solution
is tangent to the circle everywhere, so the whole field does work.
Exercise 3: Orientation
Suppose and . What are and ?
Answer
−7 and 3.
Solution
Reversing the direction reverses every tangent vector (every , , ), so the work changes sign. The arc-length element is positive for either direction, so the scalar integral is unchanged.
Exercise 4: Mass of a helix (code)
A wire has the shape of the helix , , and density . Find its mass exactly, then check it numerically.
Answer
.
Solution
import numpy as np
n = 100_000
t = (np.arange(n) + 0.5) * 2 * np.pi / n # midpoints
print(round(float(np.sum(t * np.sqrt(2)) * 2 * np.pi / n), 6), round(2 * np.sqrt(2) * np.pi**2, 6))27.915457 27.915457
Exercise 5: A river current
A river current is . A boat goes from to , once along the straight line and once along . Compute the work of the current on each path by hand, then check both with the line_integral function from the lesson.
Answer
Straight line: 4. Parabola: .
Solution
import numpy as np
def line_integral(F, pts):
pts = np.asarray(pts, dtype=float)
mid = (pts[1:] + pts[:-1]) / 2
step = pts[1:] - pts[:-1]
return float(np.sum(np.sum(F(mid.T).T * step, axis=1)))
current = lambda p: np.array([p[1], 1 + 0 * p[0]])
t = np.linspace(0, 2, 10_001)
print(round(line_integral(current, np.column_stack([t, t])), 6),
round(line_integral(current, np.column_stack([t, t**2 / 2])), 6))4.0 3.333333
Test yourself
If is traversed in the opposite direction, which integral changes sign?
- the length of
- none of them
A force is perpendicular to the direction of motion at every point of a path. The work it does is…
- times the length
- 0
- negative
- it depends on the parametrisation
Which formula computes for , ?