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

Computes the numerical derivative 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})

IMPORTANT: The function is evaluated around at_x (with a small tolerance).

Output

  • dfdx – numerical derivative of f(x) w.r.t x @ x

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)
  • A pre-selected stepsize is scaled based on error estimates

Example

use russell_chk::deriv_central5;
struct Arguments {}
let f = |x: f64, _: &mut Arguments| f64::exp(-2.0 * x);
let args = &mut Arguments {};
let at_x = 1.0;
let d = deriv_central5(at_x, f, args);
let d_correct = -2.0 * f64::exp(-2.0 * at_x);
assert!(f64::abs(d - d_correct) < 1e-11);