Piecewise Polynomial Interpolation 1
Piecewise linear interpolation Suppose we have data point (x k,y k ), k =0, 1,...N. A piecewise linear polynomial that interpolates these points is given by p(x) =p k (x), x 2 [x k,x k+1 ] where the polynomials p k (x) can be written as p k (x) =a 1 (x x k )+a 0, k =1, 2,...N 1 At this point, we could easily compute the coe cients a =[a 1,a 0 ], but let s go through a more general procedure that we can use for higher order piecewise polynomials. 2
Piecewise linear interpolation We can write this as a function of a local variable t which measures the distance along the interval. t = x x k x k+1 x k x = h k t + x k where h k = x k+1 x k. Then, for 0 apple t apple 1, we have p k (x) =ep k (t) =ea 1 t + ea 0 t =1 (x k+1,y k+1 ) t =0 Constraints to impose ep k (0) = y k (x k,y k ) 0 <t<1 ep k (1) = y k+1 3
Piecewise linear interpolation This leads to a linear system apple 0 1 1 1 apple ea1 ea 0 = apple yk y k+1 which we write as Aea = p. To solve for the coe we use the matrix co-factor C to get A 1 A 1 = CT det(a) = 1 det(a) apple 1 1 1 0 cients, We have det(a) = 1, and so ea = C T p 4
Piecewise linear interpolation To see what ep k (t) looks like, we let q(t) =[t, 1] and write ep k (t) = ea T q(t) = p T Cq(t) apple T = yk y k+1 1 1 1 0 apple t 1 The advantage of this form is that we can clearly see how the polynomial ep k (t) depends on our data y k and y k+1 : ep k (t) =(1 t)y k + ty k+1 5
Piecewise linear interpolation To build a spline in Matlab, we need to have the coefficients a 0 and a 1 of our original polynomial. p k (x) =a 1 (x x k )+a 0 We can recover these coe cients by substituting t = (x x k )/h k into ep k (t) =ea 1 t + ea 0 to get p k (x) =ea 1 x h k xk + ea 0 = a 1 (x x k )+a 0 where a 1 = ea 1 /h k and ea 0 = a 0. 6
Piecewise linear interpolation We can also write a =[a 1,a 0 ]using a = p T apple 1 1 CS = y k y k+1 1 0 " yk+1 y # k = h k y k apple 1/hk 0 0 1 These are exactly the coefficients that Matlab needs to build a spline using mkpp. function pp_linear = build_linear_spline(xdata,ydata)! N = length(xdata)-1;! h = diff(xdata); %(x1-x0, x2-x1, x3-x2,...)! C = [1-1; -1 0];! for k = 1:N,! p = [ydata(k); ydata(k+1)];! S = diag([1./h(k) 1]);! coeffs(k,:) = -p'*c*s;! end! pp_linear = mkpp(xdata,coeffs);! end 7
Piecewise linear interpolation (x 4,y 4 ) (x 0,y 0 ) (x 2,y 2 ) (x 5,y 5 ) 8
Piecewise linear interpolation Piecewise linear interpolation has these properties Continuous at nodes (x k,y k ). Derivatives are not continuous (the function is not smooth!) Can we come design a method that matches derivatives at the nodes as well? 9
Hermite interpolation We can impose smoothness at the nodes to get a nicer looking curve. Suppose we match the function values and derivatives. p(x k+1 )=y k+1 p 0 (x k+1 )=d k+1 This approach assumes that we have derivatives dk at each of the nodes xk. p(x k )=y k Four conditions allow us to find the four coefficients of a cubic polynomial p 0 (x k )=d k p k (x) =a 3 (x x k ) 3 + a 2 (x x k ) 2 + a 1 (x x k )+a 0 10
Hermite interpolation As before, define t = x x k x k+1 x k,! x = h k t + x k where h k = x k+1 x k. Then, for 0 apple t apple 1, we have p k (x) ep k (t) =ea 3 t 3 + ea 2 t 2 + ea 1 t + ea 0 and our four conditions become ep k (0) = y k ep k (1) = y k+1 ep 0 k (0) = d kh k ep 0 k (1) = d k+1h k 11
Hermite interpolation This leads to the following linear system for the coe cients ea n : We write the above system as Aea = p. To invert A, weusethe co-factor matrix C to get A 1 = CT det(a) See Strang s Introduction to Linear Algebra for more information on the co-factor formula for the matrix inverse. 12
Hermite interpolation We can then write ep k (t) as ep k (t) =ea T q(t) = p T Cq(t) where q(t) =[t 3,t 2,t,1] T and we have used the fact that det(a) = 1. Computing C, we get 2 3 2 2 3 0 1 t 3 ep k (t) = T y k y k+1 d k h k d k+1 h k 6 2 3 0 0 7 6 t 2 4 1 2 1 0 5 4 t 1 1 0 0 1 3 7 5 which leads to a formula that clearly shows how the polynomial depends on our data y k, y k+1, d k and d k+1 : ep k (t) = 1 t 2 (3 2t) y k + t 2 (3 2t) y k+1 + t(t 1) 2 h k d k + t 2 (t 1) h k d k+1 13
Piecewise Cubic Hermite Interpolating Polynomial (PCHIP) ep k (t) = 1 t 2 (3 2t) y k + t 2 (3 2t) y k+1 + t(t 1) 2 h k d k + t 2 (t 1) h k d k+1 x k )/h k, we can recover our original poly- If we substitute t =(x nomial in x : p k (x) =a 3 (x x k ) 3 + a 2 (x x k ) 2 + a 1 (x x k )+a 0 where a n = ea n /h n k. We can write these coe a = p T CS cients as These are exactly the coefficients that Matlab needs to build a spline using mkpp. where S = diag(h 3 k,h 2 k,h 1 k, 1), p =[y k,y k+1,d k h k,d k+1 h k ], and C is the co-factor matrix of our original coe cient matrix A. 14
The derivatives? Where do we get the derivatives d k from? If we know f(x), we can set d k = f 0 (x k ). We can approximate derivatives using data (x k,y k ). d k = G ( k 1, k), k = y k+1 y k h k where, for example, we could take G(a, b) = a+b 2, G(a, b) = a, G(a, b) = b or some other reasonable function. Matlab s PCHIP chooses derivatives to avoid introducing new extrema in the spline. We can impose additional smoothness conditions and solve implicitly for the derivatives. 15
Derivatives? Given data points (x 0,y 0 ),...,(x N,y N ), impose continuity in the second derivative at the interior nodes, p 00 k(x k+1 )=p 00 k+1(x k+1 ), k =1, 2,...N 1. We can formulate this as a linear expression F(d) =Bd + c =0 where B 2R (N+1) (N+1), c 2R (N+1) 1 and F(d) =0expresses the equality of the second derivative at the internal nodes, plus two additional constraints. Solving for d, we have d = B 1 c, d 2R N+1 16
End conditions? 17
Cubic spline 18
Periodic spline 19