Interpolation with Spline Functions
Splines are functions which match given values at the points x1,...,xNT, shown in Figure 1, and have continuous derivatives up to some order at the knots, or the points x2,...,xNT1. Cubic splines are most common. In this case the function is represented by a cubic polynomial within each interval and has continuous first and second derivatives at the knots. Two more conditions can be specified arbitrarily. These are usually the second derivatives at the two end-points, which are commonly taken as zero; this gives the natural cubic splines.
Consider the points shown in Figure 1a. The notation for each interval is shown in Figure1b. Within each interval the function is represented as a cubic polynomial.
Figure 1. Finite Elements for Cubic Splines
The interpolating function takes on specified values at the knots.
Given the set of values {xi,f(xi)}it is desired to pass a smooth curve through those points, and the curve shall have continuous first and second derivatives at the knots.
The formulas for the cubic spline are derived as follows for one region. Since the function is a cubic function the third derivative is constant and the second derivative is linear in x. Write it as
Integrate this once to give
and once more to give
Now define
so that
A number of algebraic steps make the interpolation easy. Write these formulas for the i-th element as well as the i1-th element. Then apply the continuity conditions for the first and second derivative and eliminate the values yi' and y"i-1 [see Ortega and Poole, p. 163, 1981]. The result is
This is a tri-diagonal system for the set of {y''i} in terms of the set of {yi}. Since the continuity condtions apply only for i=2,...,NT-1 we have only NT2 conditions for the NT values of y''i. Two additional conditions are needed, and these are usually taken as the value of the second derivative at each end of the domain, y''1, y''NT. If these values are zero we get the natural cubic splines; they can also be set to achieve some other purpose, such as making the first derivative match some desired condition at the two ends. With these values taken as zero, in the natural cubic spline, we have a NT2 system of tridiagonal equations, which is easily solved (link). Once the second derivatives are known at each of the knots the first derivatives are given by
The function itself is then known within each element.
The spline interplation is easily done in Matlab. The following code supplies a vector y(x), fits those points to a natural spline [pp = spline(x,y)], evaluates the spline at a set of points xx [v=ppval(pp,xx);], and then plots the spline (in blue) as well as the knots (in red). Since the original function is a cubic function, the spline interpolation is exact.
»x = [0 1 2 3 4];
»y = [0 1 8 27 64];
»pp=spline(x,y);
»xx = 0:.05:4;
»v=ppval(pp,xx);
»plot(xx,v,'b-',x,y,'ro')
»xlabel('x')
»ylabel('y')
»title('Spline Interpolation')
Other interpolation schemes are: global polynomials as powers of x that go through a fixed number of points; orthogonal polynomials of x that give a best fit; rational polynomials that are ratios of polynomials; piecewise polynomials derived with forward differences (points to the right) and backward differences (points to the left); and finite elements.