Integration along curves and over surfaces. A scalar line integral measures total mass along a wire. A vector line integral (work integral) measures how much a vector field pushes along a path. Surface integrals measure flux: how much of the field passes through a surface.
Scalar line integrals
If you have a scalar function f and a curve C parametrized by r(t), the line integral ∫_C f ds integrates f along the curve, weighted by arc length. Think: total mass of a wire with varying density.
Scheme
; Scalar line integral of f(x,y) = x + y along the unit circle; r(t) = (cos(t), sin(t)), t in [0, 2*pi]; |r'(t)| = 1 (unit speed); integral = integral from 0 to 2*pi of (cos(t)+sin(t))*1 dt = 0
(define pi 3.141592653589793)
(define (integrate fn a b n)
(let* ((dt (/ (- b a) n)))
(do ((i 0 (+ i 1))
(sum 0 (+ sum (fn (+ a (* dt (+ i 0.5)))))))
((= i n) (* sum dt)))))
(define result
(integrate
(lambda (t)
(let ((x (cos t)) (y (sin t)))
; f(x,y) * |r'(t)| = (x+y) * 1
(+ x y)))
0 (* 2 pi) 1000))
(display "Scalar line integral of (x+y) on unit circle = ")
(display result) (newline)
(display "Exact: 0 (symmetry: positive and negative cancel)")
Python
# Scalar line integral of f(x,y) = x+y along unit circleimportmath
n = 10000
dt = 2 * math.pi / n
total = 0.0for i inrange(n):
t = dt * (i + 0.5)
x, y = math.cos(t), math.sin(t)
total += (x + y) # |r'(t)| = 1
result = total * dt
print("Scalar line integral of (x+y) on unit circle = " + format(result, ".6f"))
print("Exact: 0 (symmetry: positive and negative cancel)")
Vector line integrals
The work done by a force field F along a path C is ∫_C F · dr. Parametrize the curve, compute F(r(t)) · r'(t), and integrate. If F is conservative (F = grad(f)), the integral depends only on endpoints.
Scheme
; Work integral: F = (y, x), path r(t) = (t, t^2) for t in [0, 1]; r'(t) = (1, 2t); F(r(t)) = (t^2, t); F . r' = t^2*1 + t*2t = t^2 + 2t^2 = 3t^2; integral from 0 to 1 of 3t^2 dt = 1
(define (integrate fn a b n)
(let* ((dt (/ (- b a) n)))
(do ((i 0 (+ i 1))
(sum 0 (+ sum (fn (+ a (* dt (+ i 0.5)))))))
((= i n) (* sum dt)))))
(define work
(integrate
(lambda (t)
(let ((x t) (y (* t t)))
; F = (y, x) = (t^2, t); r' = (1, 2t); dot product
(+ (* y 1) (* x (* 2 t)))))
011000))
(display "Work = integral of F.dr = ") (display work) (newline)
(display "Exact: 1")
Python
import numpy as np
t = np.linspace(0, 1, 10000)
dt = t[1] - t[0]
# F = (y, x) = (t^2, t), r' = (1, 2t)
integrand = t**2 * 1 + t * 2*t # F.r' = 3t^2
work = np.sum(integrand * dt)
print(f"Work = {work:.4f}, exact = 1.0")
Surface integrals and flux
A surface integral ∫∫_S F · dS measures the total flux of a vector field through a surface. dS = n dA where n is the unit normal. Flux is the amount of "stuff" flowing through the surface per unit time.
Scheme
; Flux of F = (0, 0, z) through the top of the unit cube z=1, x in [0,1], y in [0,1]; Normal n = (0, 0, 1) (pointing up); F . n = z = 1 on the surface z=1; Flux = integral of 1 dA over [0,1]x[0,1] = 1
(define (double-integrate f n)
(let* ((dx (/ 1.0 n)) (sum 0))
(do ((i 0 (+ i 1))) ((= i n) (* sum dx dx))
(do ((j 0 (+ j 1))) ((= j n))
(let ((x (* dx (+ i 0.5)))
(y (* dx (+ j 0.5))))
(set! sum (+ sum (f x y))))))))
; On z=1 surface: F.n = 1
(define flux
(double-integrate (lambda (x y) 1) 100))
(display "Flux of (0,0,z) through top of unit cube = ")
(display flux) (newline)
(display "Exact: 1") (newline)
; More interesting: F = (0, 0, x+y), same surface
(define flux2
(double-integrate (lambda (x y) (+ x y)) 200))
(display "Flux of (0,0,x+y) through top = ")
(display flux2) (newline)
(display "Exact: 1")
Python
# Surface integrals: flux through top of unit cube# F = (0,0,z), surface z=1, normal = (0,0,1)# F.n = 1 on z=1
n = 100
dx = 1.0 / n
flux = sum(1 * dx * dx for i inrange(n) for j inrange(n))
print("Flux of (0,0,z) through top of unit cube = " + format(flux, ".4f"))
print("Exact: 1")
# F = (0,0,x+y), same surface
flux2 = sum((dx*(i+0.5) + dx*(j+0.5)) * dx * dx for i inrange(n) for j inrange(n))
print("Flux of (0,0,x+y) through top = " + format(flux2, ".4f"))
print("Exact: 1")