Numerical integration, often called quadrature, is the process of approximating the definite integral of a function: \[ I = \int_a^b f(x) dx \] In physics and engineering, integration is required to calculate work done by a variable force, center of mass, total charge, magnetic flux, and probabilities. When analytical integration is impossible or the function is only known at discrete data points, numerical methods are essential.
Theory
1. Newton-Cotes Formulas
Newton-Cotes formulas approximate the integrand \(f(x)\) with a polynomial of degree \(n\) that passes through equally spaced points, and then integrate the polynomial exactly.
Rectangle and Midpoint Methods (Degree 0)
Approximates the area as a series of rectangles. - Left Rectangle: \(I \approx \sum f(x_i) \Delta x\) - Midpoint: \(I \approx \sum f(\frac{x_i + x_{i+1}}{2}) \Delta x\)
Trapezoidal Rule (Degree 1)
Approximates the function with a straight line between \(a\) and \(b\). \[ \int_a^b f(x) dx \approx (b - a) \frac{f(a) + f(b)}{2} \]Composite Trapezoidal Rule: Divide \([a,b]\) into \(n\) segments of width \(h = \frac{b-a}{n}\). \[ I \approx \frac{h}{2} \left[ f(x_0) + 2 \sum_{i=1}^{n-1} f(x_i) + f(x_n) \right] \]Error:\(O(h^2)\).
Simpson’s 1/3 Rule (Degree 2)
Approximates the function using a parabola passing through 3 points. \[ \int_a^b f(x) dx \approx \frac{b-a}{6} \left[ f(a) + 4f\left(\frac{a+b}{2}\right) + f(b) \right] \]Composite Simpson’s 1/3 Rule: (Requires an even number of segments \(n\)) \[ I \approx \frac{h}{3} \left[ f(x_0) + 4\sum_{i=1,3,5}^{n-1} f(x_i) + 2\sum_{j=2,4,6}^{n-2} f(x_j) + f(x_n) \right] \]Error:\(O(h^4)\). Significantly more accurate than trapezoidal.
Simpson’s 3/8 Rule (Degree 3)
Uses a cubic polynomial through 4 points. Useful when the number of segments is a multiple of 3. \[ I \approx \frac{3h}{8} [f(x_0) + 3f(x_1) + 3f(x_2) + f(x_3)] \]
2. Gauss Quadrature
Newton-Cotes formulas use equally spaced points. Gauss Quadrature optimizes both the weights and the locations of the evaluation points (nodes) to achieve the highest possible accuracy for a given number of points. An \(n\)-point Gaussian quadrature rule integrates polynomials of degree up to \(2n-1\) exactly. \[ \int_{-1}^1 f(x) dx \approx \sum_{i=1}^n w_i f(x_i) \] Where \(x_i\) are the roots of Legendre polynomials, and \(w_i\) are specific weights. For a general interval \([a,b]\), a coordinate transformation is applied: \(x = \frac{b-a}{2}t + \frac{b+a}{2}\).
Geometric / Visual Explanation
Let’s visualize how these methods approximate the area under a curve.
import numpy as npdef trapezoidal(f, a, b, n): h = (b - a) / n x = np.linspace(a, b, n+1) y = f(x)return (h/2) * (y[0] +2*np.sum(y[1:-1]) + y[-1])def simpson13(f, a, b, n):if n %2!=0:raiseValueError("n must be even for Simpson's 1/3 rule") h = (b - a) / n x = np.linspace(a, b, n+1) y = f(x)return (h/3) * (y[0] +4*np.sum(y[1:-1:2]) +2*np.sum(y[2:-2:2]) + y[-1])def gauss_quadrature_2pt(f, a, b):# Transformationdef transform(t):return ((b-a)*t + a+b)/2.0# 2-point nodes and weights t1, t2 =-1/np.sqrt(3), 1/np.sqrt(3) w1, w2 =1.0, 1.0 integral = w1 * f(transform(t1)) + w2 * f(transform(t2))return integral * (b-a)/2.0