Finite Difference on Finite Difference

September 05, 2026

I was recently writing a finite-difference test for an analytic acceleration implementation.

Suppose we have a smooth trajectory x(t)x(t), and the implementation gives its acceleration a(t)=x(t)a(t)=x''(t). To test it independently, we can estimate velocity with a central difference,

v^(t)=x(t+hv)x(thv)2hv,\hat v(t) = \frac{x(t+h_v)-x(t-h_v)}{2h_v},

and then finite-difference that estimate again:

a^(t)=v^(t+ha)v^(tha)2ha.\hat a(t) = \frac{\hat v(t+h_a)-\hat v(t-h_a)}{2h_a}.

The question is how much numerical error to expect in a^(t)\hat a(t). That determines the finite-difference step sizes and, in turn, the test tolerance.

One Central Difference

For a smooth function f(t)f(t),

f(t+h)f(th)2h=f(t)+h26f(3)(t)+O(h4).\frac{f(t+h)-f(t-h)}{2h} = f'(t) + \frac{h^2}{6}f^{(3)}(t) + O(h^4).

So the truncation error is O(h2)O(h^2).

Floating-point arithmetic adds another source of error. If we ignore scale factors and other constants, the subtraction f(t+h)f(th)f(t+h)-f(t-h) carries O(ϵ)O(\epsilon) roundoff, where ϵ\epsilon is machine epsilon. Dividing by hh turns this into O(ϵ/h)O(\epsilon/h).

A simple error model is therefore

E(h)=Cth2+Crϵh,E(h) = C_t h^2 + C_r\frac{\epsilon}{h},

where CtC_t and CrC_r collect the truncation and roundoff constants.

The optimal step minimizes E(h)E(h). Setting dE/dh=0dE/dh=0 gives

hopt=(Cr2Ctϵ)1/3,h_{\mathrm{opt}} = \left( \frac{C_r}{2C_t}\epsilon \right)^{1/3},

so, up to constants,

hoptϵ1/3.h_{\mathrm{opt}}\sim\epsilon^{1/3}.

The ratio Cr/CtC_r/C_t only enters through a cube root, so the optimal step is not very sensitive to moderate errors in these constants. For order-of-magnitude reasoning, treating them as order one is usually enough.

A Naive Nested Error Model

The velocity estimate has error

v^(t)v(t)=O(hv2)+O(ϵhv).\hat v(t)-v(t) = O(h_v^2) + O\left(\frac{\epsilon}{h_v}\right).

The acceleration estimate applies another central difference and divides by hah_a. If we treat both parts of the inner error as independent perturbations at each time sample, we would get

Ea=?O(ha2)+O(hv2ha)+O(ϵhvha).E_a \stackrel{?}{=} O(h_a^2) + O\left(\frac{h_v^2}{h_a}\right) + O\left(\frac{\epsilon}{h_vh_a}\right).

The problem with this model is that the inner truncation error is not an independent perturbation at each time sample. It has smooth temporal structure.

The Inner Truncation Error Is Smooth

The inner central difference has the more specific expansion

v^(t)=x(t)+hv26x(3)(t)+O(hv4).\hat v(t) = x'(t) + \frac{h_v^2}{6}x^{(3)}(t) + O(h_v^4).

Its leading truncation error is therefore hv26x(3)(t)\frac{h_v^2}{6}x^{(3)}(t), which varies smoothly with tt.

Substituting this expansion into the outer central difference gives

a^(t)=x(t)+ha2+hv26x(4)(t)+O(ha4+ha2hv2+hv4).\hat a(t) = x''(t) + \frac{h_a^2+h_v^2}{6}x^{(4)}(t) + O\left( h_a^4+h_a^2h_v^2+h_v^4 \right).

Remark

The outer difference is applied to v^\hat v as a function of tt, which gives

a^(t)=v^(t)+ha26v^(3)(t)+O(ha4).\hat a(t) = \hat v'(t) + \frac{h_a^2}{6}\hat v^{(3)}(t) + O(h_a^4).

Evaluating v^\hat v' and v^(3)\hat v^{(3)} uses the fact that the derivative operator and a constant shift operator commute. The central difference is a combination of two such shifts, so

v^(k)(t)=x(k)(t+hv)x(k)(thv)2hv.\hat v^{(k)}(t) = \frac{x^{(k)}(t+h_v)-x^{(k)}(t-h_v)}{2h_v}.

Each is then a central difference of xx' and x(3)x^{(3)}, giving v^=x+hv26x(4)+O(hv4)\hat v'=x''+\frac{h_v^2}{6}x^{(4)}+O(h_v^4) and v^(3)=x(4)+O(hv2)\hat v^{(3)}=x^{(4)}+O(h_v^2). Substituting recovers the expansion above.

So the truncation error is

a^(t)a(t)=O(ha2)+O(hv2).\hat a(t)-a(t) = O(h_a^2)+O(h_v^2).

The second finite difference does not turn the inner O(hv2)O(h_v^2) truncation error into O(hv2/ha)O(h_v^2/h_a).

The outer difference does divide by hah_a, but the change in the inner truncation bias between that-h_a and t+hat+h_a is itself O(hahv2)O(h_a h_v^2). The hah_a cancels.

Roundoff does not have the same smooth dependence on time. The inner roundoff is O(ϵ/hv)O(\epsilon/h_v), and the outer finite difference introduces another factor of 1/ha1/h_a.

The leading error model is therefore

E(hv,ha)=Ct(hv2+ha2)+Crϵhvha.E(h_v,h_a) = C_t\left(h_v^2+h_a^2\right) + C_r\frac{\epsilon}{h_vh_a}.

Choosing hvh_v and hah_a

We can choose both step sizes by minimizing this error model.

The optimum satisfies

Ehv=2CthvCrϵhv2ha=0,\frac{\partial E}{\partial h_v} = 2C_t h_v - C_r\frac{\epsilon}{h_v^2h_a} = 0,

and

Eha=2CthaCrϵhvha2=0.\frac{\partial E}{\partial h_a} = 2C_t h_a - C_r\frac{\epsilon}{h_vh_a^2} = 0.

These two equations imply hv=hah_v=h_a.

So equal inner and outer step sizes are a consequence of this model, not an assumption.

Writing both as hh,

E(h)=2Cth2+Crϵh2.E(h) = 2C_t h^2 + C_r\frac{\epsilon}{h^2}.

Setting dE/dh=0dE/dh=0 gives

hopt=(Cr2Ctϵ)1/4.h_{\mathrm{opt}} = \left( \frac{C_r}{2C_t}\epsilon \right)^{1/4}.

Therefore,

hvopthaoptϵ1/4.h_v^{\mathrm{opt}} \sim h_a^{\mathrm{opt}} \sim \epsilon^{1/4}.

For double precision, ϵ1/4\epsilon^{1/4} is roughly 10410^{-4}.

The constants matter even less here than in the first-derivative case, since their ratio only enters through a fourth root.

At the optimum, the minimum error scales as

Eminϵ1/2.E_{\min}\sim\epsilon^{1/2}.

This gives the expected numerical floor up to the problem-dependent constants that the simple model leaves out.

For a test, a reasonable workflow is to choose hvh_v and hah_a near this scale, sweep around them numerically, observe the actual error floor, and set the tolerance above that floor with some margin.

Takeaway

The key point is that finite-difference truncation error has temporal structure.

For a smooth trajectory, the leading truncation bias is itself smooth in time. Applying another finite difference therefore differentiates that bias; it does not simply divide its magnitude by the new step size.

That is why the inner O(hv2)O(h_v^2) truncation error remains O(hv2)O(h_v^2), while roundoff picks up the additional 1/ha1/h_a.