[Math] Linear Spline Interpolation

interpolationnumerical methodsspline

Can someone explain to me how linear splines work and what formulas are used. I can only seem to find information on cubic splines. Which I don't really understand either

Specifically, if I were given 5 data points how would i make a linear and cubic spline?

Best Answer

You're correct that a linear spline is just a sequence of straight lines. So, in between the data points $P_{i-1} = (x_{i-1}, y_{i-1})$ and $P_{i} = (x_{i}, y_{i})$, the equation of the spline is just $$ y = \frac{x_i - x}{x_i - x_{i-1}}y_{i-1} + \frac{x - x_{i-1}}{x_i - x_{i-1}}y_{i} $$ You can easily confirm that $y(x_{i-1}) = y_{i-1}$ and $y(x_{i}) = y_{i}$, so the linear pieces join properly, with no discontinuity.

Cubic splines are covered in many places. The basic idea is to assume that each segment of the curve (between two successive data points) is a cubic polynomial, whose coefficients are to be determined. We want these cubic pieces to join smoothly; specifically, where they meet, we want their first and second derivative values to match. You write down equations that express this derivative matching. You get a system of linear equations in which the unknowns are the coefficients of the polynomial pieces. The system of equations is nicely "banded", and therefore easy to solve.

For further details, see here, or here, or this answer.

Computing cubic splines is much easier if you express each segment in Hermite form, rather than algebraic form. This ensures from the outset that values and first derivatives match, and you only have to solve a linear system that forces second derivatives to match, too. The size of your system of equations is much smaller -- you have roughly $N$ unknowns, instead of $4N$.