All formulas

Numerical methods

Central difference

num.central-differenceAlso called central difference, numerical derivative, finite difference, symmetric difference quotient

Dhf(x)=f(x+h)−f(x−h)2hD_{h} f(x) = \frac{f(x + h) - f(x - h)}{2 h}

Estimate of f′(x) from the two points symmetric about x. The error is h2 f‴(ξ)/6 for some ξ within h of x, second order in h; rounding adds about ε|f|/h, so the best step in float64 is near ε1/3.

Notation

ff

the function to differentiate

f is one of the test functions e×, sin x, x3 − 2x + 1, 1/(1 + e−×), exp(−x2/2), arctan x

xx

the point

x ∈ ℝ

hh

the step

h > 0

Dhf(x)D_{h} f(x)

Result: the derivative estimate

scalar

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    f(x)=x3−2x+1,x=1,h=1/2f(x) = x^{3} - 2x + 1, x = 1, h = 1/2

    Result

    Dhf(x)=54\displaystyle D_{h} f(x) = \frac{5}{4}

    ≈ 1.25

    f′(1) = 1; the difference adds h2 f‴/6 = 1/4.

  2. Given

    f(x)=ex,x=0,h=1f(x) = e^{x}, x = 0, h = 1

    Result

    Dhf(x)=sinh⁡(1)\displaystyle D_{h} f(x) = \sinh{\left(1 \right)}

    ≈ 1.17520119364

  3. Given

    f(x)=sin⁡x,x=0,h=1f(x) = \sin x, x = 0, h = 1

    Result

    Dhf(x)=sin⁡(1)\displaystyle D_{h} f(x) = \sin{\left(1 \right)}

    ≈ 0.841470984808

Common mistakes

  • Uses the forward difference

    (f(x + h) − f(x))/h is only first order: its error h f″(ξ)/2 is much larger than the central difference's.

    Fix: Evaluate at x + h and x − h and divide by 2h.

  • Divides by h instead of 2h

    The two points are 2h apart, so dividing by h doubles the estimate.

    Fix: Divide the difference by 2h.

Source

  • Press et al. 2007, §5.7

    Press, William H. et al. Numerical Recipes: The Art of Scientific Computing. 3rd ed. Cambridge University Press, 2007.