Skip to main content

5.1 Differentiation algorithms

Source: Ideas from Computational Physics (Landau, Páez, Bordeianu), Ch. 5 intro–§5.1.2. Rewritten in original wording. Continues 4.3b RNG test labs. Labs: 5.1b Differentiation labs.

This chapter opens with numerical differentiation — short but foundational — then spends most of its length on integration algorithms, ending with Monte Carlo integration (a different species). Here we only take the differentiation half through central differences.

5.1 The problem​

Figure 5.1 is the spirit of the task: a projectile path with air resistance, measured only at discrete times. You need dy/dtdy/dt as a function of tt, and there is no closed formula to differentiate by hand.

The calculus limit dy/dt=lim⁡h→0[y(t+h)−y(t)]/hdy/dt=\lim_{h\to 0}[y(t+h)-y(t)]/h is hostile on a machine. As hh shrinks, y(t+h)−y(t)y(t+h)-y(t) rides the edge of machine epsilon εm\varepsilon_m (subtractive cancellation), while dividing by a vanishing hh invites overflow. You need stable finite-hh formulas and an eye for the truncation / round-off tradeoff from Chapter 3.

Forward and central differences​

Taylor-expand one step ahead, rearrange into a finite-hh slope, then compare the forward chord (t,t+h)(t,t+h) with the central chord (t−h/2,t+h/2)(t-h/2,t+h/2). Walk the definition → rearrange → fd → cd — pick the matching equation at each step (lookalikes included).

Geometrically, forward difference is the slope of the chord from tt to t+ht+h (Figure 5.1 left); truncation is typically O(h)O(h). Central difference cancels even powers of hh, leaving leading error O(h2)O(h^{2}) when yy is smooth.

Parabola check. For y=a+bt2y=a+bt^{2}, the exact derivative is 2bt2bt, while forward difference returns 2bt+bh2bt+bh (good only when h≪1/∣b∣h\ll 1/|b|). Central difference returns the exact 2bt2bt for every hh (higher derivatives vanish).

Figure 5.1 — left: forward chord; right: central chord on a sampled trajectory.

Quiz

Finite differences

1 / 4

Why is the literal limit definition dangerous on a computer?

Pick an answer to see if you are right.

Next: 5.1b Differentiation labs — implement forward and central differences and compare errors. Then 5.2 Extrapolated difference.