MathsCalculus

Interpolation
calculator.

Estimate a value between known data points with a polynomial, a spline or a line.

How it's worked out

Interpolation assumes your points are exact, and it draws a curve through every one of them. Through n + 1 points with different x values, exactly one polynomial has degree n or less. Lagrange and Newton are two ways to write that same polynomial.

Lagrange gives each y value a weight. A weight is 1 at its own point and 0 at all the others.

Lk(x)\displaystyle L_k(x)=∏j≠kx−xjxk−xj\displaystyle {}= \prod_{j \ne k} \frac{x - x_j}{x_k - x_j}
P(x)\displaystyle P(x)=∑kykLk(x)\displaystyle {}= \sum_k y_k \, L_k(x)

Newton builds the polynomial from divided differences, which are slopes of slopes. Each new point adds one more term, and the earlier terms stay as they were.

f[x0,x1]\displaystyle f[x_0, x_1]=y1−y0x1−x0\displaystyle {}= \frac{y_1 - y_0}{x_1 - x_0}
P(x)\displaystyle P(x)=c0\displaystyle {}= c_0+c1(x−x0)\displaystyle {}+ c_1 (x - x_0)+c2(x−x0)(x−x1)\displaystyle {}+ c_2 (x - x_0)(x - x_1)+…\displaystyle {}+ \dots

A natural cubic spline does not use one formula for the whole range. It fits a separate cubic between each pair of neighbors. Slope and curvature match wherever two pieces join, and the curvature is 0 at both ends. The straight line uses only the two points on either side of x.

y\displaystyle y=y0+(y1−y0)(x−x0)x1−x0\displaystyle {}= y_0 + \frac{(y_1 - y_0)(x - x_0)}{x_1 - x_0}

Worked examples

Mia reads between the rows of a printed table

Mia has five values of the Bessel function J₀, spaced 0.3 apart, and she needs the value at 1.5. That sits between the rows for 1.3 and 1.6. The polynomial of degree 4 gives 0.51182, the spline gives 0.5121308 and the straight line gives 0.5102968.

The polynomial and the spline agree to about three decimal places. The line is lower than both, because this curve bends and a straight chord cuts below it. A smooth function like this one is where a single polynomial does well.

Open this example in the tool

Raj estimates a temperature between readings

Raj logged the temperature every 3 hours: 12.1, 10.4, 13.8, 19.5 and 22.3 degrees. He wants the value at 7.5 hours. The polynomial gives 16.63, the spline 16.78 and the line 16.65. That is a spread of 0.15 degrees.

To see how far to trust that, he hides the 6 hour reading and predicts it from the other four. The spline lands 0.31 degrees from the real 13.8, and the polynomial lands 0.4 away. So his estimate is good to about half a degree, and the second decimal means nothing.

Open this example in the tool

Sam tests the methods on a square root he can check

Sam takes the points (1, 1), (4, 2), (9, 3) and (16, 4), which all lie on the square root curve, and asks for x = 7. The true value is 2.6458. The cubic gives 2.6714, the spline 2.6748 and the line between 4 and 9 gives 2.6.

Both curves are off by about 0.03, and the line by about 0.05. In powers of x, the cubic is 0.000794x³ − 0.027778x² + 0.455556x + 0.571429.

Open this example in the tool

Reading your result

Look at the gap first. When the polynomial and the spline nearly agree, the curve between your points is well pinned down. When they differ by more than 5%, the tool warns you, because the polynomial is probably swinging between points.

More points do not always help a polynomial. On evenly spaced points, large errors build up near the ends as the degree rises. This is called Runge's phenomenon. This table uses the curve y=11+25x2y = \frac{1}{1 + 25x^2} at x = 0.95, on evenly spaced points from −1 to 1.

More points on the Runge curve, at x = 0.95
PointsPolynomialSplineTrue value
5−0.15950.01460.0424
9−0.89170.04170.0424
13−3.61430.04270.0424

With 13 points the polynomial is badly wrong and even has the wrong sign, while the spline is close. That is why the tool stops at 15 points and why the spline is the safer choice for long tables.

The error formula explains it. The polynomial's error depends on a high derivative of the true function and on the product of the distances from x to every data point. That product is largest near the ends, and it is why x near the middle of your data is safer. You rarely know that derivative, so hiding a point and predicting it gives you a practical test instead.

Common mistakes

  • Reading off a value outside your data. Past the first or last x, all three figures are extrapolations. A polynomial can head off fast there, and the spline and line just carry on from the last piece.
  • Interpolating noisy measurements. A curve forced through every noisy point follows the noise too. For readings with scatter, a best-fit line from the linear regression calculator usually predicts better.
  • Entering the same x twice with different y values. No function can pass through both, and the divided differences would divide by 0. Average repeat readings first.
  • Trusting many decimals. The polynomial through x = 2, 2.75 and 4 on the curve y=1xy = \frac{1}{x} gives 0.329545 at x = 3, while the true value is 0.333333. Every digit is correct arithmetic, and the third decimal is still wrong as an estimate of the curve.

Questions people ask

Not this one. A natural spline must have zero curvature at both ends, and the cubic through the same points usually does not. On the square root points at x = 7 they give 2.6748 and 2.6714. A spline with other end rules, such as not-a-knot, does reduce to the single cubic.

Sources