Function srmc::simpsons

source ·
pub fn simpsons(f: &dyn Fn(f32) -> f32, lower: f32, upper: f32, steps: i32) -> f32
Expand description

Uses Simpson’s Method to evaluate a definite integral. Function to be integrated must take on parameter of type f32, and return a value of type f32. See the example functions (sin_x and x_cubed) for examples on how to format input.

Examples

let calculated_ans1 = srmc::simpsons(&srmc::x_cubed, 2.0, 10.0, 4);
let known_ans1 = 2496.0;

assert_eq!(calculated_ans1, known_ans1);

Since this method is not exact, in the following example we are checking the function against the expected output, not the mathematically correct value of 2.

let calculated_ans2 = srmc::simpsons(&srmc::sin_x, 0.0, std::f32::consts::PI, 10);
let known_ans2 = 2.0001094;

assert_eq!(calculated_ans2, known_ans2);