Numerical methods
Central difference
num.central-differenceAlso called central difference, numerical derivative, finite difference, symmetric difference quotient
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
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
the point
x ∈ ℝ
the step
h > 0
Result: the derivative estimate
scalar
Worked examples
Each result is solved exactly and confirmed by two independent computations.
Given
Result
≈ 1.25
f′(1) = 1; the difference adds h2 f‴/6 = 1/4.
Given
Result
≈ 1.17520119364
Given
Result
≈ 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.