pub fn deriv_and_errors_central5<A>(
    at_x: f64,
    f: fn(_: f64, _: &mut A) -> f64,
    args: &mut A,
    h: f64
) -> (f64, f64, f64)
Expand description

Computes the numerical derivative and errors using central differences with 5 points

Input

  • at_x – location for the derivative of f(x, {arguments}) w.r.t x
  • f – function f(x, {arguments})
  • args – extra arguments for f(x, {arguments})
  • h – stepsize (1e-3 recommended)

IMPORTANT: The function is evaluated in [at_x-h, at_x+h].

Output

Returns the triple (dfdx, abs_trunc_err, abs_round_err) where:

  • dfdx – numerical derivative of f(x) w.r.t x @ x
  • abs_trunc_err – estimated truncation error O(h²)
  • abs_round_err – rounding error due to cancellations

Notes

  • Computes the derivative using the 5-point rule (at_x-h, at_x-h/2, at_x, at_x+h/2, at_x+h)

Example

use russell_chk::deriv_and_errors_central5;
struct Arguments {}
let f = |x: f64, _: &mut Arguments| f64::exp(-2.0 * x);
let args = &mut Arguments {};
let at_x = 1.0;
let h = 1e-3;
let (d, err, rerr) = deriv_and_errors_central5(at_x, f, args, h);
let d_correct = -2.0 * f64::exp(-2.0 * at_x);
assert!(f64::abs(d - d_correct) < 1e-13);
assert!(err < 1e-6);
assert!(rerr < 1e-12);