cmlabs.integrate.simpsonc
- cmlabs.integrate.simpsonc(xvals, yvals)[source]
Composite Simpson’s 3/8 method using cubic polynomial.
\[\begin{gather} \int_{a}^{b} f(x) \, dx \approx \sum_{i=0}^{n-1} \frac{3h}{8} \left[ f(x_i) + 3f\left(\frac{x_i + x_{i+1}}{3}\right) + 3f\left(\frac{2(x_i + x_{i+1})}{3}\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 3/8 rule approximates the area under the curve by dividing it into cubic polynomials. The height of each polynomial is determined by the function value at the endpoints and midpoints of the interval.
This method is fourth-order accurate. The error is approximately:
\[\begin{split}\begin{gather} R_{3,i}(f) = \int_{x_i}^{x_{i+1}} \left(f(x) - L_{3, i}(x) \right) dx = -\frac{3h^5}{80}f^{(4)}(\xi_i) \\ R_n(f) = \sum_{i=0}^{n-1} R_{3,i}(f) = \sum_{i=0}^{n-1} - \frac{3h^5}{80}f^{(4)}(\xi_i), \quad \xi_i \in [x_i, x_{i+1}] \\ R_n(f) = -\frac{3h^5}{80} \cdot \frac{b-a}{3h} f^{(4)}(\xi) = -\frac{f^{(4)}(\xi)(b-a)}{80} h^4, \quad \xi \in [a, b] \\ \quad \xi \in [a, b] \\ |R_n(f)| \leq \frac{M(b-a)}{80} 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}{3}}, x_{\frac{2}{3}}, x_1, x_{\frac{4}{3}}, x_{\frac{5}{3}}, x_2, \ldots, x_{\frac{n}{3}}]\]so \(n\) must be a multiple of 3. The same with the \(\textit{yvals}\)
Examples
>>> # import numpy as np >>> # from cmlabs.integrate import simpsonc >>> # xvals = np.array([0, 1, 2, 3, 4, 5, 6]) >>> # yvals = np.array([0, 1, 4, 9, 16, 25, 36]) >>> simpsonc(xvals, yvals) 72.0