Skip to content
Glacius
CalculusConcept reference

Finite differences

A centered finite difference estimates a derivative by dividing the output difference at symmetric input points by their separation.

On this page 8 sections
  1. Overview
  2. A finite difference estimates a derivative by evaluating the function at nearby inputs
  3. A finite step gives an estimate, which need not equal the exact derivative
  4. Both endpoints must lie in the domain
  5. A closer look
  6. Key takeaway
  7. Sources & further reading
  8. Concept connections

01A finite difference estimates a derivative by evaluating the function at nearby inputs#

A finite difference estimates a derivative by evaluating the function at nearby inputs. It can check a derivative implementation because it uses function values rather than the same symbolic derivative rules. The result is an approximation with a step-size tradeoff.

A centered finite difference samples the function on both sides of the input. With a positive step hh, the endpoints are xhx-h and x+hx+h.

A schematic symmetric-sample diagram, not a plot of any specific function. The two input endpoints x−h and x+h lie equally far from x. Their output values are labeled f(x−h) and f(x+h). The horizontal endpoint separation is 2h.A schematic symmetric-sample diagram, not a plot of any specific function. The two input endpoints x−h and x+h lie equally far from x. Their output values are labeled f(x−h) and f(x+h). The horizontal endpoint separation is 2h.
Figure 1A schematic symmetric-sample diagram, not a plot of any specific function. The two input endpoints x−h and x+h lie equally far from x. Their output values are labeled f(x−h) and f(x+h). The horizontal endpoint separation is 2h.
Link to this figure ↗Download SVGDownload PNG

Estimate f(x)f\prime(x) by dividing the output difference by the full spacing 2h2h:

f(x+h)f(xh)2h\frac{f(x+h)-f(x-h)}{2h}

For h=0.1h=0.1, values f(xh)=1.7f(x-h)=1.7 and f(x+h)=2.3f(x+h)=2.3 give an estimate (2.31.7)/0.2=3(2.3-1.7)/0.2=3.

For f(x)=x2f(x)=x^2 at x=2x=2 with h=0.1h=0.1, the centered numerator is f(2.1)f(1.9)=4.413.61=0.8f(2.1)-f(1.9)=4.41-3.61=0.8. Divide by 2h=0.22h=0.2 to get 4. The denominator is the full distance between the two sampled inputs, not the distance from the center to one sample.

Check your reasoning

With h=0.25h=0.25, f(xh)=4.5f(x-h)=4.5 and f(x+h)=6.5f(x+h)=6.5. Find the centered derivative estimate.

Show answer and explanation
4

Divide the output difference by the full spacing: (6.5(4.5))/0.5=4(6.5-(4.5))/0.5=4.

02A finite step gives an estimate, which need not equal the exact derivative#

A finite step gives an estimate, which need not equal the exact derivative. For f(x)=x3f(x)=x^3 at x=1x=1 with h=0.5h=0.5, the endpoint outputs are 0.1250.125 and 3.3753.375. The estimate is 3.253.25; the exact derivative there is 33.

Use both endpoint evaluations even when the value at the center is known.

Check your reasoning

A scalar response is f(x)=x3f(x)=x^3. Estimate its derivative at x=3x=3 with centered step h=0.5h=0.5.

Show answer and explanation
27.25

The endpoint outputs are 15.62515.625 and 42.87542.875. Their difference divided by 11 gives 27.2527.25.

03Both endpoints must lie in the domain#

Both endpoints must lie in the domain. A centered slope across a corner need not be a derivative. For smooth functions, subtracting nearly equal floating-point outputs can lose accuracy.

Smaller steps can reduce approximation error while increasing roundoff. They do not always improve the estimate.

Check your reasoning

With h=0.05h=0.05, f(xh)=1.8f(x-h)=1.8 and f(x+h)=2.2f(x+h)=2.2. The calculation divides by hh. Repair the centered estimate.

Show answer and explanation
4

Divide the output difference by the full spacing: (2.2(1.8))/0.1=4(2.2-(1.8))/0.1=4.

04A closer look#

A large step may average over curvature; an extremely small one can magnify numerical rounding when nearly equal outputs are subtracted. Finite-difference agreement is useful evidence for a gradient implementation, but one successful point does not check every input or edge case.

Key takeaway

Subtract the two symmetric outputs and divide by 2h; the result is a finite-step estimate.

  • Estimate a derivative with a centered finite difference.

Sources & further reading

  1. [1]
    Stanford CS231n: Gradient ChecksStanford CS231n · Article

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “Finite differences.” Math behind ML. /learn/c-finite-difference