Spline Background • Problem: high degree interpolating often have extra oscillations. 1 Example: “Runge” f(x) = 1+4x2 , x ∈ [−1, 1].

1/(1+4x2) and P (x) and P (x) 8 16 1

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 • Piecewise Polynomials provide alternative to high degree polynomials: approximation interval [a, b] is sub- divided into pieces [x1, x2], [x2, x3],..., [xn−1, xn], with a = x1 < x2 < ··· < xn = b, and a low degree polyno- mial is used to approximate f(x) on each subinterval. Example: piecewise linear approximation S(x)

f(xj+1) − f(xj) S(x) = f(xj)+(x−xj) , if x ∈ [xj, xj+1] xj+1 − xj • Splines are piecewise approximations, con- nected at xj’s with various continuity conditions. CUBIC

Cubic Interpolating Splines for a = x1 < ··· < xn = b with given data (x1, y1), (x2, y2),..., (xn, yn). • Properties of Cubic Interpolating Spline S(x),

a) S(x) is composed of cubic polynomial pieces Sj(x)

S(x) = Sj(x) if x ∈ [xj, xj+1], j = 1, 2, . . . , n − 1.

b) S(xj) = yj, j = 1, . . . , n. (interpolation) c) Sj−1(xj) = Sj(xj), j = 2, . . . , n − 1 (S ∈ C[a, b]). 0 0 1 d) Sj−1(xj) = Sj(xj), j = 2, . . . , n − 1 (S ∈ C [a, b]). 00 00 2 e) Sj−1(xj) = Sj (xj), j = 2, . . . , n − 1 (S ∈ C [a, b]). f) two end conditions: examples 00 00 i) S (x1) = S (xn) = 0 (natural or free spline); 0 0 0 0 ii) S (x1) = f (x1),S (xn) = f (xn) (complete or clamped spline); 000 000 iii) S1 = Sn−1 = 0 (parabolically terminated); 000 000 000 000 iv) S1 (x2) = S2 (x2),Sn−2(xn−1) = Sn−1(xn−1) (not-a-knot). Note: if 2 3 Sj(x) = aj +bj(x−xj)+cj(x−xj) +dj(x−xj) , condition a) provides 4(n − 1) free parameters; b)-f) give n + 3(n − 2) + 2 = 4(n − 1) constraints.

2 CUBIC SPLINE INTERPOLATION • Example: n = 3, natural, data (1,2), (2,3), (3,5).

3 1 3 S1(x) = 2 + 4(x − 1) + 4(x − 1) , 3 3 2 1 3 S2(x) = 3 + 2(x − 2) + 4(x − 2) − 4(x − 2) ,

3 CUBIC SPLINE INTERPOLATION Cubic Interpolating Spline Construction

• Spline Linear System: let hj = xj+1 − xj; start with 2 3 Sj(x) = aj + bj(x − xj) + cj(x − xj) + dj(x − xj) 2 3 = yj + bj(x − xj) + cj(x − xj) + dj(x − xj) .

c) Sj+1(xj+1) = Sj(xj+1), implies

2 3 ∆yj 2 yj+1 = yj +bjhj +cjhj +djhj; = bj +cjhj +djhj, hj

where ∆yj = yj+1 − yj. 00 Notice Sj (x) = 2cj + 6dj(x − xj), so 00 00 e) Sj+1(xj+1) = Sj (xj+1), implies

2cj+1 = 2cj + 6djhj; djhj = (cj+1 − cj)/3, 00 with extra unknown cn = Sn−1(xn)/2 added. Then

∆yj (cj+1 − cj)hj (cj+1 + 2cj)hj = bj +cjhj + = bj + ; hj 3 3

3∆yj+1 3∆yj − = 3∆bj+(cj+2+2cj+1)hj+1−(cj+1+2cj)hj. hj+1 hj 0 2 Also Sj(x) = bj + 2cj(x − xj) + 3dj(x − xj) , so 0 0 2 d) Sj+1(xj+1) = Sj(xj+1); bj+1 = bj + 2cjhj + 3djhj;

∆bj = 2cjhj + (cj+1 − cj)hj = (cj+1 + cj)hj

3∆yj+1 3∆yj − = cjhj + 2cj+1(hj + hj+1) + cj+2hj+1 hj+1 hj

4 CUBIC SPLINE INTERPOLATION 00 00 • Natural Splines: S (x1) = S (xn) = 0, so c1 = cn = 0 Linear system equations are a “tridiagonal” system

c1 = 0 3∆y2 3∆y1 c1h1 + 2c2(h1+h2) + c3h2 = − h2 h1 3∆y3 3∆y2 c2h2 + 2c3(h2+h3) + c4h3 = − h3 h2 . . 3∆yn−2 3∆y3−2 cn−3hn−3 + 2cn−2(hn−3+hn−2) + cn−1hn−2 = − hn−2 hn−3 3∆yn−1 3∆yn−2 cn−2hn−2 + 2cn−1(hn−2+hn−1) + cnhn−1 = − hn−1 hn−2 cn = 0.

which can be solved uniquely for cj’s with O(n) work; 2 dj = (cj+1 − cj)/(3hj), bj = ∆yj/hj − cjhj − djhj can be used to find remaining coefficients for Sj(x)’s. Note: if all hj = h, a simpler tridiagonal system.

5 CUBIC SPLINE INTERPOLATION • Example: n = 3, natural, with data (1,2), (2,3), (3,5), so h1 = h2 = 1, ∆y1 = 1, ∆y2 = 2. Only one equation 2c2(1 + 1) = 3(2 − 1), so c2 = 3/4, d1 = 1/4, d2 = −1/4; b1 = 1 − 0 − 1/4 = 3/4, b2 = 2 − 3/4 + 1/4 = 3/2. 3 1 S (x) = 2 + (x − 1) + (x − 1)3, 1 4 4 3 3 1 S (x) = 3 + (x − 2) + (x − 2)2 − (x − 2)3. 2 2 4 4 1 • Example: “Runge” function f(x) = 1+4x2 , x ∈ [−1, 1].

1/(1+4x2), S(x) and P (x) 4 1

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

6 CUBIC SPLINE INTERPOLATION 0 0 00 0 • Clamped Splines: let S (x1) = y1,S (xn) = yn, 0 0 2 so y1 = b1, yn = bn−1 + 2cn−1hn−1 + 3dn−1hn−1 ∆yj (cj+1+2cj)hj Using = bj + , 3hjdj = (cj+1 − cj), hj 3 “1st” and “nth” equations become 3∆y1 0 2c1h1 + c2h1 = − 3y , and h1 1 0 3∆yn−1 cn−1hn−1 + 2cnhn−1 = 3y − . n hn−1 Linear system equations are a “tridiagonal” system

3∆y1 0 2c1h1 + c2h1 = − 3y1 h1 3∆y2 3∆y1 c1h1 + 2c2(h1+h2) + c3h2 = − h2 h1 . . 3∆yn−1 3∆yn−2 cn−2hn−2 + 2cn−1(hn−2+hn−1) + cnhn−1 = − hn−1 hn−2 0 3∆yn−1 cn−1hn−1 + 2cnhn−1 = 3yn − . hn−1

which can be solved (uniquely) for cj’s with O(n) work; 2 dj = (cj+1 − cj)/(3hj), bj = ∆yj/hj − cjhj − djhj can be used to find remaining coefficients for Sj(x)’s.

7 CUBIC SPLINE INTERPOLATION • Example: n = 3, clamped, with data (1,2), (2,3), (3,5), 0 0 and y1 = 1, y3 = 2. Three equations: 2c1 + c2 = 3(2 − 1) − 3 = 0, c1 + 4c2 + c3 = 3, c2 + 2c3 = 6 − 3(2) = 0, so c1 = c3 = −1/2, c2 = 1; d1 = 1/2, d2 = −1/2; b1 = 1, b2 = 2−1+1/2 = 3/2. 1 2 1 3 S1(x) = 2 + (x − 1) − 2(x − 1) + 2(x − 1) , 3 2 1 3 S2(x) = 3 + 2(x − 2) + (x − 2) − 2(x − 2) , 000 000 • Parabolically Terminated Splines: S1 = Sn−1 = 0, so d1 = dn−1 = 0, c1 = c2, cn−1 = cn. Linear system equations are a “tridiagonal” system

c1 − c2 = 0 3∆y2 3∆y1 c1h1 + 2c2(h1+h2) + c3h2 = − h2 h1 . . 3∆yn−1 3∆yn−2 cn−2hn−2 + 2cn−1(hn−2+hn−1) + cnhn−1 = − hn−1 hn−2 cn−1 − cn = 0.

which can be solved (uniquely) for cj’s with O(n) work; 2 dj = (cj+1 − cj)/(3hj), bj = ∆yj/hj − cjhj − djhj can be used to find remaining coefficients for Sj(x)’s.

8 CUBIC SPLINE INTERPOLATION • Not-a-Knot Splines: 000 000 000 000 S1 (x2) = S2 (x2),Sn−2(xn−1) = Sn−1(xn−1), so d1 = d2, dn−2 = dn−1, and S1 = S2,Sn−2 = Sn−1. Then (c2 − c1)/h1 = (c3 − c2)/h2, (cn−1 − cn−2)/hn−2 = (cn − cn−1)/hn−1, so “1st” and “nth” equations become c1h2 − c2(h1 + h2) + c3h1 = 0 cn−2hn−1 − cn−1(hn−2 + hn−1) + cnhn−2 = 0. Linear system equations are a “tridiagonal” system

c1h2 − c2(h1 + h2) + c3h1 = 0 3∆y2 3∆y1 c1h1 + 2c2(h1+h2) + c3h2 = − h2 h1 . . 3∆yn−1 3∆yn−2 cn−2hn−2 + 2cn−1(hn−2+hn−1) + cnhn−1 = − hn−1 hn−2 cn−2hn−1 − cn−1(hn−2 + hn−1) + cnhn−2 = 0.

which can be solved (uniquely) for cj’s with O(n) work; 2 dj = (cj+1 − cj)/(3hj), bj = ∆yj/hj − cjhj − djhj can be used to find remaining coefficients for Sj(x)’s.

9 CUBIC SPLINE INTERPOLATION Efficient Spline Evaluation

• Setup: solve linear system for cj’s in O(n) time

• For each evaluation point x, find interval x ∈ [xj, xj+1] in O(log(n)) time and evaluate Sj(x) in O(1) time. Alternate formula for Sj(x) (without bj’s and dj’s):

cj 3 cj+1 3 Sj(x) = (xj+1 − x) + (x − xj) 3hj 3hj+1 yj cjhj yj+1 cj+1hj+1 + ( − )(xj+1 − x) + ( − )(x − xj). hj 3 hj+1 3 • Compare with , where setup time is O(n2) and evauation time is O(n). Error Theorem : 4 (4) if f ∈ C [a, b], with maxx∈[a,b] |f (x)| = M, and S(x) is the unique clamped spline for f(x) with nodes a = x1 < x2 ··· < xn = b, then

5M 4 |f(x) − S(x)| ≤ max hj. 384 1≤j

10