rust_scicrate 0.1.0

A scientific computing library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use crate::error::SciError;

pub fn trapezoidal<F: Fn(f64) -> f64>(f: F, a: f64, b: f64, n: usize) -> Result<f64, SciError> {
    if a >= b || n == 0 {
        return Err(SciError::InvalidInput("Invalid input parameters".to_string()));
    }

    let h = (b - a) / n as f64;
    let mut sum = 0.5 * (f(a) + f(b));
    for i in 1..n {
        sum += f(a + i as f64 * h);
    }
    Ok(h * sum)
}