Simpson's rule
In numerical integration, Simpson's rules are several approximations for definite integrals, named after Thomas Simpson (1710–1761).
The most basic of these rules, called Simpson's 1/3 rule, or just Simpson's rule, reads [math]\displaystyle{ \int_a^b f(x) \, dx \approx \frac{b - a}{6} \left[f(a) + 4f\left(\frac{a + b}{2}\right) + f(b)\right]. }[/math]
In German and some other languages, it is named after Johannes Kepler, who derived it in 1615 after seeing it used for wine barrels (barrel rule, Keplersche Fassregel). The approximate equality in the rule becomes exact if f is a polynomial up to and including 3rd degree.
If the 1/3 rule is applied to n equal subdivisions of the integration range [a, b], one obtains the composite Simpson's 1/3 rule. Points inside the integration range are given alternating weights 4/3 and 2/3.
Simpson's 3/8 rule, also called Simpson's second rule, requires one more function evaluation inside the integration range and gives lower error bounds, but does not improve on order of the error.
If the 3/8 rule is applied to n equal subdivisions of the integration range [a, b], one obtains the composite Simpson's 3/8 rule.
Simpson's 1/3 and 3/8 rules are two special cases of closed Newton–Cotes formulas.
In naval architecture and ship stability estimation, there also exists Simpson's third rule, which has no special importance in general numerical analysis, see Simpson's rules (ship stability).
Simpson's 1/3 rule
Simpson's 1/3 rule, also simply called Simpson's rule, is a method for numerical integration proposed by Thomas Simpson. It is based upon a quadratic interpolation. Simpson's 1/3 rule is as follows: [math]\displaystyle{ \begin{align} \int_a^b f(x)\, dx &\approx \frac{b - a}{6} \left[f(a) + 4f\left(\frac{a + b}{2}\right) + f(b)\right]\\ &= \frac{1}{3} h\left[f(a) + 4f\left(a + h\right) + f(b)\right], \end{align} }[/math] where [math]\displaystyle{ h = (b - a)/2 }[/math] is the step size.
The error in approximating an integral by Simpson's rule for [math]\displaystyle{ n = 2 }[/math] is [math]\displaystyle{ -\frac{1}{90} h^5f^{(4)}(\xi) = -\frac{(b - a)^5}{2880} f^{(4)}(\xi), }[/math] where [math]\displaystyle{ \xi }[/math] (the Greek letter xi) is some number between [math]\displaystyle{ a }[/math] and [math]\displaystyle{ b }[/math].[1][2]
The error is asymptotically proportional to [math]\displaystyle{ (b - a)^5 }[/math]. However, the above derivations suggest an error proportional to [math]\displaystyle{ (b - a)^4 }[/math]. Simpson's rule gains an extra order because the points at which the integrand is evaluated are distributed symmetrically in the interval [math]\displaystyle{ [a,\ b] }[/math].
Since the error term is proportional to the fourth derivative of [math]\displaystyle{ f }[/math] at [math]\displaystyle{ \xi }[/math], this shows that Simpson's rule provides exact results for any polynomial [math]\displaystyle{ f }[/math] of degree three or less, since the fourth derivative of such a polynomial is zero at all points. Another way to see this result is to note that any interpolating cubic polynomial can be expressed as the sum of the unique interpolating quadratic polynomial plus an arbitrarily scaled cubic polynomial that vanishes at all three points in the interval, and the integral of this second term vanishes because it is odd within the interval.
If the second derivative [math]\displaystyle{ f'' }[/math] exists and is convex in the interval [math]\displaystyle{ (a,\ b) }[/math], then [math]\displaystyle{ (b - a) f\left(\frac{a + b}{2}\right) + \frac{1}{3} \left(\frac{b - a}{2}\right)^3 f''\left(\frac{a + b}{2}\right) \leq \int_a^b f(x)\, dx \leq \frac{b - a}{6} \left[f(a) + 4f\left(\frac{a + b}{2}\right) + f(b)\right]. }[/math]
Derivations
Quadratic interpolation
One derivation replaces the integrand [math]\displaystyle{ f(x) }[/math] by the quadratic polynomial (i.e. parabola) [math]\displaystyle{ P(x) }[/math] that takes the same values as [math]\displaystyle{ f(x) }[/math] at the end points [math]\displaystyle{ a }[/math] and [math]\displaystyle{ b }[/math] and the midpoint [math]\displaystyle{ m = (a + b)/2 }[/math]. One can use Lagrange polynomial interpolation to find an expression for this polynomial, [math]\displaystyle{ P(x) = f(a) \frac{(x - m)(x - b)}{(a - m)(a - b)} + f(m) \frac{(x - a)(x - b)}{(m - a)(m - b)} + f(b) \frac{(x - a)(x - m)}{(b - a)(b - m)}. }[/math] Using integration by substitution, one can show that[3][2] [math]\displaystyle{ \int_a^b P(x)\, dx = \frac{b - a}{6} \left[f(a) + 4f\left(\frac{a + b}{2}\right) + f(b)\right]. }[/math] Introducing the step size [math]\displaystyle{ h = (b - a)/2 }[/math], this is also commonly written as [math]\displaystyle{ \int_a^b P(x)\, dx = \frac{1}{3} h\left[f(a) + 4f\left(a + h\right) + f(b)\right]. }[/math] Because of the [math]\displaystyle{ 1/3 }[/math] factor, Simpson's rule is also referred to as "Simpson's 1/3 rule" (see below for generalization).
Averaging the midpoint and the trapezoidal rules
Another derivation constructs Simpson's rule from two simpler approximations: the midpoint rule [math]\displaystyle{ M = (b - a)f\left(\frac{a + b}{2}\right) }[/math] and the trapezoidal rule [math]\displaystyle{ T = \frac{1}{2} (b - a)\big(f(a) + f(b)\big). }[/math]
The errors in these approximations are [math]\displaystyle{ \frac{1}{24} (b - a)^3 f''(a) + O\big((b - a)^4\big) }[/math] and [math]\displaystyle{ -\frac{1}{12} (b - a)^3 f''(a) + O\big((b - a)^4\big) }[/math] respectively, where [math]\displaystyle{ O\big((b - a)^4\big) }[/math] denotes a term asymptotically proportional to [math]\displaystyle{ (b - a)^4 }[/math]. The two [math]\displaystyle{ O\big((b - a)^4\big) }[/math] terms are not equal; see Big O notation for more details. It follows from the above formulas for the errors of the midpoint and trapezoidal rule that the leading error term vanishes if we take the weighted average [math]\displaystyle{ \frac{2M + T}{3}. }[/math] This weighted average is exactly Simpson's rule.
Using another approximation (for example, the trapezoidal rule with twice as many points), it is possible to take a suitable weighted average and eliminate another error term. This is Romberg's method.
Undetermined coefficients
The third derivation starts from the ansatz [math]\displaystyle{ \frac{1}{b - a} \int_a^b f(x)\, dx \approx \alpha f(a) + \beta f\left(\frac{a + b}{2}\right) + \gamma f(b). }[/math]
The coefficients α, β and γ can be fixed by requiring that this approximation be exact for all quadratic polynomials. This yields Simpson's rule. (This derivation is essentially a less rigorous version of the quadratic interpolation derivation, where one saves significant calculation effort by guessing the correct functional form.)
Composite Simpson's 1/3 rule
If the interval of integration [math]\displaystyle{ [a, b] }[/math] is in some sense "small", then Simpson's rule with [math]\displaystyle{ n = 2 }[/math] subintervals will provide an adequate approximation to the exact integral. By "small" we mean that the function being integrated is relatively smooth over the interval [math]\displaystyle{ [a, b] }[/math]. For such a function, a smooth quadratic interpolant like the one used in Simpson's rule will give good results.
However, it is often the case that the function we are trying to integrate is not smooth over the interval. Typically, this means that either the function is highly oscillatory or lacks derivatives at certain points. In these cases, Simpson's rule may give very poor results. One common way of handling this problem is by breaking up the interval [math]\displaystyle{ [a, b] }[/math] into [math]\displaystyle{ n \gt 2 }[/math] small subintervals. Simpson's rule is then applied to each subinterval, with the results being summed to produce an approximation for the integral over the entire interval. This sort of approach is termed the composite Simpson's 1/3 rule, or just composite Simpson's rule.
Suppose that the interval [math]\displaystyle{ [a, b] }[/math] is split up into [math]\displaystyle{ n }[/math] subintervals, with [math]\displaystyle{ n }[/math] an even number. Then, the composite Simpson's rule is given by
Dividing the interval [math]\displaystyle{ [a, b] }[/math] into [math]\displaystyle{ n }[/math] subintervals of length [math]\displaystyle{ h = (b - a)/n }[/math] and introducing the points [math]\displaystyle{ x_i = a + ih }[/math] for [math]\displaystyle{ 0 \leq i \leq n }[/math] (in particular, [math]\displaystyle{ x_0 = a }[/math] and [math]\displaystyle{ x_n = b }[/math]), we have [math]\displaystyle{ \begin{align} \int_a^b f(x)\, dx &\approx \frac{1}{3} h\sum_{i = 1}^{n/2}\big[f(x_{2i - 2}) + 4f(x_{2i - 1}) + f(x_{2i})\big]\\ &= \frac{1}{3} h\big[f(x_0) + 4f(x_1) + 2f(x_2) + 4f(x_3) + 2f(x_4) + \dots + 2f(x_{n - 2}) + 4f(x_{n - 1}) + f(x_n)\big]\\ &= \frac{1}{3} h\left[f(x_0) + 4\sum_{i = 1}^{n/2} f(x_{2i - 1}) + 2\sum_{i = 1}^{n/2 - 1} f(x_{2i}) + f(x_n)\right]. \end{align} }[/math] This composite rule with [math]\displaystyle{ n = 2 }[/math] corresponds with the regular Simpson's rule of the preceding section.
The error committed by the composite Simpson's rule is [math]\displaystyle{ -\frac{1}{180} h^4(b - a)f^{(4)}(\xi), }[/math] where [math]\displaystyle{ \xi }[/math] is some number between [math]\displaystyle{ a }[/math] and [math]\displaystyle{ b }[/math], and [math]\displaystyle{ h = (b - a)/n }[/math] is the "step length".[4][5] The error is bounded (in absolute value) by [math]\displaystyle{ \frac{1}{180} h^4(b - a) \max_{\xi \in [a, b]} \left|f^{(4)}(\xi)\right|. }[/math]
This formulation splits the interval [math]\displaystyle{ [a, b] }[/math] in subintervals of equal length. In practice, it is often advantageous to use subintervals of different lengths and concentrate the efforts on the places where the integrand is less well-behaved. This leads to the adaptive Simpson's method.
Simpson's 3/8 rule
Simpson's 3/8 rule, also called Simpson's second rule, is another method for numerical integration proposed by Thomas Simpson. It is based upon a cubic interpolation rather than a quadratic interpolation. Simpson's 3/8 rule is as follows: [math]\displaystyle{ \begin{align} \int_a^b f(x)\, dx &\approx \frac{b - a}{8} \left[f(a) + 3f\left(\frac{2a + b}{3}\right) + 3f\left(\frac{a + 2b}{3}\right) + f(b)\right]\\ &= \frac{3}{8} h\left[f(a) + 3f\left(a + h\right) + 3f\left(a + 2h\right) + f(b)\right], \end{align} }[/math] where [math]\displaystyle{ h = (b - a)/3 }[/math] is the step size.
The error of this method is [math]\displaystyle{ -\frac{3}{80} h^5f^{(4)}(\xi) = -\frac{(b - a)^5}{6480} f^{(4)}(\xi), }[/math] where [math]\displaystyle{ \xi }[/math] is some number between [math]\displaystyle{ a }[/math] and [math]\displaystyle{ b }[/math]. Thus, the 3/8 rule is about twice as accurate as the standard method, but it uses one more function value. A composite 3/8 rule also exists, similarly as above.[6]
A further generalization of this concept for interpolation with arbitrary-degree polynomials are the Newton–Cotes formulas.
Composite Simpson's 3/8 rule
Dividing the interval [math]\displaystyle{ [a, b] }[/math] into [math]\displaystyle{ n }[/math] subintervals of length [math]\displaystyle{ h = (b - a)/n }[/math] and introducing the points [math]\displaystyle{ x_i = a + ih }[/math] for [math]\displaystyle{ 0 \leq i \leq n }[/math] (in particular, [math]\displaystyle{ x_0 = a }[/math] and [math]\displaystyle{ x_n = b }[/math]), we have [math]\displaystyle{ \begin{align} \int_a^b f(x)\, dx &\approx \frac{3}{8} h\sum_{i = 1}^{n/3} \big[f(x_{3i - 3}) + 3f(x_{3i - 2}) + 3f(x_{3i - 1}) + f(x_{3i})\big]\\ &= \frac{3}{8} h\big[f(x_0) + 3f(x_1) + 3f(x_2) + 2f(x_3) + 3f(x_4) + 3f(x_5) + 2f(x_6) + \dots + 2f(x_{n - 3}) + 3f(x_{n - 2}) + 3f(x_{n - 1}) + f(x_n)\big]\\ &= \frac{3}{8} h\left[f(x_0) + 3 \sum_{i = 1,\ 3\nmid i}^{n-1} f(x_i) + 2 \sum_{i=1}^{n/3 - 1} f(x_{3i}) + f(x_n)\right]. \end{align} }[/math]
While the remainder for the rule is shown as[6] [math]\displaystyle{ -\frac{1}{80} h^4(b - a)f^{(4)}(\xi), }[/math] we can only use this if [math]\displaystyle{ n }[/math] is a multiple of three. The 1/3 rule can be used for the remaining subintervals without changing the order of the error term (conversely, the 3/8 rule can be used with a composite 1/3 rule for odd-numbered subintervals).
Alternative extended Simpson's rule
This is another formulation of a composite Simpson's rule: instead of applying Simpson's rule to disjoint segments of the integral to be approximated, Simpson's rule is applied to overlapping segments, yielding[7] [math]\displaystyle{ \int_a^b f(x)\, dx \approx \frac{1}{48} h\left[17f(x_0) + 59f(x_1) + 43f(x_2) + 49f(x_3) + 48 \sum_{i= 4 }^{n - 4} f(x_i) + 49f(x_{n - 3}) + 43f(x_{n - 2}) + 59f(x_{n - 1}) + 17f(x_n)\right]. }[/math]
The formula above is obtained by combining the composite Simpson's 1/3 rule with the one consisting of using Simpson's 3/8 rule in the extreme subintervals and Simpson's 1/3 rule in the remaining subintervals. The result is then obtained by taking the mean of the two formulas.
Simpson's rules in the case of narrow peaks
In the task of estimation of full area of narrow peak-like functions, Simpson's rules are much less efficient than trapezoidal rule. Namely, composite Simpson's 1/3 rule requires 1.8 times more points to achieve the same accuracy as trapezoidal rule.[8] Composite Simpson's 3/8 rule is even less accurate. Integration by Simpson's 1/3 rule can be represented as a weighted average with 2/3 of the value coming from integration by the trapezoidal rule with step h and 1/3 of the value coming from integration by the rectangle rule with step 2h. The accuracy is governed by the second (2h step) term. Averaging of Simpson's 1/3 rule composite sums with properly shifted frames produces the following rules: [math]\displaystyle{ \int_a^b f(x)\, dx \approx \frac{1}{24} h\left[-f(x_{-1}) + 12f(x_0) + 25f(x_1) + 24\sum_{i = 2}^{n - 2} f(x_i) + 25f(x_{n - 1}) + 12f(x_n) - f(x_{n + 1})\right], }[/math] where two points outside of the integrated region are exploited, and [math]\displaystyle{ \int_a^b f(x)\, dx \approx \frac{1}{24} h\left[9f(x_0) + 28f(x_1) + 23f(x_2) + 24\sum_{i = 3}^{n - 3} f(x_i) + 23f(x_{n - 2}) + 28f(x_{n - 1}) + 9f(x_n)\right], }[/math] where only points within integration region are used. Application of the second rule to the region of 3 points generates 1/3 Simpon's rule, 4 points - 3/8 rule.
These rules are very much similar to the alternative extended Simpson's rule. The coefficients within the major part of the region being integrated are one with non-unit coefficients only at the edges. These two rules can be associated with Euler–MacLaurin formula with the first derivative term and named First order Euler–MacLaurin integration rules.[8] The two rules presented above differ only in the way how the first derivative at the region end is calculated. The first derivative term in the Euler–MacLaurin integration rules accounts for integral of the second derivative, which equals the difference of the first derivatives at the edges of the integration region. It is possible to generate higher order Euler–Maclaurin rules by adding a difference of 3rd, 5th, and so on derivatives with coefficients, as defined by Euler–MacLaurin formula.
Composite Simpson's rule for irregularly spaced data
For some applications, the integration interval [math]\displaystyle{ I = [a, b] }[/math] needs to be divided into uneven intervals – perhaps due to uneven sampling of data, or missing or corrupted data points. Suppose we divide the interval [math]\displaystyle{ I }[/math] into an even number [math]\displaystyle{ N }[/math] of subintervals of widths [math]\displaystyle{ h_k }[/math]. Then the composite Simpson's rule is given by[9] [math]\displaystyle{ \int_a^b f(x)\, dx = \sum_{i = 0}^{N/2 - 1} \frac{h_{2i} + h_{2i + 1}}{6} \left[\left(2 - \frac{h_{2i + 1}}{h_{2i}}\right) f_{2i} + \frac{(h_{2i} + h_{2i + 1})^2}{h_{2i} h_{2i + 1}} f_{2i + 1} + \left(2 - \frac{h_{2i}}{h_{2i + 1}}\right) f_{2i + 2}\right], }[/math] where [math]\displaystyle{ f_k = f\left(a + \sum_{i = 0}^{k - 1} h_i\right) }[/math] are the function values at the [math]\displaystyle{ k }[/math]th sampling point on the interval [math]\displaystyle{ I }[/math].
In case of an odd number [math]\displaystyle{ N }[/math] of subintervals, the above formula is used up to the second to last interval, and the last interval is handled separately by adding the following to the result:[10] [math]\displaystyle{ \alpha f_N + \beta f_{N - 1} - \eta f_{N - 2}, }[/math] where [math]\displaystyle{ \begin{align} \alpha &= \frac{2h_{N - 1}^2 + 3h_{N - 1} h_{N - 2}}{6(h_{N - 2} + h_{N - 1})},\\[1ex] \beta &= \frac{h_{N - 1}^2 + 3h_{N - 1} h_{N - 2}}{6h_{N - 2}},\\[1ex] \eta &= \frac{h_{N - 1}^3}{6 h_{N - 2}(h_{N - 2} + h_{N - 1})}. \end{align} }[/math]
Example implementation in Python |
from collections.abc import Sequence def simpson_nonuniform(x: Sequence[float], f: Sequence[float]) -> float: """ Simpson rule for irregularly spaced data. :param x: Sampling points for the function values :param f: Function values at the sampling points :return: approximation for the integral See ``scipy.integrate.simpson`` and the underlying ``_basic_simpson`` for a more performant implementation utilizing numpy's broadcast. """ N = len(x) - 1 h = [x[i + 1] - x[i] for i in range(0, N)] assert N > 0 result = 0.0 for i in range(1, N, 2): h0, h1 = h[i - 1], h[i] hph, hdh, hmh = h1 + h0, h1 / h0, h1 * h0 result += (hph / 6) * ( (2 - hdh) * f[i - 1] + (hph**2 / hmh) * f[i] + (2 - 1 / hdh) * f[i + 1] ) if N % 2 == 1: h0, h1 = h[N - 2], h[N - 1] result += f[N] * (2 * h1 ** 2 + 3 * h0 * h1) / (6 * (h0 + h1)) result += f[N - 1] * (h1 ** 2 + 3 * h1 * h0) / (6 * h0) result -= f[N - 2] * h1 ** 3 / (6 * h0 * (h0 + h1)) return result |
Example implementation in R |
SimpsonInt <- function(fx, dx) { n <- length(dx) h <- diff(dx) stopifnot(exprs = { length(fx) == n all(h >= 0) }) res <- 0 for (i in seq(1L, n - 2L, 2L)) { hph <- h[i] + h[i + 1L] hdh <- h[i + 1L] / h[i] res <- res + hph / 6 * ((2 - hdh) * fx[i] + hph ^ 2 / (h[i] * h[i + 1L]) * fx[i + 1L] + (2 - 1 / hdh) * fx[i + 2L]) } if (n %% 2 == 0) { hph <- h[n - 1L] + h[n - 2L] threehth <- 3 * h[n - 1L] * h[n - 2L] sixh2 <- 6 * h[n - 2L] h1sq <- h[n - 1L] ^ 2 res <- res + (2 * h1sq + threehth) / (6 * hph) * fx[n] + (h1sq + threehth) / sixh2 * fx[n - 1L] - (h1sq * h[n - 1L]) / (sixh2 * hph) * fx[n - 2L] } res } |
See also
Notes
- ↑ Atkinson 1989, equation (5.1.15).
- ↑ 2.0 2.1 Süli & Mayers 2003, §7.2.
- ↑ Atkinson 1989, p. 256.
- ↑ Atkinson 1989, pp. 257–258.
- ↑ Süli & Mayers 2003, §7.5.
- ↑ 6.0 6.1 Matthews 2004.
- ↑ Weisstein, Equation 35.
- ↑ 8.0 8.1 Kalambet, Kozmin & Samokhin 2018.
- ↑ Shklov 1960.
- ↑ Cartwright 2017, Equation 8. The equation in Cartwright is calculating the first interval whereas the equations in the Wikipedia article are adjusting for the last integral. If the proper algebraic substitutions are made, the equation results in the values shown.
References
- Atkinson, Kendall E. (1989). An Introduction to Numerical Analysis (2nd ed.). John Wiley & Sons. ISBN 0-471-50023-2.
- Burden, Richard L.; Faires, J. Douglas (2000). Numerical Analysis (7th ed.). Brooks/Cole. ISBN 0-534-38216-9. https://archive.org/details/numericalanalysi0000burd.
- Cartwright, Kenneth V. (September 2017). "Simpson’s Rule Cumulative Integration with MS Excel and Irregularly-spaced Data". Journal of Mathematical Sciences and Mathematics Education 12 (2): 1–9. http://www.msme.us/2017-2-1.pdf. Retrieved December 18, 2022.
- Kalambet, Yuri; Kozmin, Yuri; Samokhin, Andrey (2018). "Comparison of integration rules in the case of very narrow chromatographic peaks". Chemometrics and Intelligent Laboratory Systems 179: 22–30. doi:10.1016/j.chemolab.2018.06.001. ISSN 0169-7439.
- Matthews, John H. (2004). "Simpson's 3/8 Rule for Numerical Integration". Numerical Analysis - Numerical Methods Project. California State University, Fullerton. http://math.fullerton.edu/mathews/n2003/Simpson38RuleMod.html.
- Shklov, N. (December 1960). "Simpson's Rule for Unequally Spaced Ordinates". The American Mathematical Monthly 67 (10): 1022–1023. doi:10.2307/2309244.
- Süli, Endre; Mayers, David (2003). An Introduction to Numerical Analysis. Cambridge University Press. ISBN 0-521-00794-1.
- Weisstein, Eric W.. "Newton-Cotes Formulas". http://mathworld.wolfram.com/Newton-CotesFormulas.html.
External links
- Hazewinkel, Michiel, ed. (2001), "Simpson formula", Encyclopedia of Mathematics, Springer Science+Business Media B.V. / Kluwer Academic Publishers, ISBN 978-1-55608-010-4, https://www.encyclopediaofmath.org/index.php?title=p/s085450
- Weisstein, Eric W.. "Simpson's Rule". http://mathworld.wolfram.com/SimpsonsRule.html.
- Simpson's 1/3rd rule of integration — Notes, PPT, Mathcad, Matlab, Mathematica, Maple at Numerical Methods for STEM undergraduate
- A detailed description of a computer implementation is described by Dorai Sitaram in Teach Yourself Scheme in Fixnum Days, Appendix C
Original source: https://en.wikipedia.org/wiki/Simpson's rule.
Read more |