Skip to main content

derivative_2_point

Function derivative_2_point 

Source
pub fn derivative_2_point<T>(
    f: &T,
    x: f64,
    step_size: Option<f64>,
) -> Result<f64, ErrorsJSL>
where T: Fn(f64) -> f64,
Expand description

The central difference (f(x + h) − f(x − h)) / 2h, a second-order approximation to f'(x).

step_size is relative: the step is h · max(|x|, 1), which suits functions that vary on the scale of x (for one that oscillates on a scale of 1 at large x, such as sin, pass a smaller step_size). The default h = ε^{1/3} ≈ 6·10⁻⁶ balances the truncation error h² f⁽³⁾ / 6 against the rounding error ε |f| / h, giving about ten correct digits for a well-scaled function.