Back to the main page


Interpolation

Tables of functions usually give the values at equally spaced points. Interpolation is used to find the value at some intermediate point.

Linear interpolation

Assume that the tabulated values f0 and f1 correspond to times t0 and t1 = t0+h, respectively.

The value at an intermediate point t is then

f = f0 + (t - t0) / (t1 - t0) * (f1 - f0).

This is ok if the function changes very slowly or if high precision is not needed.

Quadratic interpolation

A more precise quadratic interpolation uses a second degree polynomial going through three successive points.

Assume that the tabulated values corresponding to times t-1= t0-h, t0, t1 = t0+h are f-1, f0, f1, respectively.

Begin by calculating the ratio of the time difference to the tabulation interval:

D = (t - t0) / h.

The value of the function f at the moment t is then

f = f0 + (D / 2) (f1 - f-1) + (D / 2)2 (f1 - 2 f0 + f-1).

This works quite well for e.g. coordinates of celestial bodies.

Example

The file sun.dat gives the coordinates of the Sun in 2009. There we find the following entries
 2009  3  1 22.80237  -7.6165 
 2009  3  2 22.86482  -7.2356 
 2009  3  3 22.92712  -6.8531 
We would like to get the declination on March 2, at 10:00 UTC. Now

D = (10 - 0) / 24 = 0.4167,
f-1 = -7.6165,
f0 = -7.2356,
f1 = -6.8531,

and

f = -7.2356 + (0.4167 / 2) (-6.8531 - (-7.6165)) + (0.4167 / 2)2 (-6.8531 + 2 * 7.2356 - 7.6165)
= -7.0765.