cmlabs.integrate.weddles
- cmlabs.integrate.weddles(xvals, yvals)[source]
Composite Weddle’s method for numerical integration.
\[\begin{split}\begin{aligned} \int_{a}^{b} f(x) \, dx & \approx \sum_{i=0}^{n-1} \frac{3h}{10} \bigg[f(x_i) + 5f\left(\frac{x_i + x_{i+1}}{6}\right) + f\left(\frac{2(x_i + x_{i+1})}{6}\right) + \\ & \quad + 6f\left(\frac{3(x_i + x_{i+1})}{6}\right) + f\left(\frac{4(x_i + x_{i+1})}{6}\right) + 5f\left(\frac{5(x_i + x_{i+1})}{6}\right) + f(x_{i+1})\bigg] \end{aligned}\end{split}\]- 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
Weddle’s 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 sixth-order accurate. The error is approximately:
\[|R_n(f)| \leq \frac{M(b-a)}{1400} h^6, \quad \max_{x \in [a, b]} |f^{(6)}(x)| \leq M\]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}{6}}, x_{\frac{2}{6}}, x_{\frac{3}{6}}, x_{\frac{4}{6}}, x_{\frac{5}{6}}, x_1, \ldots, x_{\frac{n}{6}}]\]so \(n\) must be a multiple of 6. The same with the \(\textit{yvals}\)
Examples
>>> # import numpy as np >>> # from cmlabs.integrate import weddles >>> # xvals = np.array([0, 1, 2, 3, 4, 5, 6]) >>> # yvals = np.array([0, 1, 4, 9, 16, 25, 36]) >>> weddles(xvals, yvals) 72.0