TExcelTAD
Home › Calculus in Depth › Line Integrals

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 xx, yy and zz, 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 CC be a smooth curve given by r(t)=⟨x(t),y(t)⟩\mathbf{r}(t) = \langle x(t), y(t) \rangle, a≤t≤ba \le t \le b, and ff a function defined on CC. Cut the parameter interval into nn pieces; this cuts CC into subarcs of lengths Δs1,…,Δsn\Delta s_1, \ldots, \Delta s_n. Pick a sample point Pi∗P_i^* on each. The line integral of ff along CC is

∫Cf(x,y) ds=lim⁡n→∞∑f(Pi∗) Δsi\int_C f(x, y)\,ds = \lim_{n \to \infty} \sum f(P_i^*)\,\Delta s_i

whenever the limit exists. Because ds=∣r′(t)∣ dtds = \lvert \mathbf{r}'(t) \rvert\,dt (the arc length element), it is computed as an ordinary integral:

∫Cf ds=∫abf(x(t),y(t))⋅x′(t)2+y′(t)2 dt\int_C f\,ds = \int_a^b f(x(t), y(t)) \cdot \sqrt{x'(t)^2 + y'(t)^2}\,dt

Two sanity checks: if f=1f = 1, the integral is the length of CC; and the value does not depend on the parametrisation, as long as each point is traced once. If f(x,y)≥0f(x, y) \ge 0 it is the area of a "curtain" standing on CC with height ff. In three dimensions add z(t)z(t) and z′(t)2z'(t)^2 under the root.

A piecewise-smooth curve C=C1∪C2∪…∪CnC = C_1 \cup C_2 \cup \ldots \cup C_n is handled one piece at a time: ∫Cf ds=∑∫Cif ds\int_C f\,ds = \sum \int_{C_i} f\,ds.

python · try it yourself
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 xx along the arc is above 12\frac{1}{2} because the parabola gets steeper on the right, so more of its length sits at large xx. (asinh(2) is the inverse hyperbolic sine, ln⁡(2+5)\ln(2 + \sqrt{5}).)

Line integrals with respect to x, y and z

Replacing Δsi\Delta s_i by Δxi\Delta x_i or Δyi\Delta y_i gives

∫CP dx=∫abP(x(t),y(t)) x′(t) dt,∫CQ dy=∫abQ(x(t),y(t)) y′(t) dt\int_C P\,dx = \int_a^b P(x(t), y(t))\,x'(t)\,dt, \qquad \int_C Q\,dy = \int_a^b Q(x(t), y(t))\,y'(t)\,dt

and the two usually appear together as ∫CP dx+Q dy\int_C P\,dx + Q\,dy. These integrals depend on orientation: if −C-C is the same curve traversed backwards, then ∫−CP dx=−∫CP dx\int_{-C} P\,dx = -\int_C P\,dx, because every Δx\Delta x changes sign. The arc-length integral does not change, since Δs\Delta s is always positive.

Line integrals of vector fields: work

Let F\mathbf{F} be a continuous vector field and CC a smooth curve with unit tangent T\mathbf{T}. Moving a particle a short distance Δs\Delta s along CC, the force does work ≈(F⋅T) Δs\approx (\mathbf{F} \cdot \mathbf{T})\,\Delta s. Summing and taking the limit defines the line integral of F\mathbf{F} along CC:

∫CF⋅dr=∫CF⋅T ds=∫abF(r(t))⋅r′(t) dt\int_C \mathbf{F} \cdot d\mathbf{r} = \int_C \mathbf{F} \cdot \mathbf{T}\,ds = \int_a^b \mathbf{F}(\mathbf{r}(t)) \cdot \mathbf{r}'(t)\,dt

In components, with F=⟨P,Q,R⟩\mathbf{F} = \langle P, Q, R \rangle and dr=⟨dx,dy,dz⟩d\mathbf{r} = \langle dx, dy, dz \rangle, this is the same object as ∫CP dx+Q dy+R dz\int_C P\,dx + Q\,dy + R\,dz. Only the tangential part of F\mathbf{F} counts: a force perpendicular to the motion, such as the normal force on a roller-coaster car, does no work.

python · try it yourself
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 (0,0)(0, 0) to (1,1)(1, 1) give three different amounts of work, so for this F\mathbf{F} 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 F(midpoint)⋅(step vector)\mathbf{F}(\text{midpoint}) \cdot (\text{step vector}) 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.

python · try it yourself
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 xx-part of the wind helps: 0.5⋅2π=π0.5 \cdot 2\pi = \pi. The wavy path also moves in yy, with dy=cos⁡x dxdy = \cos x\,dx, exactly where the yy-part of the wind is cos⁡x\cos x, so it picks up an extra ∫02πcos⁡2x dx=π\int_0^{2\pi} \cos^2 x\,dx = \pi. 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 F⋅Δr\mathbf{F} \cdot \Delta\mathbf{r}, 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 ∫ρ ds\int \rho\,ds.
  • Path costs. In robot path planning, the cost of a route through a terrain with cost density c(x,y)c(x, y) is ∫Cc ds\int_C c\,ds, discretised along the path.
  • Graphics. Line integrals along rays (∫density ds\int \text{density}\,ds) give the optical depth used for fog, smoke and volume rendering.

Exercises

Exercise 1: A straight wire

Compute ∫C(x+y) ds\int_C (x + y)\,ds where CC is the segment from (0,0)(0, 0) to (3,4)(3, 4).

Answer

352=17.5\frac{35}{2} = 17.5.

Solution
r(t)=⟨3t,4t⟩,0≤t≤1,∣r′(t)∣=9+16=5∫C(x+y) ds=∫01(3t+4t)⋅5 dt=35∫01t dt=352\begin{aligned} &\mathbf{r}(t) = \langle 3t, 4t \rangle, \quad 0 \le t \le 1, \quad \lvert \mathbf{r}'(t) \rvert = \sqrt{9 + 16} = 5 \\ &\int_C (x + y)\,ds = \int_0^1 (3t + 4t) \cdot 5\,dt = 35 \int_0^1 t\,dt = \frac{35}{2} \end{aligned}

Exercise 2: Circulation around a circle

Compute ∫CF⋅dr\int_C \mathbf{F} \cdot d\mathbf{r} for F=⟨−y,x⟩\mathbf{F} = \langle -y, x \rangle around the circle of radius 2 centred at the origin, counter-clockwise.

Answer

8π8\pi.

Solution
r(t)=⟨2cos⁡t,2sin⁡t⟩,r′(t)=⟨−2sin⁡t,2cos⁡t⟩,0≤t≤2πF(r(t))=⟨−2sin⁡t,2cos⁡t⟩F⋅r′=4sin⁡2t+4cos⁡2t=4∫02π4 dt=8π\begin{aligned} &\mathbf{r}(t) = \langle 2 \cos t, 2 \sin t \rangle, \quad \mathbf{r}'(t) = \langle -2 \sin t, 2 \cos t \rangle, \quad 0 \le t \le 2\pi \\ &\mathbf{F}(\mathbf{r}(t)) = \langle -2 \sin t, 2 \cos t \rangle \\ &\mathbf{F} \cdot \mathbf{r}' = 4 \sin^2 t + 4 \cos^2 t = 4 \\ &\int_0^{2\pi} 4\,dt = 8\pi \end{aligned}

F\mathbf{F} is tangent to the circle everywhere, so the whole field does work.

Exercise 3: Orientation

Suppose ∫CF⋅dr=7\int_C \mathbf{F} \cdot d\mathbf{r} = 7 and ∫Cf ds=3\int_C f\,ds = 3. What are ∫−CF⋅dr\int_{-C} \mathbf{F} \cdot d\mathbf{r} and ∫−Cf ds\int_{-C} f\,ds?

Answer

−7 and 3.

Solution

Reversing the direction reverses every tangent vector (every dxdx, dydy, dzdz), so the work changes sign. The arc-length element ds=∣r′(t)∣ dtds = \lvert \mathbf{r}'(t) \rvert\,dt 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 r(t)=⟨cos⁡t,sin⁡t,t⟩\mathbf{r}(t) = \langle \cos t, \sin t, t \rangle, 0≤t≤2π0 \le t \le 2\pi, and density ρ(x,y,z)=z\rho(x, y, z) = z. Find its mass exactly, then check it numerically.

Answer

22⋅π2≈27.9154572\sqrt{2} \cdot \pi^2 \approx 27.915457.

Solution
r′(t)=⟨−sin⁡t,cos⁡t,1⟩,∣r′(t)∣=sin⁡2t+cos⁡2t+1=2m=∫Cz ds=∫02πt⋅2 dt=2⋅(2π)22=22 π2\begin{aligned} &\mathbf{r}'(t) = \langle -\sin t, \cos t, 1 \rangle, \quad \lvert \mathbf{r}'(t) \rvert = \sqrt{\sin^2 t + \cos^2 t + 1} = \sqrt{2} \\ &m = \int_C z\,ds = \int_0^{2\pi} t \cdot \sqrt{2}\,dt = \sqrt{2} \cdot \frac{(2\pi)^2}{2} = 2\sqrt{2}\,\pi^2 \end{aligned}
python · try it yourself
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 F=⟨y,1⟩\mathbf{F} = \langle y, 1 \rangle. A boat goes from (0,0)(0, 0) to (2,2)(2, 2), once along the straight line y=xy = x and once along y=x2/2y = x^2/2. 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: 103≈3.333333\frac{10}{3} \approx 3.333333.

Solution
y=x:r(t)=⟨t,t⟩,r′=⟨1,1⟩,F⋅r′=t+1,∫02(t+1) dt=2+2=4y=x2/2:r(t)=⟨t,t2/2⟩,r′=⟨1,t⟩,F⋅r′=t2/2+t,∫02(t22+t)dt=43+2=103\begin{aligned} &y = x\text{:} && \mathbf{r}(t) = \langle t, t \rangle, && \mathbf{r}' = \langle 1, 1 \rangle, && \mathbf{F} \cdot \mathbf{r}' = t + 1, && \int_0^2 (t + 1)\,dt = 2 + 2 = 4 \\ &y = x^2/2\text{:} && \mathbf{r}(t) = \langle t, t^2/2 \rangle, && \mathbf{r}' = \langle 1, t \rangle, && \mathbf{F} \cdot \mathbf{r}' = t^2/2 + t, && \int_0^2 \left(\frac{t^2}{2} + t\right) dt = \frac{4}{3} + 2 = \frac{10}{3} \end{aligned}
python · try it yourself
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 CC is traversed in the opposite direction, which integral changes sign?

  1. ∫Cf ds\int_C f\,ds
  2. the length of CC
  3. ∫CF⋅dr\int_C \mathbf{F} \cdot d\mathbf{r}
  4. none of them

A force F\mathbf{F} is perpendicular to the direction of motion at every point of a path. The work it does is…

  1. ∣F∣\lvert \mathbf{F} \rvert times the length
  2. 0
  3. negative
  4. it depends on the parametrisation

Which formula computes ∫Cf ds\int_C f\,ds for r(t)\mathbf{r}(t), a≤t≤ba \le t \le b?

  1. ∫abf(r(t)) dt\int_a^b f(\mathbf{r}(t))\,dt
  2. ∫abf(r(t))⋅r′(t) dt\int_a^b f(\mathbf{r}(t)) \cdot \mathbf{r}'(t)\,dt
  3. ∫abf(r(t)) ∣r′(t)∣ dt\int_a^b f(\mathbf{r}(t))\,\lvert \mathbf{r}'(t) \rvert\,dt
  4. f(r(b))−f(r(a))f(\mathbf{r}(b)) - f(\mathbf{r}(a))