cmlabs.integrate.simpsonq
- cmlabs.integrate.simpsonq(xvals, yvals)[source]
Composite Simpson’s 1/3 method using quadratic polynomial.
\[\begin{gather} \int_{a}^{b} f(x) \, dx \approx \sum_{i=0}^{n-1} \frac{h}{3} \left[ f(x_i) + 4f\left(\frac{x_i + x_{i+1}}{2}\right) + f(x_{i+1})\right] \end{gather}\]- Parameters:
xvals (array_like, 1-D) – The sorted x-coordinates of the data points.
yvals (array_like, 1-D) – The y-coordinates of the data points, i.e., \(f(x)\).
- Returns:
The approximate value of the integral.
- Return type:
float
Notes
Simpson’s 1/3 rule approximates the area under the curve by dividing it into parabolas. The height of each parabola is determined by the function value at the endpoints and the midpoint of the interval.
This method is fourth-order accurate. The error is approximately:
\[\begin{split}\begin{gather} R_{2,i}(f) = \int_{x_i}^{x_{i+1}} \left(f(x) - L_{2, i}(x) \right) dx = -\frac{h^5}{90}f^{(4)}(\xi_i) \\ R_n(f) = \sum_{i=0}^{n-1} R_{2,i}(f) = \sum_{i=0}^{n-1} - \frac{h^5}{90}f^{(4)}(\xi_i), \quad \xi_i \in [x_i, x_{i+1}] \\ R_n(f) = -\frac{h^5}{90} \cdot \frac{b-a}{2h} f^{(4)}(\xi) = -\frac{f(\xi)(b-a)}{180} h^4, \quad \xi \in [a, b] \\ |R_n(f)| \leq \frac{M(b-a)}{180} h^4, \quad \max_{x \in [a, b]} |f^{(4)}(x)| \leq M \end{gather}\end{split}\]where \(h\) is the width of the rectangles and depends on the number of intervals \(n\).
The \(\textit{xvals}\) array is treated as
\[[x_0, x_{\frac{1}{2}}, x_1, x_{\frac{3}{2}}, x_2, \ldots, x_{\frac{n}{2}}]\]so \(n\) must be even. The same with the \(\textit{yvals}\) array.
Examples
>>> # import numpy as np >>> # from cmlabs.integrate import simpsonq >>> # xvals = np.array([0, 1, 2, 3, 4]) >>> # yvals = np.array([0, 1, 4, 9, 16]) >>> simpsonq(xvals, yvals) 21.333333333333332