Expand description
Numerical integration using a Simpson’s rule.
A popular quadrature rule (also known as Kepler’s barrel rule). It can be derived in the simplest case by replacing the integrand with a parabola that has the same function values at the end points a & b, as well as the Simpson m=(a+b)/2, which results in the integral formula S(f) = (b-a)/6 * [ f(a) + 4f(m) + f(b) ]
Dividing the interval [a, b] into N neighboring intervals of length h = (b-a)/N and applying the Simpson rule to each subinterval, the integral is given by
S(f) = h/6 * [ f(a) + f(b) + 2Sum_{k=1..N-1} f(x_k) + 4Sum_{k=1..N} f( (x_{k-1} + x_k)/2 )]
with x_k = a + k*h.
use gauss_quad::simpson::Simpson;
use approx::assert_abs_diff_eq;
use core::f64::consts::PI;
let eps = 0.001;
let n = 10;
let quad = Simpson::new(n)?;
// integrate some functions
let integrate_euler = quad.integrate(0.0, 1.0, |x| x.exp());
assert_abs_diff_eq!(integrate_euler, 1.0_f64.exp() - 1.0, epsilon = eps);
let integrate_sin = quad.integrate(-PI, PI, |x| x.sin());
assert_abs_diff_eq!(integrate_sin, 0.0, epsilon = eps);
Structs§
- A Simpson’s rule.
- The error returned by
Simpson::new
if given a degree of 0. - An owning iterator over the nodes of the rule.
- An iterator of the nodes of the rule.