Interpolation, Explained

After reading this you can fit a straight line or a smooth cubic spline through a set of data points, evaluate it at any x you like, and know when the answer is trustworthy.

What interpolation does

Interpolation builds a function that passes exactly through every data point you give it. You then read off values between the points. Suppose you measure a quantity at x = 0, 1, 2, 3, 4 and get y = 1, 2.7, 7.4, 20, 54.6. You never measured at x = 2.5, but interpolation gives you an estimate there.

The estimate depends on the shape you assume between points. A linear interpolant connects neighbors with straight segments. A cubic spline threads a smooth curve through all points so the result has no visible kinks. For the data above, linear interpolation at x = 2.5 gives the midpoint of the segment from (2, 7.4) to (3, 20), which is 13.7. A natural cubic spline gives about 13.0, slightly lower because the curve bends to stay smooth.

When to use it and when not to

Use interpolation when you trust the data points themselves and only want values between them. Sensor readings at fixed times, a table of physical constants, or values you sampled from an expensive simulation are all good cases.

Do not use interpolation to model noisy data. If each y carries measurement error, forcing the curve through every point reproduces the noise. In that situation you want a fitted trend, not an interpolant. Interpolation also says nothing reliable outside the data range.

Extrapolating past the ends is unreliable, and the calculator warns you when your query falls outside the data. A cubic spline can swing far away just outside the last point. For the demo data the spline at x = 5 already misses the true trend by a wide margin, so treat any value beyond x = 0 to x = 4 as a guess, not a measurement.

The linear formula

Linear interpolation between two neighboring points (x_0, y_0) and (x_1, y_1) uses the fraction of the way x sits between them.

y = y_0 + (x - x_0)\cdot \frac{y_1 - y_0}{x_1 - x_0}

Here x_0 and x_1 are the bracketing data x values, y_0 and y_1 their y values, and x is your query point. The ratio is the slope of the segment. At x = 2.5 with (2, 7.4) and (3, 20), the slope is (20 - 7.4) / (3 - 2) = 12.6, so y = 7.4 + 0.5 * 12.6 = 13.7.

The cubic spline formula

A natural cubic spline uses a separate cubic polynomial on each interval, chosen so the whole curve is continuous and has continuous first and second derivatives. On the interval from x_i to x_{i+1} the piece looks like this.

S_i(x) = a_i + b_i(x - x_i) + c_i(x - x_i)^2 + d_i(x - x_i)^3

The coefficient a_i is just y_i. The others come from solving a small linear system that ties neighboring pieces together at the interior points. The word natural means the second derivative is set to zero at both ends, which stops the curve from curling up artificially. The payoff is smoothness: unlike the linear version, the spline has no corners where the segments meet.

With n data points a spline solves for the second derivatives at the interior points using a tridiagonal system. For 5 points that is 3 unknowns. The calculator does this for you; the formula above is what it evaluates once the coefficients are known.

Worked example with the demo data

Load the demo: x = 0, 1, 2, 3, 4, y = 1, 2.7, 7.4, 20, 54.6, query at x = 2.5, method natural cubic spline. Those y values are close to e^x, so the true value at 2.5 is about 12.18.

  1. Find the bracketing interval. 2.5 lies between x = 2 and x = 3, so the spline uses the piece S_2.
  2. The solver sets end second derivatives to zero and matches slopes and curvatures at x = 1, 2, 3. This yields the four coefficients of S_2.
  3. Evaluate at x - x_2 = 0.5. The result is approximately 13.0.
  4. Compare methods. Linear gives 13.7, spline gives 13.0, and the true exponential value is 12.18. The spline is closer here because the data curves upward and a straight segment overshoots.

Neither method is exact, because interpolation only knows the five sampled points, not the formula behind them. The spline wins on curved data; the linear method wins when you want a conservative straight read between points.

The dots are your data. The curve is the natural cubic spline. It passes through every dot and reads about 13.0 at x = 2.5.

Reading and interpreting the result

The calculator returns one number: the interpolant evaluated at your query x. Read it as "the value the fitted curve takes there," not "the true value." The two agree only when your assumed shape matches reality.

The chart shows the whole interpolated curve so you can judge the fit by eye. Look for the query marker sitting on a smooth, sensible part of the curve. If the spline dips below or rises above all nearby data in a way the raw points do not suggest, that overshoot is a sign to switch to linear, which cannot overshoot within a segment.

Between two data points the linear and spline methods can differ. On the curved demo data at x = 2.5 the linear reading is 13.7 and the spline reading is 13.0, a gap of 0.7. On nearly straight data the two methods agree to within a rounding error.

Common mistakes

The errors below come up often. Each has a concrete symptom.

Unsorted or duplicate x values
Both methods need distinct, increasing x values. Two points with the same x give an undefined slope, since the denominator x_1 - x_0 is zero.
Mismatched list lengths
You need exactly as many y values as x values. Four x and five y cannot pair up.
Reading spline wiggle as signal
A spline through few points can overshoot between them. If the data really is monotone, an unexpected dip is an artifact, not a feature.
Extrapolating
Querying at x = 5 for demo data ending at x = 4 leaves the supported range. The spline there can run wildly off the true trend.
Too few points
A natural cubic spline needs at least 3 points to bend; with 2 points it reduces to a straight line, the same as the linear method.

Related tools

Once you have a curve, you often want to do calculus on it or compare it to a known function. These pages help.

To integrate under sampled data, the Numerical Integrator and the Riemann Sum Calculator both work from function values on an interval. If the data comes from a known formula, the Derivative & Integral Calculator gives exact results instead of estimates. To approximate a smooth function near one point rather than fit sampled data, use the Taylor & Maclaurin Series. To study zeros, extrema and inflection points of a formula, the Function Analyzer & Grapher is the right stop.

Frequently asked questions

Which method should I pick, linear or spline?

Use spline when the underlying quantity is smooth and curved, like the exponential demo data where spline (13.0) beats linear (13.7) against the true 12.18. Use linear when you want a simple, safe estimate that never overshoots and you do not trust the data to be smooth.

Why does the spline differ from the true function value?

The spline only knows your points. At x = 2.5 it returns 13.0 while the exponential the data came from returns 12.18. Adding more points near the query shrinks that gap.

Can I interpolate outside my data range?

You can enter such a query, but the result is extrapolation and the tool warns you. A natural cubic spline extends linearly past the ends because its end curvature is zero, so it misses fast-growing data badly beyond the last point.

How many points do I need?

Two points give a single straight line under either method. Three or more let the spline actually curve. More points in the region you care about improve accuracy the most.

Does the order of my points matter?

The x values must be distinct and increasing after sorting. Repeated x values break both formulas because the slope denominator becomes zero.