Cubic Spline Interpolation Introduction Theory Process
Total Page:16
File Type:pdf, Size:1020Kb
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 s1ÝxÞ if x1 ² x < x2 s2ÝxÞ if x2 ² x < x3 SÝxÞ = (1 ) _ sn?1ÝxÞ if xn?1 ² x < xn where si is a third degree polynomial defined by 3 2 siÝxÞ = aiÝx ? xiÞ + biÝx ? xiÞ + ciÝx ? xiÞ + di (2 ) for i = 1,2,...,n ? 1. The first and second derivatives of these n ? 1 equations are fundamental to this process, and they are 1 2 v 2 siÝxÞ = 3aiÝx ? xiÞ + 2biÝx ? xiÞ + ci (3 ) vv si ÝxÞ = 6aiÝx ? xiÞ + 2bi (4 ) for i = 1,2,...,n ? 1. The Four Properties of Cubic Splines Our spline will need to conform to the following stipulations. 1. The piecewise function SÝxÞ will interpolate all data points. 2. SÝxÞ will be continuous on the interval ßx1,xnà v 3. S ÝxÞ will be continuous on the interval ßx1,xnà vv 4. S ÝxÞ will be continuous on the interval ßx1,xnà Since the piecewiece function SÝxÞ will interpolate all of the data points, we can conclude that SÝxiÞ = yi (5 ) for i = 1,2,...,n ? 1. Since xi 5 ßxi,xi+1à, SÝxiÞ = siÝxiÞ and we can use equation (2) to produce yi = siÝxiÞ (6 ) 3 2 yi = aiÝxi ? xiÞ + biÝxi ? xiÞ + ciÝxi ? xiÞ + di yi = di for each i = 1,2,...,n ? 1. Since the curve SÝxÞ must be continuous across its entire interval, it can be concluded that each sub-function must join at the data points, so siÝxiÞ = si?1ÝxiÞ (7 ) for i = 2,3,...,n. From equation (2), siÝxiÞ = di and (8 ) 3 2 si?1ÝxiÞ = ai?1Ýxi ? xi?1Þ + bi?1Ýxi ? xi?1Þ + ci?1Ýxi ? xi?1Þ + di?1 so 3 2 di = ai?1Ýxi ? xi?1Þ + bi?1Ýxi ? xi?1Þ + ci?1Ýxi ? xi?1Þ + di?1 (9 ) for i = 2,3,...,n ? 1. Letting h = xi ? xi?1 in equation (8), we have 3 2 di = ai?1h + bi?1h + ci?1h + di?1 (10 ) for i = 2,3,...,n ? 1. Also, to make the curve smooth across the interval, the derivatives must be equal at the data points; that is, v v siÝxiÞ = si?1ÝxiÞ (11 ) 3 However, by equation (3), v siÝxiÞ = ci and v 2 si?1ÝxiÞ = 3ai?1Ýxi ? xi?1Þ + 2bi?1Ýxi ? xi?1Þ + ci?1 so 2 ci = 3ai?1Ýxi ? xi?1Þ + 2bi?1Ýxi ? xi?1Þ + ci?1. (12 ) Again, letting h = xi ? xi?1, we arrive at 2 ci = 3ai?1h + 2bi?1h + ci?1 (13 ) for i = 2,3,u,n ? 1. vv From equation (4), si ÝxÞ = 6aiÝx ? xiÞ + 2bi, so vv si ÝxÞ = 6aiÝx ? xiÞ + 2bi (14 ) vv si ÝxiÞ = 6aiÝxi ? xiÞ + 2bi vv si ÝxiÞ = 2bi for i = 2,3,...,n ? 2. vv vv vv Lastly, since si ÝxÞ has to be continuous across the interval, si ÝxiÞ = si+1ÝxiÞ for i = 1,2,3,`,n ? 1. This and equation (14) lead us to the equation vv si Ýxi+1Þ = 6aiÝxi+1 ? xiÞ + 2bi (15 ) vv si+1Ýxi+1Þ = 6aiÝxi+1 ? xiÞ + 2bi (16 ) and, letting h = xi+1 ? xi and using the conclusion from equations (14) and (16), vv si+1Ýxi+1Þ = 6aiÝxi+1 ? xiÞ + 2bi (17 ) 2bi+1 = 6aih + 2bi (18 ) vv These equations can be much simplified by substituting Mi for si ÝxiÞ and expressing the above equations in terms of Mi and yi. This makes the determination of the weights ai, bi, ci, and di a much easier task. Each bi can be represented by vv si ÝxiÞ = 2bi (19 ) Mi = 2bi Mi bi = 2 and di has already been determined to be di = yi. (20 ) Similarly, using equation ai can be re-written as 4 2bi+1 = 6aih + 2bi (21 ) 6aih = 2bi+1 ? 2bi 2bi+1 ? 2bi ai = 6h Mi+1 Mi 2Ý 2 Þ ? 2Ý 2 Þ ai = 6h Mi+1 ? Mi ai = 6h and ci can be re-written as 3 2 di+1 = aih + bih + cih + di (22 ) 3 2 cih = ?aih ? bih ? di + di+1 3 2 ?aih ? bih ? di + di+1 ci = h 3 2 ?aih ? bih ?di + di+1 ci = + h h 2 di ? di+1 ci = Ý?aih ? bihÞ ? h Mi+1 ? Mi 2 Mi yi ? yi+1 ci = ?Ý h + hÞ ? 6h 2 h yi+1 ? yi Mi+1 ? Mi 3Mi ci = ? Ý h + hÞ h 6 6 yi+1 ? yi Mi+1 ? Mi + 3Mi ci = ? Ý Þh h 6 yi+1 ? yi Mi+1 + 2Mi ci = ? Ý Þh. h 6 We now have our equations for determining the weights for our n ? 1 equations Mi+1 ? Mi ai = (23 ) 6h Mi bi = 2 yi+1 ? yi Mi+1 + 2Mi ci = ? Ý Þh h 6 di = yi These systems can be handled more conveniently by putting them into matrix form as follows 5 2 ci+1 = 3aih + 2bih + ci (24 ) M M M yi 1 ? yi M 2M yi 2 ? yi 1 M 2M 3Ý i+1 ? i Þh2 + 2Ý i Þh + + ? Ý i+1 + i Þh = + + ? Ý i+2 + i+1 Þh 6h 2 h 6 h 6 M M M M 2M M 2M yi 1 ? yi yi 2 ? yi 1 3Ý i+1 ? i Þh2 + 2Ý i Þh ? Ý i+1 + i Þh + Ý i+2 + i+1 Þh = ?Ý + Þ + + + 6h 2 6 6 h h 3M 3M 6M M 2M M 2M yi ? 2yi 1 + yi 2 hÝ i+1 ? i + i ? Ý i+1 + i Þ + Ý i+2 + i+1 ÞÞ = + + 6 6 6 6 h h yi ? 2yi+1 + yi+2 ÝMi + 4Mi 1 + Mi 2Þ = 6 + + h yi ? 2yi+1 + yi+2 Mi + 4Mi 1 + Mi 2 = 6Ý Þ + + h2 for i = 1,2,3,`,n ? 1 which leads to the matrix equation M1 1 4 1 0 ` 0 0 0 0 M2 y1 ? 2y2 + y3 0 1 4 1 ` 0 0 0 0 M3 y2 ? 2y3 + y4 0 0 1 4 ` 0 0 0 0 M4 y3 ? 2y4 + y5 6 _ _ _ _ b _ _ _ _ _ = _ h2 0 0 0 0 ` 4 1 0 0 Mn?3 yn?4 ? 2yn?3 + yn?2 0 0 0 0 ` 1 4 1 0 Mn?2 yn?3 ? 2yn?2 + yn?1 0 0 0 0 ` 0 1 4 1 Mn?1 yn?2 ? 2yn?1 + yn Mn (25 ) Note that this system has n ? 2 rows and n columns, and is therefore under-determined. In order to generate a unique cubic spline, two other conditions must be imposed upon the system. This leads us to our next section. Three types of Splines Natural splines This first spline type includes the stipulation that the second derivative be equal to zero at the endpoints. M1 = Mn = 0 (26 ) This results in the spline extending as a line outside the endpoints. The matrix for determining the M1 ? Mn values can be adapted accordingly. 6 0 1 0 0 0 ` 0 0 0 0 M2 y1 ? 2y2 + y3 0 1 4 1 ` 0 0 0 0 M3 y2 ? 2y3 + y4 0 0 1 4 ` 0 0 0 0 M4 y3 ? 2y4 + y5 6 _ _ _ _ b _ _ _ _ _ = _ h2 0 0 0 0 ` 4 1 0 0 Mn?3 yn?4 ? 2yn?3 + yn?2 0 0 0 0 ` 1 4 1 0 Mn?2 yn?3 ? 2yn?2 + yn?1 0 0 0 0 ` 0 0 0 1 Mn?1 yn?2 ? 2yn?1 + yn 0 (27 ) For reasons of convenience, the first and last columns of this matrix can be eliminated, as they correspond to the M1 and Mn values, which are both 0. 4 1 0 ` 0 0 0 M2 y1 ? 2y2 + y3 1 4 1 ` 0 0 0 M3 y2 ? 2y3 + y4 0 1 4 ` 0 0 0 M4 y3 ? 2y4 + y5 6 _ _ _ b _ _ _ _ = _ h2 0 0 0 ` 4 1 0 Mn?3 yn?4 ? 2yn?3 + yn?2 0 0 0 ` 1 4 1 Mn?2 yn?3 ? 2yn?2 + yn?1 0 0 0 ` 0 1 4 Mn?1 yn?2 ? 2yn?1 + yn (28 ) This results in an n ? 2 by n ? 2 matrix, which will determine the remaining solutions for M2 through Mn?1.