The areas / V18. Measure and integration
Mathlib.MeasureTheory.Integral.IntervalIntegral.TrapezoidalRule
12 declarations. Mathlib docs · Source
The trapezoidal rule
This file contains a definition of integration on [[a, b]] via the trapezoidal rule, along with
an error bound in terms of a bound on the second derivative of the integrand.
Main results
trapezoidal_error_le: the convergence theorem for the trapezoidal rule.
References
We follow the proof on (Wikipedia)[https://en.wikipedia.org/wiki/Trapezoidal_rule] for the error bound.
def trapezoidal_integral E1 #
(ℝ → ℝ) → ℕ → ℝ → ℝ → ℝ
Integration of f from a to b using the trapezoidal rule with N+1 total evaluations of
f. (Note the off-by-one problem here: N counts the number of trapezoids, not the number of
evaluations.)
def trapezoidal_error E1 #
(ℝ → ℝ) → ℕ → ℝ → ℝ → ℝ
The absolute error of trapezoidal integration.
theorem trapezoidal_integral_symm E1 #
∀ (f : ℝ → ℝ) {N : ℕ}, 0 < N → ∀ (a b : ℝ), trapezoidal_integral f N a b = -trapezoidal_integral f N b a
Just like exact integration, the trapezoidal approximation retains the same magnitude but changes sign when the endpoints are swapped.
theorem trapezoidal_error_symm E1 #
∀ (f : ℝ → ℝ) {N : ℕ}, 0 < N → ∀ (a b : ℝ), trapezoidal_error f N a b = -trapezoidal_error f N b a
The absolute error of the trapezoidal rule does not change when the endpoints are swapped.
theorem trapezoidal_integral_eq E0 #
∀ (f : ℝ → ℝ) (N : ℕ) (a : ℝ), trapezoidal_integral f N a a = 0
Just like exact integration, the trapezoidal integration from a to a is zero.
theorem trapezoidal_error_eq E0 #
∀ (f : ℝ → ℝ) (N : ℕ) (a : ℝ), trapezoidal_error f N a a = 0
The error of the trapezoidal integration from a to a is zero.
theorem trapezoidal_integral_one E0 #
∀ (f : ℝ → ℝ) (a b : ℝ), trapezoidal_integral f 1 a b = (b - a) / 2 * (f a + f b)
An exact formula for integration with a single trapezoid (the "midpoint rule").
theorem sum_trapezoidal_integral_adjacent_intervals E1 #
∀ {f : ℝ → ℝ} {N : ℕ} {a h : ℝ},
0 < N →
∑ i ∈ Finset.range N, trapezoidal_integral f 1 (a + ↑i * h) (a + (↑i + 1) * h) =
trapezoidal_integral f N a (a + ↑N * h)
A basic trapezoidal equivalent to IntervalIntegral.sum_integral_adjacent_intervals. More
general theorems are certainly possible, but many of them can be derived from repeated applications
of this one.
theorem trapezoidal_integral_ext E1 #
∀ {f : ℝ → ℝ} {N : ℕ} {a h : ℝ},
0 < N →
trapezoidal_integral f N a (a + ↑N * h) + trapezoidal_integral f 1 (a + ↑N * h) (a + (↑N + 1) * h) =
trapezoidal_integral f (N + 1) a (a + (↑N + 1) * h)
A simplified version of the previous theorem, for use in proofs by induction and the like.
theorem sum_trapezoidal_error_adjacent_intervals E2 #
∀ {f : ℝ → ℝ} {N : ℕ} {a h : ℝ},
0 < N →
IntervalIntegrable f MeasureTheory.volume a (a + ↑N * h) →
∑ i ∈ Finset.range N, trapezoidal_error f 1 (a + ↑i * h) (a + (↑i + 1) * h) = trapezoidal_error f N a (a + ↑N * h)
Since we have sum_[]_adjacent_intervals theorems for both exact and trapezoidal integration,
it's natural to combine them into a similar formula for the error. This theorem is in particular
used in the proof of the general error bound.
theorem trapezoidal_error_le E3 #
∀ {f : ℝ → ℝ} {a b : ℝ},
DifferentiableOn ℝ f (Set.uIcc a b) →
DifferentiableOn ℝ (derivWithin f (Set.uIcc a b)) (Set.uIcc a b) →
∀ {ζ : ℝ},
(∀ (x : ℝ), |iteratedDerivWithin 2 f (Set.uIcc a b) x| ≤ ζ) →
∀ {N : ℕ}, 0 < N → |trapezoidal_error f N a b| ≤ |b - a| ^ 3 * ζ / (12 * ↑N ^ 2)
The standard error bound for trapezoidal integration on the general interval [[a, b]].
theorem trapezoidal_error_le_of_c2 E1 #
∀ {f : ℝ → ℝ} {a b : ℝ},
ContDiffOn ℝ 2 f (Set.uIcc a b) →
∀ {ζ : ℝ},
(∀ (x : ℝ), |iteratedDerivWithin 2 f (Set.uIcc a b) x| ≤ ζ) →
∀ {N : ℕ}, 0 < N → |trapezoidal_error f N a b| ≤ |b - a| ^ 3 * ζ / (12 * ↑N ^ 2)
The error bound for trapezoidal integration in the slightly weaker, but very common, case where
f is C^2.