Skip to main content

integral

Function integral 

Source
pub fn integral<T: Numeric, F: Fn(T) -> T>(
    function: &F,
    limits: [T; 2],
) -> Result<T, IntegrateError>
Expand description

The integral of a single-variable function over an interval.

This picks a method on your behalf: it walks the interval in DEFAULT_TOTAL_ITERATIONS steps using Boole’s rule, which is the strongest all-round choice for a smooth integrand. Reach for IterativeSingle to change the rule or the step count, or GaussianSingle for quadrature. Either limit may be infinite.

§Errors

IntegrateError::LimitsIllDefined if the limits are reversed, equal, NaN, or point the wrong way at an infinity, or IntegrateError::NonFinite if the integrand blows up on the way.

§Examples

use multicalc::numerical_integration::integral;
let line = |x: f64| 2.0 * x;
let limits = [0.0, 2.0];

let area = integral(&line, limits)?;                    // 2x over [0, 2] is 4
assert!((area - 4.0).abs() < 1e-9);

// a decaying integrand may run to infinity
let decay = |x: f64| (-x).exp();
let to_infinity = [0.0, f64::INFINITY];

let tail = integral(&decay, to_infinity)?;              // e^-x over [0, inf) is 1
assert!((tail - 1.0).abs() < 1e-6);