Spline Functions – An Elegant View of Interpolation Bruce Cohen bic@cgl.ucsf.edu http://www.cgl.ucsf.edu/home/bic David Sklar dsklar46@yahoo.com Start with xv intensity controls Some Goals To present a concrete introduction to a widely used class of methods for creating continuous functions that interpolate discrete data To apply straightforward geometric and algebraic operations with functions and their graphs that may revolutionize your views of interpolation and approximation To present a concrete example of a general method for extending a 1-dimensional scheme into higher dimensions To stimulate some curricular or student project ideas To see how elementary themes can lead to some beautiful pictures and a lingering vision Game Plan Connecting the dots – continuous piecewise linear interpolation using the data to compute a linear equation for each subinterval using a linear combination of “Spline basis functions” Connecting the dashes – smooth, piecewise cubic, interpolation using slope and function data to compute a cubic equation for each subinterval using a linear combination of “Cubic spline basis functions” As time allows: Interpolation using 2-D splines Bilinear spline basis functions Bicubic spline basis functions Overview – “elementary mathematics from an advanced standpoint” linear algebra, finite dimensional function spaces, inner products, small support, almost orthogonal bases, tensor products, finite element methods, wavelets, … Connecting the Dots Write a formula for a piecewise linear function that interpolates five given data points 3,2.5 2, 2 5,2 4,1.5 1,1 0 1 f x 2 x 1 x 1 2 11 x 2 12 x 12 3 4 if 1 x 2 if 2 x 3 if 3 x 4 if 4 x 5 5 6 Write a formula for a piecewise linear function that interpolates n given data points x2 , y2 x3 , y3 xn , yn xn1, yn1 x1, y1 x1 f x x2 x3 y1 x2 y2 x1 y2 y1 x x x x2 x1 2 1 y2 x3 y3 x2 y3 y 2 x x3 x2 x3 x2 yn 1 xn yn xn 1 yn yn 1 x x x xn xn 1 n n 1 xn 1 xn if x1 x x2 if x2 x x3 if xn 1 x xn Interpolation using a Linear Spline Basis The “linear spline function” approach involves carefully choosing a set of “basis functions” 1 , 2 , , n such that the interpolating function f can be written as a simple linear combination: f x y11 x y2 2 x yn n x n y x i 1 i i , for all x1 x xn xi , yi xi1, yi1 xi2 , yi2 xi1, yi1 xi2 , yi2 i xi 2 xi 1 xi ,1 xi xi 1 xi 2 A closer look at a linear combination of basis functions f x y11 x y2 2 x yi 1i 1 yi i yn n x yi 1i 1 i xi 1 i 1 i xi2 , yi2 yi i xi 2 y x xi , yi xi1, yi1 xi2 , yi2 n xi1, yi1 xi ,1 xi xi 1 xi 2 i The linear spline basis functions can be constructed as sums of translations and horizontal scalings of two “elementary basis functions” 0 1 if x 0,1 1 x 1 x 0 if x 0,1 0 x 0 2 x 1 if x 0,1 if x 0,1 x xi x xi 1 i x 2 1 x x x x i 1 i i 1 i xi 1 xi xi 1 A summary description of the linear spline basis 1. Elementary basis functions – basically constructed on the unit interval 1 x 0 1 x if x 0,1 if x 0,1 x 0 2 x if x 0,1 if x 0,1 1 0 2. A set of nodes -- x1 x2 0 1 xn 3. Spline basis functions – sums of (usually) two translated and scaled elementary basis functions interior: i 2, ,n 1 x xi 1 x xi i x 2 1 xi xi 1 xi 1 xi xi 1 endpoints: i 1 and n x x1 1 x 1 x2 x1 xi xi 1 x xn1 n x 2 xn xn1 x1 x2 xn 1 xn Points from sin(x) Points Points from sin(x) Linear Interpolation Points from sin(x) Linear Interpolation & “retail” 1 e Points from g x 2 x2 2 Points from g(x) Linear Interpolation Points from g(x) Linear Interpolation & “retail” Find a smooth piecewise cubic function that has specified function values and slopes at n given data points x3 , y3 y 3 x2 , y2 xn , yn y n y 2 xn1, yn1 yn 1 x1, y1 y1 x1 x2 x3 xn 1 xn where yi denotes the slope or first derivative at the data point Why cubic ? xi , yi Find a cubic function that has specified function values and slopes at 2 given data points x2 , y2 y 2 x1, y1 y1 x1 3 2 Let p x ax bx cx d Using the data we have x2 so p x 3ax 2 2bx c p x1 ax13 bx12 cx1 d y1 p x2 ax23 bx22 cx2 d y2 p x1 3ax12 2bx1 c y1 p x2 3ax22 2bx2 c y2 We have 4 equations in the four unknown coefficients. So we can solve for a, b, c, and d. Geometer’s sketch pad demonstration The cubic spline basis functions can be constructed as sums of translations and scalings of four “elementary basis functions” 0 1 2 0 0, 2 1 1 2 0 0, 2 1 0 0 1 2 0 0, 2 1 0 2 0 0, 2 1 1 0 1 1 0 1 , 1 1 0 1 0 0 , 1 1 0 0 1 1 0 0, 1 1 0 1 0 1 , 1 1 0 Construction of the cubic elementary basis functions A somewhat more interesting way is to note that the most general cubic with a zero value and a zero derivative at 0 is 2 x x 2 ax b so 2 x 3ax 2 2bx 0 Now 1 and 2 0 0, 2 1 1 2 0 0, 2 1 0 2 1 1 2 1 0 3a 2b 0 a 2, b 3 Hence and There are several reasonable ways to find the cubic function with the desired properties. A straightforward way is to write a general cubic, compute its derivative, substitute the desired conditions and solve the resulting system of four linear equations for the coefficients. a b 1 2 x x 2 3 2 x We can see by symmetry that 1 x 2 1 x 1 x 3 2 1 x 2 0 1 1 0 1, 1 1 0 1 0 0, 1 1 0 1 x 2 x 1 2 x 1 2 x 1 2 Construction of the cubic elementary basis functions 0 1 2 0 0, 2 1 0 2 0 0, 2 1 1 In this case we note that the most general cubic with a zero value and a zero derivative at 0 and a zero value at 1 is 2 x ax x 1 so 2 x a 3x 2 2 x 2 Now 2 1 1 a 1 hence 2 x x 2 x 1 0 1 1 0 0, 1 1 0 1 0 1, 1 1 0 Again using symmetry we see that 1 x 2 1 x 1 x 1 x 1 2 1 x x 2 x x 1 2 The Cubic Spline Elementary Basis Functions 0 2 x 3 2 x 2 x 0 0 2 x x 1 2 x 0 1 if x 0,1 if x 0,1 1 if x 0,1 if x 0,1 1 0 if x 0,1 if x 0,1 2 x 1 x 12 1 x 2 1 x 0 0 2 x x 1 1 x 2 1 x 0 1 if x 0,1 if x 0,1 Construction of the cubic spline basis functions as translations and scalings of four “elementary basis functions” 0 1 0 1 2 x 1 x x xi 1 x xi i x 2 1 xi xi 1 xi 1 xi used to match function values xi 1 xi xi 1 Scaling the derivative basis functions 1 x 0 1 2x 1 x 1 1 2 2 0 or x 1 1 1 2 A horizontal scaling by a factor of ½ changes the slopes by a factor of 2. 1 2 A vertical scaling by a factor of ½ returns the slopes to their corresponding values. 0 1 2 x x In general the function h g has the same slope at as g x has at x h h Construction of the cubic spline “derivative” basis functions from the “derivative” elementary basis functions 0 0 1 2 x 1 1 x xi 1 xi xi 1 x xi 1 x xi i x xi xi 1 2 x x i 1 i 1 x x x x i 1 i i 1 i used to match slope values A summary description of the Cubic Spline Basis 1. Elementary basis functions – basically constructed on the unit interval four for the cubics 0 2 x 3 2 x 0 2 x 0 2 x x 1 2 x 0 if x 0,1 if x 0,1 2 x 1 x 1 1 x 2 1 x 0 0 1 if x 0,1 if x 0,1 2. A set of nodes -- x1 x2 1 0 1 2 x x 1 1 x 2 1 x 0 xn if x 0,1 if x 0,1 2 1 if x 0,1 if x 0,1 3. Spline basis functions – sums of (usually) two translated and scaled elementary basis functions interior: i 2, x xi 1 x xi i x 2 1 xi xi 1 xi 1 xi ,n 1 x xi 1 x xi i x xi xi 1 2 xi 1 xi 1 xi xi 1 xi 1 xi endpoints: i 1 and n x x1 1 x 1 x2 x1 x1 x x1 1 x x2 x1 1 x2 x1 x1 x2 xi 1 xi xi 1 xi 1 xi xi 1 x xn 1 n x 2 xn xn 1 x2 x xn1 n x xn xn 1 2 xn xn1 xn 1 xn 1 xn xn A closer look at a linear combination of cubic basis functions f x y11 x y11 x y22 x y22 x n i 1 ynn x ynn x yi i x yi i x xi , yi xi1, yi1 yi xi1, yi1 yi1 yi1 yi i x yi i x xi2 , yi2 yi 2 i yi2 xi 2 i yi i xi2 , yi2 xi 1 xi yii xi 1 xi 2 Points from sin(x) Linear Interpolation & slopes Points from sin(x) Cubic Interpolation Points from sin(x) Interpolations and “Retail” 1 e Points from g x 2 x2 2 Points from g(x) Linear Interpolation Points from g(x) Linear Interpolation & slopes Points from g(x) Cubic Interpolation Points from g(x) Interpolations and “Retail” Geometer’s sketch pad demonstration Some nice properties of cubic splines Localization – changing the data at a point only changes the interpolant on the two adjacent subintervals Simple construction - starts with really only two elementary basis functions; the rest follows from symmetry, scaling, translation and linear combinations Easy “synthesis” - since the interpolating function is a finite linear combination with the data as the coefficients it is easy to construct Easy “analysis” - if we are given a cubic splines function we can find the coefficients by evaluating function and its derivative at the nodes Smoothness – good but not great. We have continuous first derivatives, but generally discontinuous second derivatives. Easy implementation - it is straight forward to program a computer to set up and evaluate cubic splines efficiently. Find a two-dimensional analog of linear interpolation for specified function values at mn given data points xn , ym x1, ym x , y i x1, y1 x , y ,z j i xn , y1 x1, y1, z11 j ij xn , ym , znm x1, ym , z1m xn , y1, zn1 A closer view of four data points There a are four “bilinear” 2-D elementary basis functions. They are constructed from the 1-D elementary basis functions 1,1,1 0 y 1 t 1 0 1 2 t We define the bilinear 2-D elementary basis functions as follows: 11 x, y 1 x 1 y 12 x, y 1 x 2 y x 21 x, y 2 x 1 y 22 x, y 2 x 2 y The two 1-D elementary basis functions 0 1 t 1 0 2 t The four bilinear 2-D elementary basis functions 1 y y 21 x, y 2 x 1 y x x 11 x, y 1 x 1 y 22 x, y 2 x 2 y y y 12 x, y 1 x 2 y x x We can construct the 2-D bilinear spline basis functions directly from the 1-D linear spline basis functions We define the ijth 2-D bilinear spline basis function as follows: i x xi 1 xi i j x, y i x j y xi 1 x , y ,1 j y y j 1 i yj j y j 1 As in the 1-D case we want our basis functions to be zero at all but one of the data points. x i 1 , y j 1 ,0 We can construct the 2-D bicubic spline basis functions directly from the 1-D bicubic spline basis functions At each data point we define four 2-D bicubic spline basis functions as follows: i x xi 1 xi xi 1 i j x, y i x j y i x xi 1 i j x, y i x j y xi xi 1 i j x, y i x j y i j x, y i x j y x , y ,1 i j y y j 1 yj y j 1 yj y j 1 j j x y j 1 x i 1 , y j 1 ,0 The 2-D bicubic spline basis functions y j 1 y j 1 yj yj y j 1 xi 1 y j 1 xi 1 xi i j x, y i x j y xi 1 xi i j x, y i x j y xi 1 y j 1 y j 1 yj yj y j 1 xi 1 y j 1 xi 1 xi xi i j x, y i x j y xi 1 i j x, y i x j y xi 1 What have we done? Connecting the dots – continuous piecewise linear interpolation using the data to compute a linear equation for each subinterval using a linear combination of “Spline basis functions” Connecting the dashes – smooth, piecewise cubic, interpolation using slope and function data to compute a cubic equation for each subinterval using a linear combination of “Cubic spline basis functions” Interpolation using 2-D splines Bilinear spline basis functions Bicubic spline basis functions Overview – “elementary mathematics from an advanced standpoint” linear algebra, finite dimensional function spaces, inner products, small support, almost orthogonal bases, tensor products, finite element methods, wavelets, … The end