Mohiuddin Ahmad SUNG-BONG JANG Interpolation II (8.4 SPLINE INTERPOLATION) (8.5 MATLAB’s INTERPOLATION Functions)
Computer Vision and Pattern Recognition Lab. Mohiuddin Ahmad ID: Computer Vision and Pattern Recognition Lab. Dept. of Computer Science and Engineering Korea University 8.4 : Spline Interpolation
Computer Vision and Pattern Recognition Lab. Spline Interpolation Interpolation qualities Piecewise polynomial Want smooth curves Local control Local data changes have local effect Continuous with respect to the data No wiggling if data changes slightly Low computational effort Items to be discussed Piecewise linear interpolation Piecewise quadratic interpolation Cubic spline interpolation
Computer Vision and Pattern Recognition Lab. Spline Interpolation Piecewise Linear Interpolation Simplest form of piecewise polynomial interpolation Interpolate the data with piecewise linear function
Computer Vision and Pattern Recognition Lab. Piecewise Linear Interpolation Example 8.14 Piecewise Linear Interpolation Function is continuous, but not smooth Figure 8.18 Piecewise linear interpolation
Computer Vision and Pattern Recognition Lab. Piecewise Quadratic Interpolation “Knots” Where the intervals meet to be the midpoints between the data points where the function values are given Processing 4 data points Define node points Spacing between consecutive data points Relationships:
Computer Vision and Pattern Recognition Lab. Piecewise Quadratic Interpolation We define Since, Equations at the interior nodes [impose continuity condition on the polynomials]
Computer Vision and Pattern Recognition Lab. Piecewise Quadratic Interpolation Equations at the interior nodes [impose continuity condition on the first derivative of the polynomials] We have 6 equations & 8 unknown coefficients (a 1, a 2, a 3, a 4, b 1, b 2, b 3, b 4 ) Equations for the coefficients at the zero slope conditions at the end points of the interval
Computer Vision and Pattern Recognition Lab. Piecewise Quadratic Interpolation – Example Example : 8: 15 Piecewise interpolating polynomial
Computer Vision and Pattern Recognition Lab. Piecewise Cubic Interpolation Cubic splines → piecewise cubic polynomial Calculation of the coefficients of cubic polynomial Cubic spline function, S(x)
Computer Vision and Pattern Recognition Lab. Piecewise Cubic Interpolation Cubic spline function S(x), n-cubic polynomials P i (x) with coefficients p i,0, p i,1, p i,2, and p i,3 satisfy the following properties: 1. S(x) consists of piecewise cubic (I) 2. Piecewise cubic interpolate the given set of data points (II) 3. Piecewise cubics represent a smooth continuous function (III & IV) 4. Second derivative of the resulting function is continuous (V) S(x) is piecewise cubic, S // (x) is linear on [x 0, x n-1 ]..
Computer Vision and Pattern Recognition Lab. Piecewise Cubic Interpolation Use S // (x i )=a i-1, S // (x i+1 )=a i and h i =x i+1 – x i, we get, Integrating twice will introduce two constant of integration, and the cubic function is From, definition of h i, Integrating P // i (x) twice and using P i (x i ) =y i & P i (x i+1 ) =y i+1 : Determine the value of b i and c i.
Computer Vision and Pattern Recognition Lab. Piecewise Cubic Interpolation Construction of cubic spline From property IV, 1 st derivative of P i and P i+1 must agree at x i+1 Natural cubic spline assigns, 2 nd derivative is zero at the endpoints Error in cubic spline interpolation is |S(x) – g(x) | S(x) : spline interpolation function g(x) : function that generated the data Let, h : maximum spacing between node points, G=max|g (4) (x)|. Condition: |S(x) – g(x) | < kh 4 G =O(h 4 )
Computer Vision and Pattern Recognition Lab. Choice of Two Additional Conditions End point constraints for a cubic spline Description of the strategyEquation involving a 0 and a n (i) Clamped cubic spline: specify S / (x 0 ), S / (x n ) (ii) Natural cubic spline (iii) Extrapolated S // (x) to the end points (iv) S // (x) is constant near the end points (v)Specify S // (x) at each endpoint
Computer Vision and Pattern Recognition Lab. Example 8.16 Natural Cubic Spline Interpolation Example 8:16
Computer Vision and Pattern Recognition Lab. Example 8.16 Natural Cubic Spline Interpolation Example 8:16 – cont’d
Computer Vision and Pattern Recognition Lab. Matlab function for Natural Cubic Spline Matlab code
Computer Vision and Pattern Recognition Lab. Matlab function for Natural Cubic Spline
Computer Vision and Pattern Recognition Lab. Example 8.17 Runge Function Using tridiagonal solution a1 = , a2 = , a3 = Solving for bi & ci gives b1 = , b2 = , b3 = , b4 = c1 = , c2 = , c3 = , c4 =
Computer Vision and Pattern Recognition Lab. Example 8.17 Runge Function Using tridiagonal solution a1= ; a2 = ; a3= ; a4= ; a5= ; a6= ; a7=1.2221; Consider natural cubic spline, a0 = 0; a8 = 0; Solving for bi & ci gives b1 = , b2 = , b3 = , b4 = , b5 = , b6 = , b7 = , b8 = c1 = , c2 = , c3 = , c4 = , c5 = , c6 = , c7 = , c8 =
Computer Vision and Pattern Recognition Lab. Example 8.17 Runge Function Cubic spline simplifies p1 = *( x ).^3 / *( x) *(x ) ; -1 ≤x ≤ ; p2 = *( x).^3 / * (x ).^3 / *( x) *(x ); ≤x ≤ ; p3 = *( x).^3 / * (x ).^3 / *( x) * (x ) ; ≤x ≤ ; p4 = *( x).^3 / * (x ).^3 / *( x) * (x ); ≤x ≤ 0.0 ; p5 = *( x).^3 / * (x ).^3 / *( x) * (x ) ; 0.0 ≤x ≤ 0.25 ; p6 = *(0.5 - x).^3 / * (x ).^3 / *( x) * (x ) ; 0.25 ≤x ≤ 0.50 ; p7 = *( x).^3 / * (x ).^3 / *( x) * (x ); 0.5 ≤x ≤ 0.75 ; p8 = *( x).^3 / * (x ).^3 / *( x) * (x ) ; 0.75 ≤x ≤ 1.00 ;
Computer Vision and Pattern Recognition Lab. Example 8.18, Chemical Reaction Product Data Chemical reaction of data Conclusion: Curve is smoother than higher degree polynomial [ Fig. 8.6] Additional data points do not really improve the appearance of original curve [Fig. 8.5]
Computer Vision and Pattern Recognition Lab. Example: 8.19 Difficult data Data points: Tridiagonal system of equations: Using tridiagonal system a0 = 0 a1= a2= a3= a4= a5= a6= a7= a8 = 0 Solving for bi & ci gives b1 = 0, b2 = , b3 = , b4 = , b5 = , b6 = , b7 = , b8 = c1 = , c2 = , c3 = , c4 = c5 = , c6 = , c7 = , c8 = 0
Computer Vision and Pattern Recognition Lab. Example: 8.19 Difficult data – cont’d Resulting piecewise function made the following polynomial
SUNG-BONG JANG Interpolation II (8.5,MATLAB’s INTERPOLATION Functions)
26 / 7 ■ Interpolation in One-Dimensional MATLAB function : Interp yy = interp1(x,y,xx,method) : interpolates using alternative methods: ‘linear‘ : Linear interpolation (default) 'nearest‘ : Nearest neighbor interpolation ‘cubic’ : Piecewise cubic Hermite interpolation 'spline‘ : Cubic spline interpolation method : linear x = 0:10; y = sin(x); xi = 0:.25:10; yi = interp1(x,y,xi); plot(x,y,'o',xi,yi)
27 / 7 ■ Interpolation in One-Dimensional method : nearest x = 0:10; y = sin(x); xi = 0:.25:10; yi = interp1(x,y,xi,’nearest’); plot(x,y,'o',xi,yi) method : cubic x = 0:10; y = sin(x); xi = 0:.25:10; yi = interp1(x,y,xi,’cubic’); plot(x,y,'o',xi,yi)
28 / 7 ■ Interpolation in One-Dimensional method : spline x = 0:10; y = sin(x); xi = 0:.25:10; yi = interp1(x,y,xi,’spline’); plot(x,y,'o',xi,yi) Faster methods - the data are given at equally spaced intervals(i.e, x is equally spaced and monotonic): *linear,*cubic,*spline > What is Monotonic ? : A function which is either entirely nonincreasing or nondecreasing.nonincreasingnondecreasing A function is monotonic if its first derivative (which need not be continuous) does notderivative change sign. - the data that are not uniformly spaced : interp1q. - MATLAB ‘plot’ function : linear interpolation
29 / 7 ■ Interpolation in Two Dimensionals Overview -much more difficult than for a single variable. -Defined on a rectangular grid : z(i,j) = f(x(i),y(j)); -Interp2 : three ways in which the interp2 can be used. > ZI = interp2(Z,ntimes) expands Z by interleaving interpolates between every element, working recursively for ntimes. interp2(Z) is the same as interp2(Z,1). > ZI = interp2(Z,XI,YI) assumes that X = 1:n and Y = 1:m, where [m,n] = size(Z). > ZI = interp2(X,Y,Z,XI,YI) returns matrix ZI containing elements corresponding to the elements of XI and YI and determined by interpolation within the two-dimensional function specified by matrices X, Y, and Z.
30 / ZI = interp2(Z,ntimes) - expands the data matrix z by interleaving interpolanes between every element. xx = [x 1, (x 2 + x 2 )/2, x 2,….,(x n-1 + x n-1 )/2, x n ] yy = [y 1, (y 2 + y 2 )/2, y 2,….,(y n-1 + y n-1 )/2, y n ] - zz = interp2(z); zz = interp2(z,k) - Example) x=[2,4], y =[3,5,7], z= x’y, >> interp2(z) ans = >> interp2(z,2) ans = mesh(x,y,z’) * mesh(X,Y,Z) : draws a wireframe mesh with color determined by Z so color is proportional to surface height. If X and Y are vectors, length(X) = n and length(Y) = m, where [m,n] = size(Z). ■ Interpolation in Two Dimensionals
31 / 7 ■ Interpolation in Two Dimensionals ZI = interp2(Z,XI,YI) - the data values beiing interpolated are defined on a mesh given by the vectors x=1:n and y=1:m, where [m,n] = size(z); y gives the rows of the grid and x gives the column - zz = interp2(z’,xx,yy’); - Example) x=[1 2 3] y=[ ] z = x’*y z= Xx= [1, 1.2,2,2.2,3] Yy=[1,1.8,2,4,4.8,5] Zz = interp2(z ’,xx,yy ’ ) = - mesh(xx,yy,zz)
32 / 7 ■ Interpolation in Two Dimensionals ZI = interp2(X,Y,Z,XI,YI), - Interpolation on a Rectangular Grid - Example) x=[2,4], y =[3,5,7], z= x’y, xx=[2, 2.4,2.6,2.8,3,3.5,4] yy = [3,3.5,5,7] zz= interp2(x,y’,z’,xx,yy’) >> zz=interp2(x,y,z',xx,yy') zz = mesh(xx,yy,zz)
33 / 7 ■ Interpolation in Two Dimensionals bilinear interpolation - The value of an interpolated point is a combination of the values of the four closest points. - the form of each rectangular subregion z= a + bx + cy + dxy, Region Rij, four corners : data values: (x i,y j ),(x i,y i+1 ),(x i+1,y J ),(x i+1,y i+1 ), four unknowns : a ij,b ij,c ij,d ij z(i,j) = a ij + b ij x i + c ij y j +d ij x i y j z(i,j+1) = a ij + b ij x i + c ij y j+1 +d ij x i y j+1 z(i+1,j) = a ij + b ij x i+1 + c ij y j +d ij x i+1 y j z(i+1,j+1) = a ij + b ij x i+1 + c ij y j+1 +d ij x i+1 y j+1 - the form of the interpolation methods : ZI = interp2(X,Y,Z,XI,YI,method), method=> ‘cubic’,nearest’,’linear’ - interp3 : VI = interp3(X,Y,Z,V,XI,YI,ZI) interpolates to find VI, the values of the underlying three-dimensional function V at the points in arrays XI,YI and ZI. 3-dimensional - interpn : VI = interpn(X1,X2,X3,...,V,Y1,Y2,Y3,...) interpolates to find VI, the values of the underlying multidimensional function V at the points in the arrays Y1, Y2, Y3, etc. n- dimensional