Function diff2

Calculate the derivative of a function using a two-point formula, i.e. a forward- or backward-difference formula.

real diff2(Func) (
  scope Func f,
  real x,
  real fx,
  real scale = 1.00000
);

This method only evaluates the function once (in addition to the function value provided by the user), and is therefore the fastest way to compute a derivative. However, it is also the least accurate method, and should only be used if the function is very expensive to compute and you have already calculated f(x).

Parameters

NameDescription
f The function to differentiate.
x The point at which to take the derivative.
fx The function value at x, i.e. f(x)
scale A characteristic scale for f. When this is positive, the forward-difference formula is used, and when it is negative, the backward-difference formula is used.

Returns

An approximation to the derivative of f at the point x. The relative error in the result is at best on the order of sqrt(real.epsilon). Usually it is much higher.