The definite integral is the signed area under a curve. Riemann sums approximate it with rectangles. The Fundamental Theorem of Calculus connects integration to differentiation: the integral of f' is f, and the derivative of the integral is f.
Riemann sums
Divide [a,b] into n equal subintervals of width Δx = (b-a)/n. In each subinterval, pick a sample point and multiply f(sample) by Δx. The sum of these rectangles approximates the area. As n approaches infinity, the sum becomes the integral.
Scheme
; Left Riemann sum for integral of x^2 from 0 to 1; Exact answer: 1/3
(define (f x) (* x x))
(define a 0)
(define b 1)
(define (riemann-sum n)
(let ((dx (/ (- b a) n)))
(let loop ((i 0) (sum 0))
(if (= i n) (* sum dx)
(let ((x (+ a (* i dx))))
(loop (+ i 1) (+ sum (f x))))))))
(display "n=4: ") (display (riemann-sum 4)) (newline)
(display "n=10: ") (display (riemann-sum 10)) (newline)
(display "n=100: ") (display (riemann-sum 100)) (newline)
(display "n=1000: ") (display (riemann-sum 1000)) (newline)
(display "Exact: 1/3 = 0.3333...")
Python
# Left Riemann sum for integral of x^2 from 0 to 1def riemann_sum(f, a, b, n):
dx = (b - a) / n
returnsum(f(a + i*dx) * dx for i inrange(n))
f = lambda x: x**2for n in [4, 10, 100, 1000]:
print(f"n={n:<5} sum = {riemann_sum(f, 0, 1, n):.6f}")
print("Exact: 1/3 = 0.333333...")
The definite integral
The integral from a to b of f(x)dx is the limit of the Riemann sum as n approaches infinity. It represents signed area: positive above the x-axis, negative below. The notation is inherited from Leibniz.
Scheme
; Signed area: sin(x) from 0 to 2*pi; Positive hump + negative hump = 0
(define pi 3.141592653589793)
(define (integrate f a b n)
(let ((dx (/ (- b a) n)))
(let loop ((i 0) (sum 0))
(if (= i n) (* sum dx)
(let ((x (+ a (* i dx) (/ dx 2)))) ; midpoint
(loop (+ i 1) (+ sum (f x))))))))
(display "sin from 0 to pi: ")
(display (/ (round (* (integrate sin 0 pi 1000) 10000)) 10000)) (newline) ; 2.0
(display "sin from pi to 2pi: ")
(display (/ (round (* (integrate sin pi (* 2 pi) 1000) 10000)) 10000)) (newline) ; -2.0
(display "sin from 0 to 2pi: ")
(display (/ (round (* (integrate sin 0 (* 2 pi) 1000) 10000)) 10000)) ; 0.0
Python
importmath# Signed area: sin(x) from 0 to 2*pidef integrate(f, a, b, n=1000):
dx = (b - a) / n
returnsum(f(a + (i+0.5)*dx) * dx for i inrange(n))
print("sin from 0 to pi: {:.4f}".format(integrate(math.sin, 0, math.pi))) # 2.0print("sin from pi to 2pi:{:.4f}".format(integrate(math.sin, math.pi, 2*math.pi))) # -2.0print("sin from 0 to 2pi: {:.4f}".format(integrate(math.sin, 0, 2*math.pi))) # 0.0
Fundamental Theorem of Calculus, Part 1
If F(x) = integral from a to x of f(t)dt, then F'(x) = f(x). Differentiation undoes integration. The integral with a variable upper limit is an antiderivative of the integrand.
Scheme
; FTC Part 1: d/dx [integral from 0 to x of f(t) dt] = f(x); F(x) = integral of t^2 from 0 to x = x^3/3; F'(x) = x^2 = f(x)
(define (integrate f a b n)
(let ((dx (/ (- b a) n)))
(let loop ((i 0) (sum 0))
(if (= i n) (* sum dx)
(let ((x (+ a (* i dx) (/ dx 2))))
(loop (+ i 1) (+ sum (f x))))))))
(define (f t) (* t t))
; F(x) = integral from 0 to x of t^2 dt
(define (F x) (integrate f 0 x 500))
; Numerical derivative of F
(define h 0.0001)
(define (F-prime x) (/ (- (F (+ x h)) (F (- x h))) (* 2 h)))
(display "F'(2) = ") (display (/ (round (* (F-prime 2) 1000)) 1000)) (newline)
(display "f(2) = ") (display (f 2)) (newline)
(display "F'(3) = ") (display (/ (round (* (F-prime 3) 1000)) 1000)) (newline)
(display "f(3) = ") (display (f 3))
Python
# FTC Part 1: d/dx[integral from 0 to x of t^2 dt] = x^2def integrate(f, a, b, n=500):
dx = (b - a) / n
returnsum(f(a + (i+0.5)*dx) * dx for i inrange(n))
f = lambda t: t**2
F = lambda x: integrate(f, 0, x)
h = 1e-4for x in [2, 3]:
F_prime = (F(x+h) - F(x-h)) / (2*h)
print("F'({}) = {:.3f}".format(x, F_prime))
print("f({}) = {}".format(x, f(x)))
Fundamental Theorem of Calculus, Part 2
If F is any antiderivative of f (meaning F' = f), then the integral from a to b of f(x)dx = F(b) - F(a). This turns integration from a limit of sums into a subtraction. It is the most important result in calculus.
Scheme
; FTC Part 2: integral of f from a to b = F(b) - F(a); f(x) = 3x^2, antiderivative F(x) = x^3
(define (f x) (* 3 x x))
(define (F x) (* x x x))
; Integral from 1 to 4
(define exact (- (F 4) (F 1))) ; 64 - 1 = 63; Compare with numerical integration
(define (integrate f a b n)
(let ((dx (/ (- b a) n)))
(let loop ((i 0) (sum 0))
(if (= i n) (* sum dx)
(let ((x (+ a (* i dx) (/ dx 2))))
(loop (+ i 1) (+ sum (f x))))))))
(define numerical (integrate f 1410000))
(display "F(4) - F(1) = ") (display exact) (newline)
(display "Numerical: ") (display (/ (round (* numerical 1000)) 1000))
Python
# FTC Part 2: integral of f from a to b = F(b) - F(a)# f(x) = 3x^2, F(x) = x^3def integrate(f, a, b, n=10000):
dx = (b - a) / n
returnsum(f(a + (i+0.5)*dx) * dx for i inrange(n))
f = lambda x: 3*x**2
F = lambda x: x**3
exact = F(4) - F(1) # 64 - 1 = 63
numerical = integrate(f, 1, 4)
print("F(4) - F(1) =", exact)
print("Numerical: {:.3f}".format(numerical))
Antiderivatives
An antiderivative of f is any function F such that F' = f. The general antiderivative is F(x) + C, where C is an arbitrary constant. The integral sign without limits denotes the general antiderivative.
Scheme
; Common antiderivatives:; integral of x^n dx = x^(n+1)/(n+1) + C (n != -1); integral of 1/x dx = ln|x| + C; integral of sin(x) dx = -cos(x) + C; integral of cos(x) dx = sin(x) + C; integral of e^x dx = e^x + C
(define (deriv f)
(let ((h 0.00001))
(lambda (x) (/ (- (f (+ x h)) (f (- x h))) (* 2 h)))))
; Verify: F(x) = x^4/4 is antiderivative of f(x) = x^3
(define (F x) (/ (expt x 4) 4))
(define F-prime (deriv F))
(display "F'(2) = ") (display (/ (round (* (F-prime 2) 1000)) 1000)) (newline) ; 8
(display "x^3 at 2 = ") (display (* 222)) (newline)
; Verify: -cos(x) is antiderivative of sin(x)
(define (G x) (- (cos x)))
(define G-prime (deriv G))
(display "G'(1) = ") (display (/ (round (* (G-prime 1) 10000)) 10000)) (newline)
(display "sin(1) = ") (display (/ (round (* (sin 1) 10000)) 10000))
Python
importmath# Common antiderivatives verified numericallydef deriv(f, x, h=1e-5):
return (f(x+h) - f(x-h)) / (2*h)
# F(x) = x^4/4 is antiderivative of x^3
F = lambda x: x**4 / 4print("F'(2) = {:.3f}".format(deriv(F, 2))) # 8print("x^3 at 2 =", 2**3)
# -cos(x) is antiderivative of sin(x)
G = lambda x: -math.cos(x)
print("G'(1) = {:.4f}".format(deriv(G, 1)))
print("sin(1) = {:.4f}".format(math.sin(1)))