[][src]Module gauss_quad::simpson

Numerical integration using the Simpson 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.

extern crate gauss_quad; 
use gauss_quad::{Simpson}; 

#[macro_use]
extern crate assert_float_eq;

use std::f64::consts::PI;

fn main() {
    
    let eps = 0.001;
    
    let n = 10; 
    let quad = Simpson::init(n); 
    
    // integrate some functions 
    let integrate_euler = quad.integrate(0.0, 1.0, |x| x.exp()); 
    assert_float_absolute_eq!(integrate_euler, 1.0_f64.exp() - 1.0, eps); 

    let integrate_sin = quad.integrate(-PI, PI, |x| x.sin()); 
    assert_float_absolute_eq!(integrate_sin, 0.0, eps);
}

Structs

Simpson

A Simpson rule quadrature scheme.