4.8.13Numerical Methods

Numerical differentiation — forward, backward, central differences

1,592 words7 min readdifficulty · medium3 backlinks

WHAT / WHY / HOW

WHAT are we doing? Estimating f(x)f'(x) (and higher derivatives) from a table of values or a function we can sample, without an exact symbolic derivative.

WHY does it matter? In real problems ff comes from experiments, simulations, or is too messy to differentiate by hand. ODE solvers, optimisation, and PDEs all rest on these formulas.

HOW do we get the formulas? From the Taylor series. Everything below is derived, never dumped.


Derivation from first principles (Taylor series)

Taylor expansion about xx, stepping by ±h\pm h:

f(x+h)=f(x)+hf(x)+h22f(x)+h36f(x)+f(x+h)=f(x)+hf'(x)+\frac{h^2}{2}f''(x)+\frac{h^3}{6}f'''(x)+\cdots f(xh)=f(x)hf(x)+h22f(x)h36f(x)+f(x-h)=f(x)-hf'(x)+\frac{h^2}{2}f''(x)-\frac{h^3}{6}f'''(x)+\cdots

1. Forward difference

Why this step? We want f(x)f'(x) alone, so solve the first expansion for f(x)f'(x).

f(x+h)f(x)=hf(x)+h22f(x)+f(x+h)-f(x)=hf'(x)+\frac{h^2}{2}f''(x)+\cdots Divide by hh: f(x)=f(x+h)f(x)hh2f(ξ)errorf'(x)=\frac{f(x+h)-f(x)}{h}-\underbrace{\frac{h}{2}f''(\xi)}_{\text{error}}

2. Backward difference

Why this step? Same idea but solve the f(xh)f(x-h) expansion.

f(x)f(xh)=hf(x)h22f(x)+f(x)-f(x-h)=hf'(x)-\frac{h^2}{2}f''(x)+\cdots f(x)=f(x)f(xh)h+h2f(ξ)f'(x)=\frac{f(x)-f(x-h)}{h}+\frac{h}{2}f''(\xi)

3. Central difference (the star)

Why this step? Subtract the two expansions so the symmetric ff'' terms cancel.

f(x+h)f(xh)=2hf(x)+2h36f(x)+f(x+h)-f(x-h)=2hf'(x)+\frac{2h^3}{6}f'''(x)+\cdots The h22f\frac{h^2}{2}f'' terms are equal and opposite → they die. Divide by 2h2h:

f(x)=f(x+h)f(xh)2hh26f(ξ)f'(x)=\frac{f(x+h)-f(x-h)}{2h}-\frac{h^2}{6}f'''(\xi)

4. Central second derivative

Why this step? Add the two expansions so the odd (ff', ff''') terms cancel and ff'' survives.

f(x+h)+f(xh)=2f(x)+h2f(x)+h412f(x)+f(x+h)+f(x-h)=2f(x)+h^2 f''(x)+\frac{h^4}{12}f''''(x)+\cdots Solve for ff'':

Figure — Numerical differentiation — forward, backward, central differences

Worked examples


Common mistakes (steel-manned)


80/20 — what to actually remember

Recall Feynman: explain to a 12-year-old

Imagine you're on a hill and want to know how steep it is right where you stand.

  • Forward: you take one step forward, see how much higher/lower you got, that's your guess of the steepness.
  • Backward: same but you step back.
  • Central: you check one step forward AND one step back, and use the difference between those two spots. Because you peeked on both sides, your steepness guess is much more honest — the little bumps cancel out. If you take giant steps (hh big) you miss small bumps; if you take insanely tiny steps your ruler isn't precise enough and the answer gets noisy. So pick a sensibly small step.

Flashcards

Forward difference formula for f(x)f'(x)?
f(x+h)f(x)h\dfrac{f(x+h)-f(x)}{h}, error O(h)O(h).
Backward difference formula for f(x)f'(x)?
f(x)f(xh)h\dfrac{f(x)-f(x-h)}{h}, error O(h)O(h).
Central difference for f(x)f'(x) and its order?
f(x+h)f(xh)2h\dfrac{f(x+h)-f(x-h)}{2h}, error O(h2)O(h^2).
Central second-derivative formula?
f(x+h)2f(x)+f(xh)h2\dfrac{f(x+h)-2f(x)+f(x-h)}{h^2}, error O(h2)O(h^2).
Why is central more accurate than forward?
Subtracting the two Taylor expansions cancels the ff'' term, removing the O(h)O(h) error, leaving O(h2)O(h^2).
What leading error term does forward difference have?
h2f(ξ)-\dfrac{h}{2}f''(\xi).
What leading error term does central first-difference have?
h26f(ξ)-\dfrac{h^2}{6}f'''(\xi).
Why can't you make hh arbitrarily small?
Roundoff/cancellation error grows like ε/h\varepsilon/h; total error has an optimum at finite hh.
Trick to derive an even-derivative formula?
ADD the f(x+h)f(x+h) and f(xh)f(x-h) Taylor series (odd terms cancel).
When must you use forward or backward instead of central?
At the edges of a data table where one neighbour is missing.

Connections

Concept Map

cannot reach h to 0

derives all formulas

derives all formulas

subtract expansions

add expansions

uses x and x+h

uses x-h and x

averages both views

error

error

f'' terms cancel

odd terms cancel

more accurate

Derivative as limit

Finite step h approximation

Taylor series expansion

Forward difference

Backward difference

Central difference

Central second derivative

O of h first order

O of h squared second order

Central preferred

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, derivative ka matlab hai slope — kisi point pe function kitni tezi se badal raha hai. Asli formula mein hum h0h\to0 karte hain, lekin computer infinitesimal nahi samajhta, sirf finite numbers samajhta hai. Isliye hum chhota sa finite hh lete hain aur slope nikaal lete hain. Forward difference aage ka point use karta hai (xx aur x+hx+h), backward peeche ka (xhx-h aur xx), aur central dono side dekh ke average jaisa kaam karta hai (xhx-h aur x+hx+h).

Sabse important baat: ye saare formula Taylor series se nikalte hain — koi rata-rataya formula nahi hai. Jab tum f(x+h)f(x+h) aur f(xh)f(x-h) ke expansions ko subtract karte ho, to ff'' wala term cancel ho jata hai, isliye central difference ka error O(h2)O(h^2) ban jata hai — yaani forward/backward (O(h)O(h)) se kahin zyada accurate, woh bhi same hh pe free mein! Aur agar tum dono expansions ko add karoge, to odd terms cancel ho jate hain aur tumhe ff'' ka formula mil jata hai. Yaad rakhne ka mantra: "Subtract for odd, Add for even".

Ek bada trap: log sochte hain ki hh jitna chhota utna achha. Galat! Bahut chhota hh lene par numerator mein do almost-equal numbers ka difference aata hai, aur computer ki rounding error ε/h\varepsilon/h ki tarah badh jaati hai — answer noisy ho jata hai. Toh ek optimal hh hota hai, na bahut bada na bahut chhota. Exam aur real-world dono mein central difference default choice hai, aur forward/backward sirf table ke edges pe use karo jahan ek neighbour available nahi hota.

Go deeper — visual, from zero

Test yourself — Numerical Methods

Connections