Unlike previous methods of Interpolating,
Spline interpolation does not produce the same unique interpolating polynomial, as with the Lagrange method, Vandermonde matrix method, or Newton’s divided difference method. Spline interpolation addresses some of the error concerns of polynomial interpolation: low-degree polynomial interpolation can give poor approximations, while high-degree interpolating polynomials may introduce large over-correcting oscillations. Spline interpolation uses multiple lower-degree polynomials, such that high-order error is reduced. Because each spline is also using fewer terms, problems arising from using a large number of data points, such as vanishing determinants in Vandermonde matrices, can be avoided. Cubic splines create a series of piecewise cubic polynomials.
For n+1 data points:
The interpolating splines are as follows:
Where
Which is simplified by using the substitution , giving:
To guarantee the smooth continuity of the interpolating Spline , we have the following conditions:
1) So that the splines properly interpolate the given points.
2) so that two adjacent splines’ endpoints are equal.
3) so that two adjacent splines’ first derivatives at endpoints are equal.
4) so that two adjacent splines’ second derivatives at endpoints are equal.
As stated on the Wikipedia page, the first requirement imposes n+1 restrictions, while requirements 2,3,4 impose n-1 restrictions each, for a total n+1 + 3(n-1) = 4n-2. Because we are finding 4 coefficients for each of n polynomials, we need 4n restrictions. Clamped splines use fixed bounding conditions for at , while natural splines use a fixed second derivative, such that . Our analysis will use natural splines.
We will use these 4 rules and 2 bound conditions to construct a tri-diagonal matrix which can be efficiently solved, giving the coefficients of each spline.
Ensuring Rule 2:
First, the simple form of :
Rules 1 and 2 together give us:
Combining these two equations gives us:
Since :
Where
Ensuring Rule 3:
Next, we ensure first derivatives match:
Taking the derivative of the simple form of a spline, we have:
We combine , with the two previous equations, much as we did in step 1:
Ensuring Rule 4:
We repeat step 2, but for the second derivative, using:
and
We are ensuring rule 4:
Combining these, we have:
Giving:
Solving for :
With our bound conditions , we can now solve for . What follows is an algebraic manipulation to remove b and d from the equation so that c is in terms of h, y.
First we solve Eq.1 for and then increment all indices and solve for . In both cases, we divide by or respectively:
Next, we rearrange Eq.3:
Multiply both sides by and divide both sides by 2 to give:
Next, solve Eq.2 for the d term as follows:
Substitute this into the right-hand side of Eq.6 to get:
Move the c term from the right side to the left:
Combine like terms on the left side to get:
By substituting Eq.4 (for ) and Eq.5 (for ) into Eq.8 we get:
Moving both c terms to the left gives:
The terms cancel on the left and we rearrange the right side to get:
Now we solve Eq.6 for :
And for :
We can substitute Eq.10 and Eq.11 into Eq.9 and factor the , finally removing the terms:
Multiplying by 3 to remove fractions and moving the terms on the right to the left:
Combining like terms on the left:
And finally expressing the left as a combination of c:
We can now express this as a tri-diagonal matrix times c and using an efficient recursive method to solve the tri-diagonal, get the values of c:
Solving the remaining coefficients:
With c known, we can easily calculate the remaining coefficients:
We solve Eq.3 for and then increment indices:
We know that to satisfy
Finally, we can calculate using Eq.5:
Code + Example:
This python code has a function Spline(data) that takes a set of ordered x,y pairs and returns a list of tuples, where each tuple represents the values . The function splinesToPlot(splines,xn,res) takes a set of spline coefficient tuples, a right endpoint, and a grid resolution and creates X and Y vectors corresponding to the plot of the spline set. The two pictures below were generated using this python code to compare the Lagrange interpolating polynomial and Spline Interpolation using 5 data points. The function being estimated is the same as in previous sections: . The second picture is an absolute error log plot of each method.
In both images, the red dashed line represents the Cubic Spline interpolation, while the solid blue line is the Lagrange interpolating polynomial. In the first image, the black line is the actual function.