Companion Website

Computational Physics

From Equations to Simulation

Chapter 7

Numerical Integration

The ten programs below provide compact reference implementations of the principal integration methods discussed in Chapter 7, from composite Newton–Cotes and Gaussian quadrature to adaptive, improper, oscillatory, and two-dimensional integration. Each step of the algorithms is kept explicit, making it easy to compare corresponding implementations in MATLAB, Python, C++, and Fortran. Two C++ versions are still in preparation. For larger scientific applications, tested library routines are generally preferable; see the Chapter 7 Resources page for language-specific options.

Program MATLAB Python C++ Fortran
Composite trapezoidal and Simpson rules Comparison of the O(h2) composite trapezoidal rule and the O(h4) composite Simpson rule on successively finer uniform grids. DownloadDownloadDownload Download
Gauss–Legendre quadrature General n-point Gauss–Legendre quadrature, with nodes obtained by Newton iteration on Legendre polynomials and weights calculated from their derivatives. DownloadDownloadDownload Download
Adaptive Simpson integration Adaptive Simpson integration using an explicit interval stack instead of recursion, with local error estimated by comparing one panel with two half-panels. DownloadDownloadComing soon Download
Gauss–Kronrod adaptive integration (G7–K15 and G10–K21) Comparison of adaptive integration using the embedded G7–K15 and G10–K21 pairs, which reuse function evaluations and provide local error indicators. These pairs are also used in QUADPACK. DownloadDownloadDownload Download
QUANC8 adaptive integration Adaptive integration using a 9-point, 8-panel Newton–Cotes rule, following the classic QUANC8 routine of Forsythe, Malcolm, and Moler. Download Download Download Download
Improper integral on a semi-infinite interval Mapping of [0,∞) to [0,1) with x = t/(1 − t), followed by Gauss–Legendre quadrature. DownloadDownloadDownload Download
Cauchy principal-value integral Analytical subtraction of the singular part, followed by Gauss–Legendre quadrature of the regularized integrand. DownloadDownloadDownload Download
Rapidly oscillating integrand Comparison of composite Simpson and quadratic Filon integration for integrals of the form ∫ f(x) cos(ωx) dx. DownloadDownloadDownload Download
Nested Simpson integration in two dimensions Nested Simpson integration with automatic global refinement for a two-dimensional domain with a variable inner limit. DownloadDownloadComing soon Download
Tensor-product Gauss–Legendre quadrature in two dimensions Tensor-product Gauss–Legendre quadrature for a smooth function on a rectangular domain. DownloadDownloadDownload Download

← Back to Chapter 7