Expand description
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.
use gauss_quad::midpoint::{Midpoint, MidpointError};
use approx::assert_abs_diff_eq;
use core::f64::consts::PI;
let eps = 0.001;
let n = 30;
let quad = Midpoint::new(n)?;
// integrate some functions
let two_thirds = quad.integrate(-1.0, 1.0, |x| x * x);
assert_abs_diff_eq!(two_thirds, 0.66666, epsilon = eps);
let estimate_sin = quad.integrate(-PI, PI, |x| x.sin());
assert_abs_diff_eq!(estimate_sin, 0.0, epsilon = eps);
// some functions need more steps than others
let m = 100;
let better_quad = Midpoint::new(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_abs_diff_eq!(0.135257, piecewise, epsilon = eps);
Structs§
- A midpoint rule.
- The error returned by
Midpoint::new
if given a degree of 0. - An owning iterator over the nodes of the rule.
- An iterator of the nodes of the rule.