Numeric derivative

From CodeCodex

Compute the numerical approximation to the derivative of a function: (f(x+d) - f(x-d)) / (2 d).

Implementations[edit]

OCaml[edit]

# let d dx f x =
    (f (x +. dx) -. f (x -. dx)) /. (2. *. dx);;
val d : float -> (float -> float) -> float -> float
# let d = d (sqrt epsilon_float);;
val d : (float -> float) -> float -> float

For example:

# let f x = x ** 3. -. x -. 1.;;
val f : float -> float
# let f' = d f;;
val f' : float -> float
# f' 7.;;
- : float = 146.