Interpolation
calculator.
Estimate a value between known data points with a polynomial, a spline or a line.
| # | x | y |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 |
Using 5 data points. Edit them below the answer.
- Natural cubic spline
- 0.512131
- Straight line
- 0.510297
The polynomial and the spline are 0.0003 apart, so the curve is well pinned down here. The straight line is 0.0015 away, which shows how much the bend matters between these neighbors.
| # | x | y |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 |
The curves through your points
Drag the point to move xThe 5 data points with the polynomial of degree 4, the natural cubic spline and straight lines between neighbors. At x = 1.5 the polynomial gives 0.51182, the spline 0.512131 and the line 0.510297.
Working
- 1Sort the points and check that every x differs(1, 0.765198), (1.3, 0.620086), (1.6, 0.455402), (1.9, 0.281819), (2.2, 0.110362)
- 2Divided differences: the top entry of each order is one of Newton's coefficients0.765198, −0.483706, −0.108734, 0.065878, 0.001825
- 3Newton form: each term multiplies in one more distance to a data pointP(1.5) = 0.765198 + (−0.483706)(1.5 − 1) + (−0.108734)(1.5 − 1)(1.5 − 1.3) + 0.065878(1.5 − 1)(1.5 − 1.3)(1.5 − 1.6) + 0.001825(1.5 − 1)(1.5 − 1.3)(1.5 − 1.6)(1.5 − 1.9) = 0.51182
- 4The same polynomial written out in powers of xP(x) = 0.001825x⁴ + 0.055293x³ − 0.343047x² + 0.073391x + 0.977735
- 5Straight line between the neighbors (1.3, 0.620086) and (1.6, 0.455402)Line at 1.5 = 0.510297
- 6Check with Lagrange's weights, which use no divided differenceThe sum of each y times its weight (−0.028807, 0.288066, 0.864198, −0.144033, 0.020576) = 0.51182
- 7Check that the polynomial meets every data pointLargest miss: below 10⁻¹⁰
Divided differences
| From x | To x | Difference | |
|---|---|---|---|
| 0 | 1 | 1 | 0.765198 |
| 0 | 1.3 | 1.3 | 0.620086 |
| 0 | 1.6 | 1.6 | 0.455402 |
| 0 | 1.9 | 1.9 | 0.281819 |
| 0 | 2.2 | 2.2 | 0.110362 |
| 1 | 1 | 1.3 | −0.483706 |
| 1 | 1.3 | 1.6 | −0.548946 |
| 1 | 1.6 | 1.9 | −0.578612 |
| 1 | 1.9 | 2.2 | −0.571521 |
| 2 | 1 | 1.6 | −0.108734 |
Each row is one entry of the triangular table. Rows starting at the first x, in bold, are the coefficients in Newton's form.
Hide each point and predict it
| Hidden x | Its y | Poly miss | Spline miss |
|---|---|---|---|
| 1, end | 0.765198 | −0.0004 | 0.0196 |
| 1.3 | 0.620086 | 8.9 × 10⁻⁵ | −0.0048 |
| 1.6 | 0.455402 | −5.9 × 10⁻⁵ | 0.0005 |
| 1.9 | 0.281819 | 8.9 × 10⁻⁵ | 0.0018 |
| 2.2, end | 0.110362 | −0.0004 | −0.0021 |
Each row refits the curves without that point, then compares them with its real y. Small misses mean the curves can be trusted between points. An end point is predicted from outside the other data, so a large miss there is normal.
Many x values at once
| # | x |
|---|---|
| 1 | |
| 2 | |
| 3 |
All three estimates
| x | Polynomial | Spline | Line |
|---|---|---|---|
| 1.1 | 0.719646 | 0.718181Line 0.716827 | 0.716827 |
| 1.5 | 0.51182 | 0.512131Line 0.510297 | 0.510297 |
| 2 | 0.223875 | 0.224349Line 0.224667 | 0.224667 |
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.
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.
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.
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.
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.
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.
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 at x = 0.95, on evenly spaced points from −1 to 1.
| Points | Polynomial | Spline | True value |
|---|---|---|---|
| 5 | −0.1595 | 0.0146 | 0.0424 |
| 9 | −0.8917 | 0.0417 | 0.0424 |
| 13 | −3.6143 | 0.0427 | 0.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 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.