Chapter 2 – Numerical Differentiation and Integration

Lesson: key ideas for two-point formulas, higher-order differences, and classical quadrature rules, followed by interactive exercises to test understanding.

Two-point formulas for numerical differentiation

Lesson: approximate first derivatives using pairs of nearby function values.

Lesson. Numerical differentiation replaces the exact derivative with finite differences built from function values at nearby points. For a step size h, the forward difference is f′(x) ≈ ( f(x + h) − f(x) ) / h, the backward difference is f′(x) ≈ ( f(x) − f(x − h) ) / h, and the central difference is f′(x) ≈ ( f(x + h) − f(x − h) ) / (2 h). The forward and backward formulas have truncation error proportional to h, while the central formula is typically more accurate with truncation error proportional to h².

Exercise 1 – Compare two-point formulas on eˣ

Use f(x) = eˣ and approximate f′(x₀) at a chosen x₀ using forward, backward, and central differences with step size h.

Hint

Compare each approximation with the exact derivative f′(x₀) = eˣ₀ and observe how the error behaves when you reduce h.

Higher-order derivative formulas

Lesson: use finite-difference stencils to approximate second derivatives.

Lesson. Finite differences generalize naturally to higher derivatives by combining several function values with suitable coefficients. A standard three-point central approximation of the second derivative is f″(x) ≈ ( f(x + h) − 2 f(x) + f(x − h) ) / h², which is second-order accurate in h. More accurate formulas for higher derivatives can be derived systematically using Taylor expansions and symmetry.

Exercise 2 – Approximate the second derivative of sin(x)

For f(x) = sin(x) the exact second derivative is f″(x) = −sin(x). Compare the finite-difference approximation with the exact value at a chosen x₀ and step size h.

Stencil formula
Three-point central formula for the second derivative:

f''(x0) ≈ [ f(x0 + h) - 2 f(x0) + f(x0 - h) ] / h^2

This approximation is based on Taylor expansions of f around x0.
          

Rectangle (midpoint) rule

Lesson: approximate definite integrals using rectangles centered in subintervals.

Lesson. Numerical integration replaces the area under a curve by simple geometric shapes whose areas are easy to compute. The midpoint rule partitions the interval into subintervals and approximates each piece by a rectangle whose height is the function value at the subinterval midpoint. For n subintervals of width Δx on [a, b], the midpoint approximation is Mₙ = Δx ∑ f(mᵢ), where mᵢ are the midpoints.

Exercise 3 – Midpoint rule for ∫₀¹ eˣ dx

The exact value of ∫₀¹ eˣ dx is e − 1. Use the midpoint rule with n subintervals to approximate the integral and compare the absolute error.

Formula
For a function f on [a, b] and n subintervals:

Δx = (b - a) / n
m_i = a + (i - 0.5) Δx,  i = 1, ..., n

Midpoint rule:
M_n = Δx * Σ_{i=1}^n f(m_i).
          

Trapezoidal vs rectangle rules

Lesson: compare two basic quadrature rules on the same integral.

Lesson. The trapezoidal rule approximates the graph of the function on each subinterval by a straight line between the endpoints, producing trapezoids. For n subintervals on [a, b] with step size Δx, the trapezoidal approximation is Tₙ = (Δx / 2) [ f(x₀) + 2 ∑ f(xᵢ) + f(xₙ) ]. For smooth functions, the midpoint rule often achieves slightly smaller error than the trapezoidal rule for the same n, although the difference depends on the function shape.

Exercise 4 – Compare trapezoidal and midpoint errors for sin(x)

Consider ∫₀^π sin(x) dx, whose exact value is 2. Compute both midpoint and trapezoidal approximations with n subintervals and compare the absolute errors.

Trapezoidal formula
Trapezoidal rule on [a, b] with n subintervals:

Δx = (b - a) / n
x_i = a + i Δx

T_n = (Δx / 2) * [ f(x_0) + 2 Σ_{i=1}^{n-1} f(x_i) + f(x_n) ].
          

Simpson’s rule on practical examples

Lesson: combine endpoint and midpoint information to get higher accuracy.

Lesson. Simpson’s rule fits a quadratic polynomial through each pair of subintervals and integrates that polynomial exactly, combining ideas from the midpoint and trapezoidal rules. For an even number n of subintervals and step size Δx, Simpson’s approximation is Sₙ = Δx/3 [ f(x₀) + 4 f(x₁) + 2 f(x₂) + ⋯ + 4 f(xₙ₋₁) + f(xₙ) ], which typically yields much smaller errors for smooth functions than midpoint or trapezoidal rules with the same n.

Exercise 5 – Compare three rules on 1 / (1 + x²)

Use f(x) = 1 / (1 + x²) on [0, 1], whose exact integral equals π/4. For an even n, compute midpoint, trapezoidal, and Simpson’s approximations and compare their absolute errors.

Interpretation

Observe how the Simpson approximation converges faster to π/4 as n increases, compared with the midpoint and trapezoidal rules on this smooth test function.