[−][src]Module gauss_quad::midpoint
Numerical integration using the midpoint rule.
This is one of the simplest integration schemes.
- Divide the domain into equally sized sections.
- Find the function value at the midpoint of each section.
- The section's integral is approximated as a rectangle as wide as the section and as tall as the function value at the midpoint.
extern crate gauss_quad; use gauss_quad::{Midpoint}; #[macro_use] extern crate assert_float_eq; use std::f64::consts::PI; fn main() { let eps = 0.001; let n = 30; let quad = Midpoint::init(n); // integrate some functions let two_thirds = quad.integrate(-1.0, 1.0, |x| x * x); assert_float_absolute_eq!(two_thirds, 0.66666, eps); let estimate_sin = quad.integrate(-PI, PI, |x| x.sin()); assert_float_absolute_eq!(estimate_sin, 0.0, eps); // some functions need more steps than others let m = 100; let better_quad = Midpoint::init(m); let piecewise = better_quad.integrate(-5.0, 5.0, |x| if x > 1.0 && x < 2.0 { (-x * x).exp() } else { 0.0 }); assert_float_absolute_eq!(0.135257, piecewise, eps); }
Structs
Midpoint | A midpoint rule quadrature scheme. |