# Cubic Spline Interpolation

Sky McKinley and Megan Levine Math 45: Linear Algebra
Abstract. An introduction into the theory and application of cubic splines with accompanying Matlab m-file cspline.m

Introduction
Real world numerical data is usually difficult to analyze. Any function which would effectively correlate the data would be difficult to obtain and highly unwieldy. To this end, the idea of the cubic spline was developed. Using this process, a series of unique cubic polynomials are fitted between each of the data points, with the stipulation that the curve obtained be continuous and appear smooth. These cubic splines can then be used to determine rates of change and cumulative change over an interval. In this brief introduction, we will only discuss splines which interpolate equally spaced data points, although a more robust form could encompass unequally spaced points.

Theory
The fundamental idea behind cubic spline interpolation is based on the engineer’s tool used to draw smooth curves through a number of points. This spline consists of weights attached to a flat surface at the points to be connected. A flexible strip is then bent across each of these weights, resulting in a pleasingly smooth curve. The mathematical spline is similar in principle. The points, in this case, are numerical data. The weights are the coefficients on the cubic polynomials used to interpolate the data. These coefficients ’bend’ the line so that it passes through each of the data points without any erratic behavior or breaks in continuity.

Process
The essential idea is to fit a piecewise function of the form s 1 ÝxÞ SÝxÞ = s 2 ÝxÞ if if _ s n?1 ÝxÞ if where s i is a third degree polynomial defined by s i ÝxÞ = a i Ýx ? x i Þ 3 + b i Ýx ? x i Þ 2 + c i Ýx ? x i Þ + d i for i = 1, 2, ..., n ? 1. The first and second derivatives of these n ? 1 equations are fundamental to this process, and they are (2) x n?1 ² x < x n x1 ² x < x2 x2 ² x < x3 (1)

1

Letting h = x i ? x i?1 in equation (8). SÝxÞ will be continuous on the interval ßx 1 . The piecewise function SÝxÞ will interpolate all data points. n ? 1. s vi Ýx i Þ = s vi?1 Ýx i Þ (11) . n ? 1...... we have d i = a i?1 h 3 + b i?1 h 2 + c i?1 h + d i?1 (10) (9) for i = 2. we can conclude that SÝx i Þ = y i for i = 1. x n à S vv ÝxÞ will be continuous on the interval ßx 1 . it can be concluded that each sub-function must join at the data points. Since the curve SÝxÞ must be continuous across its entire interval. the derivatives must be equal at the data points. 3. n ? 1. Also. From equation (2)... 2. . so s i Ýx i Þ = s i?1 Ýx i Þ for i = 2. 2. . SÝx i Þ = s i Ýx i Þ and we can use equation (2) to produce y i = s i Ýx i Þ y i = a i Ýx i ? x i Þ + b i Ýx i ? x i Þ + c i Ýx i ? x i Þ + d i 3 2 (5) (6) yi = di for each i = 1. that is.. . n ? 1. (3) (4) The Four Properties of Cubic Splines 1.. Our spline will need to conform to the following stipulations. to make the curve smooth across the interval. . 4.. s i Ýx i Þ = d i and s i?1 Ýx i Þ = a i?1 Ýx i ? x i?1 Þ + b i?1 Ýx i ? x i?1 Þ + c i?1 Ýx i ? x i?1 Þ + d i?1 3 2 (7) (8) so d i = a i?1 Ýx i ? x i?1 Þ 3 + b i?1 Ýx i ? x i?1 Þ 2 + c i?1 Ýx i ? x i?1 Þ + d i?1 for i = 2. 3. 2. x i+1 à.. x n à Since the piecewiece function SÝxÞ will interpolate all of the data points.2 s vi ÝxÞ = 3a i Ýx ? x i Þ 2 + 2b i Ýx ? x i Þ + c i s vv ÝxÞ = 6a i Ýx ? x i Þ + 2b i i for i = 1.. x n à S v ÝxÞ will be continuous on the interval ßx 1 .. Since x i 5 ßx i . 2. 3.. n ? 1.... 3. . n.. .

Each b i can be represented by s vv Ýx i Þ = 2b i i M i = 2b i bi = Mi 2 and d i has already been determined to be di = yi. 3. letting h = x i ? x i?1 . This and equation (14) lead us to the equation s vv Ýx i+1 Þ = 6a i Ýx i+1 ? x i Þ + 2b i i s vv Ýx i+1 Þ i+1 = 6a i Ýx i+1 ? x i Þ + 2b i and. so i s vv ÝxÞ = 6a i Ýx ? x i Þ + 2b i i s vv Ýx i Þ = 6a i Ýx i ? x i Þ + 2b i i s vv Ýx i Þ = 2b i i for i = 2. n ? 2. `. s vi Ýx i Þ = c i and s vi?1 Ýx i Þ = 3a i?1 Ýx i ? x i?1 Þ 2 + 2b i?1 Ýx i ? x i?1 Þ + c i?1 so c i = 3a i?1 Ýx i ? x i?1 Þ 2 + 2b i?1 Ýx i ? x i?1 Þ + c i?1 . c i . This makes the determination of the weights a i... Similarly. Lastly. by equation (3). s vv Ýx i Þ = s vv Ýx i Þ for i+1 i i i = 1.. letting h = x i+1 ? x i and using the conclusion from equations (14) and (16). and d i a much easier task. u. 3. s vv Ýx i+1 Þ = 6a i Ýx i+1 ? x i Þ + 2b i i+1 2b i+1 = 6a i h + 2b i (17) (18) (15) (16) (14) (13) (12) These equations can be much simplified by substituting M i for s vv Ýx i Þ and expressing the i above equations in terms of M i and y i . 3.3 However. . b i . n ? 1. since s vv ÝxÞ has to be continuous across the interval. 2. using equation a i can be re-written as (20) (19) . Again. From equation (4). we arrive at c i = 3a i?1 h 2 + 2b i?1 h + c i?1 for i = 2. s vv ÝxÞ = 6a i Ýx ? x i Þ + 2b i . n ? 1.

ci = ?Ý 6 h 3 2 (21) (22) We now have our equations for determining the weights for our n ? 1 equations a i = M i+1 ? M i 6h Mi bi = 2 y i+1 ? y i ci = ? Ý M i+1 + 2M i Þh 6 h di = yi These systems can be handled more conveniently by putting them into matrix form as follows (23) .4 2b i+1 = 6a i h + 2b i 6a i h = 2b i+1 ? 2b i a i = 2b i+1 ? 2b i 6h M i+1 2Ý 2 Þ ? 2Ý M i Þ 2 ai = 6h a i = M i+1 ? M i 6h and c i can be re-written as d i+1 = a i h 3 + b i h 2 + c i h + d i c i h = ?a i h ? b i h ? d i + d i+1 3 2 c i = ?a i h ? b i h ? d i + d i+1 h ?a i h 3 ? b i h 2 + ?d i + d i+1 ci = h h d i ? d i+1 2 c i = Ý?a i h ? b i hÞ ? h M i+1 ? M i h 2 + M i hÞ ? y i ? y i+1 c i = ?Ý 2 6h h y i+1 ? y i M i+1 ? M i h + 3M i hÞ ci = ?Ý 6 6 h y i+1 ? y i M i+1 ? M i + 3M i Þh ?Ý ci = 6 h y i+1 ? y i M i+1 + 2M i Þh.

M1 = Mn = 0 This results in the spline extending as a line outside the endpoints. 3. and is therefore under-determined. (26) . n ? 1 which leads to the matrix equation M1 1 0 0 0 0 0 4 1 0 0 0 0 1 4 1 0 0 0 0 1 4 0 0 0 ` 0 ` 0 ` 0 ` 4 ` 1 ` 0 0 0 0 1 4 1 0 0 0 0 1 4 0 0 0 0 0 1 M2 M3 M4 _ M n?3 M n?2 M n?1 Mn = 62 h y 1 ? 2y 2 + y 3 y 2 ? 2y 3 + y 4 y 3 ? 2y 4 + y 5 _ y n?4 ? 2y n?3 + y n?2 y n?3 ? 2y n?2 + y n?1 y n?2 ? 2y n?1 + y n (25) _ _ _ _ b _ _ _ _ Note that this system has n ? 2 rows and n columns. The matrix for determining the M 1 ? M n values can be adapted accordingly. `. In order to generate a unique cubic spline. 2. Three types of Splines Natural splines This first spline type includes the stipulation that the second derivative be equal to zero at the endpoints. two other conditions must be imposed upon the system. This leads us to our next section.5 c i+1 = 3a i h 2 + 2b i h + c i y ? yi ? Ý M i+1 + 2M i Þh 3Ý M i+1 ? M i Þh 2 + 2Ý M i Þh + i+1 2 6 h 6h 3Ý M i+1 ? M i Þh 2 + 2Ý M i Þh ? Ý M i+1 + 2M i Þh + Ý M i+2 + 2M i+1 Þh 2 6 6 6h 3M i+1 ? 3M i + 6M i ? Ý M i+1 + 2M i Þ + Ý M i+2 + 2M i+1 ÞÞ hÝ 6 6 6 6 h ÝM i + 4M i+1 + M i+2 Þ 6 = = = = (24) y i+2 ? y i+1 M i+2 + 2M i+1 Þh ?Ý 6 h y i+1 ? y i y i+2 ? y i+1 ?Ý Þ+ h h y i ? 2y i+1 + y i+2 h y i ? 2y i+1 + y i+2 h y i ? 2y i+1 + y i+2 6Ý Þ h2 M i + 4M i+1 + M i+2 = for i = 1.

6 0 1 0 0 0 0 0 0 1 0 0 0 0 0 4 1 0 0 0 0 1 4 0 0 0 ` 0 ` 0 ` 0 ` 4 ` 1 ` 0 0 0 0 1 4 0 0 0 0 0 1 0 0 0 0 0 0 1 M2 M3 M4 _ M n?3 M n?2 M n?1 0 (27) For reasons of convenience. The spline is now unique. the first and last columns of this matrix can be eliminated. which are both 0. as they correspond to the M 1 and M n values. 4 1 0 0 0 0 1 4 1 0 0 0 0 1 4 0 0 0 ` 0 ` 0 ` 0 ` 4 ` 1 ` 0 0 0 0 1 4 1 0 0 0 0 1 4 M2 M3 M4 _ M n?3 M n?2 M n?1 = 62 h y 1 ? 2y 2 + y 3 y 2 ? 2y 3 + y 4 y 3 ? 2y 4 + y 5 _ y n?4 ? 2y n?3 + y n?2 y n?3 ? 2y n?2 + y n?1 y n?2 ? 2y n?1 + y n (28) This results in an n ? 2 by n ? 2 matrix. which will determine the remaining solutions for M 2 through M n?1 . = 62 h y 1 ? 2y 2 + y 3 y 2 ? 2y 3 + y 4 y 3 ? 2y 4 + y 5 _ y n?4 ? 2y n?3 + y n?2 y n?3 ? 2y n?2 + y n?1 y n?2 ? 2y n?1 + y n _ _ _ _ b _ _ _ _ _ _ _ b _ _ _ .

be equal to M 2 and M n?1 respectively. M 1 and M n .7 10 5 0 -5 -1 0 -1 5 -2 0 0. This type of cubic spline is useful for periodic and exponential data. M1 = M2 M n = M n?1 The result of this condition is the curve becomes a parabolic curve at the endpoint.5 1 1.5 4 4.5 3 3.5 Figure 1. The matrix equation for this type of spline is ` 0 ` 0 ` 0 ` 4 ` 1 ` 0 y 1 ? 2y 2 + y 3 y 2 ? 2y 3 + y 4 y 3 ? 2y 4 + y 5 = 62 h _ y n?4 ? 2y n?3 + y n?2 y n?3 ? 2y n?2 + y n?1 y n?2 ? 2y n?1 + y n (30) We can now determine the values for M 2 through M n?1 .5 2 2. (29) 5 1 0 0 0 0 1 4 1 0 0 0 0 1 4 0 0 0 0 0 0 1 4 1 0 0 0 0 1 5 M2 M3 M4 _ M n?3 M n?2 M n?1 _ _ _ b _ _ _ .5 5 5. Natural interpolating curve Parabolic Runout Spline The parabolic spline imposes the condition that the second derivative at the endpoints. with the values for M 1 and M n already determined.

rather than two separate functions. It assigns M 1 to be 2M 2 ? M 3 and M n to be 2M n?1 ? M n?2 . Parabolic Runout curve Note that the endpoint behavior is a bit more extreme than with the natural spline option. The matrix equation for this type is 6 1 0 0 0 0 0 4 1 0 0 0 0 1 4 0 0 0 ` 0 ` 0 ` 0 ` 4 ` 1 ` 0 0 0 0 1 4 0 0 0 0 0 1 6 M2 M3 M4 _ M n?3 M n?2 M n?1 = 62 h y 1 ? 2y 2 + y 3 y 2 ? 2y 3 + y 4 y 3 ? 2y 4 + y 5 _ y n?4 ? 2y n?3 + y n?2 y n?3 ? 2y n?2 + y n?1 y n?2 ? 2y n?1 + y n (31) _ _ _ b _ _ _ .8 10 0 -1 0 -2 0 -3 0 -4 0 -5 0 -6 0 0 1 2 3 4 5 6 Figure 2. Cubic Runout Spline This last type of spline has the most extreme endpoint behavior. This causes the curve to degrade to a single cubic curve over the last two intervals.

or more widely used than these other types of splines. Function Imitation Cubic splines would not be necessary were it simple to determine a well-behaved function to fit any data set. Cubic Runout curve Note the pronounced curvature at the endpoints. This is. they are not intrinsically superior to.9 20 0 -2 0 -4 0 -6 0 -8 0 -1 0 0 -1 2 0 -1 4 0 -1 6 0 0 1 2 3 4 5 6 Figure 3. Thus. Moreover. usually not the case. of course. however. the consistency and efficiency of the spline as a data correlation tool will be demonstrated. Curve Fitting with Splines The main application of cubic spline interpolation techniques is. such as the Periodic Spline and the Clamped Spline. Keep in mind that there are many other types of interpolating spline curves. . the cubic spline technique is used to generate a function to fit the data. the following figure was generated using the function y = sinÝxÞ. The three discussed in this work are simply the ones which we have chosen to examine. it can be shown that data generated by a particular function is interpolated by a spline which behaves more or less like the original function. This is testimony to the consistency of splines. To this end. curve fitting. For example.

4 0. 6 -0 . 8 -1 -4 -3 -2 -1 0 1 2 3 4 Figure 4.6 0. Spline through six points on a sine curve. This next figure was generated by connecting six data points along the above line with a cubic spline function. 6 -0 .6 0.2 0 -0 . we can see the degree to which the cubic curve imitates the original function y = sinÝxÞ.4 0. 4 -0 . 4 -0 .10 1 0.2 0 -0 . The graph of y = sinÝxÞ. . By superimposing Figure 5 on Figure 4. 1 0.8 0. 2 -0 . 8 -1 -3 -2 -1 0 1 2 3 Figure 5.8 0. 2 -0 .

11 1 0. . the cubic curve closely imitates the sine curve. This characteristic also works with more erratic functions.4 0. 4 -0 . Take for instance the function below. It has no extreme behavior between data points. Clearly. 2 -0 .8 0. and it effectively correlates the points.6 0.2 0 -0 . 6 -0 . Superimposition of a spline curve on a sine function. 8 -1 -3 -2 -1 0 1 2 3 Figure 6.

4 -0 . . the 100 random data points below. A cubic spline approximation of ßsinÝxÞ + cosÝxÞà 4 . for instance. While the fit is not perfect. Take.4 0. 2 -0 . 6 -0 .2 0 -0 . 8 -6 -4 -2 0 2 4 6 Figure 7.8 0. it does closely approximate the function without a great degree of divergence.6 0.12 1 0. 3 Data interpretation The splines other strength lies in its ability to correlate data which doesn’t follow any specific pattern without a single polynomial’s extreme behavior.

it is not much of a task to generate an approximating spline which generally correlates the data while being much more well behaved.2 0.4 0.3 0. .7 0.7 0.5 0. a much more reasonable range than an approximating polynomial.4 0. can interpolate all 100 points without the drastic behavior that the necessary 99th degree polynomial would exhibit.8 0.8 0. 1. 2 0 0.3 0. Also.2 0.13 1 0.1 0. A spline.8 0.2 0. while the spline is not exactly pleasing to the eye.5 0.9 1 Figure 8.2 0 -0 . Note that.9 0.2. its range stays from 0 to 1.7 0.6 0. Random data points Clearly there is no relation between these data points.2 1 0.3 0.6 0. however.9 1 Figure 9.5 0.1 0.4 0.4 0.8 0.1 0 0 0.6 0. Cubic curve through the random data.6 0.

interpolating data with a spline becomes an easy task.4 0.6 0. The m-file used is available for download at http://online.3 0. Approximating cubic curve The above figure demonstrates the general trends in the data (of which there are none) without necessarily connecting all data points.2 0. Cubic Splines and Matlab All of the graphics in this paper were generated using a cubic spline m-file and Matlab software. Yves. UMAP: Module 718.6 0. Once the algorithm for spline generation is produced. Splines in Single and Multivariable Calculus.us/instruct/darnold/laproj/fall98/Skymeg Conclusion Cubic spline interpolation is a powerful data analysis tool.1 0.5 0.8 0.3 0. Duane and Bruce Littlefield.4 0.14 1 0. Lexington.cc.7 0.9 1 Figure 10.2 0.5 0.8 0.ca.1 0 0 0. Mastering Matlab 5.redwoods. Its curvature is much less severe than that of fig 9. no matter how random the data may seem.9 0. Bibliography Hanselman. Splines correlate data efficiently and effectively.7 0. 1998 New Jersey: Prentice Hall Nievergelt. MA: COMAP . 1993.

15 Rorres. 1997 New Jersey: Prentice Hall . Chris and Howard Anton. Van Loan. Applications of Linear Algebra 3ed. Introduction to Scientific Computing. Charles F. 1984 New York: John Wiley and Sons.