PIECEWISE INTERPOLATION IN MATLAB

Use MATLAB to fit nine equally spaced data points sampled from this function in the interval [ −1, 1]. Employ a a not-a-knot spline and b a clamped spline with end slopes of f ′ 1 = 1 and f ′ n− 1 = − 4. Solution. a The nine equally spaced data points can be generated as in x = linspace-1,1,9; y = 1.1+25x.2; Next, a more finely spaced vector of values can be generated so that we can create a smooth plot of the results as generated with the spline function: xx = linspace-1,1; yy = splinex,y,xx; Recall that linspace automatically creates 100 points if the desired number of points are not specified. Finally, we can generate values for Runge’s function itself and display them along with the spline fit and the original data: yr = 1.1+25xx.2; plotx,y,o,xx,yy,xx,yr,-- As in Fig. 18.6, the not-a-knot spline does a nice job of following Runge’s function with- out exhibiting wild oscillations between the points. b The clamped condition can be implemented by creating a new vector yc that has the desired first derivatives as its first and last elements. The new vector can then be used to 18.5 PIECEWISE INTERPOLATION IN MATLAB 445 1 0.8 0.6 0.2 0.4 ⫺1 ⫺0.5 0.5 1 FIGURE 18.6 Comparison of Runge’s function dashed line with a 9 -point not-a-knot spline fit generated with MATLAB solid line. generate and plot the spline fit: yc = [1 y -4]; yyc = splinex,yc,xx; plotx,y,o,xx,yyc,xx,yr,-- As in Fig. 18.7, the clamped spline now exhibits some oscillations because of the artificial slopes that we have imposed at the boundaries. In other examples, where we have knowl- edge of the true first derivatives, the clamped spline tends to improve the fit.

18.5.2 MATLAB Function:

interp1 The built-in function interp1 provides a handy means to implement a number of differ- ent types of piecewise one-dimensional interpolation. It has the general syntax yi = interp1x, y, xi, method where x and y = vectors containing values that are to be interpolated, yi = a vector con- taining the results of the interpolation as evaluated at the points in the vector xi , and method = the desired method. The various methods are • nearest —nearest neighbor interpolation. This method sets the value of an inter- polated point to the value of the nearest existing data point. Thus, the interpolation looks like a series of plateaus, which can be thought of as zero-order polynomials. • linear —linear interpolation. This method uses straight lines to connect the points. 1 0.8 0.6 0.2 0.4 ⫺1 ⫺0.5 0.5 1 FIGURE 18.7 Comparison of Runge’s function dashed line with a 9 -point clamped end spline fit generated with MATLAB solid line. Note that first derivatives of 1 and − 4 are specified at the left and right boundaries, respectively. • spline —piecewise cubic spline interpolation. This is identical to the spline function. • pchip and cubic —piecewise cubic Hermite interpolation. If the method argument is omitted, the default is linear interpolation. The pchip option short for “piecewise cubic Hermite interpolation” merits more discussion. As with cubic splines, pchip uses cubic polynomials to connect data points with continuous first derivatives. However, it differs from cubic splines in that the second derivatives are not necessarily continuous. Further, the first derivatives at the knots will not be the same as for cubic splines. Rather, they are expressly chosen so that the interpolation is “shape preserving.” That is, the interpolated values do not tend to overshoot the data points as can sometimes happen with cubic splines. Therefore, there are trade-offs between the spline and the pchip options. The results of using spline will generally appear smoother because the human eye can detect dis- continuities in the second derivative. In addition, it will be more accurate if the data are val- ues of a smooth function. On the other hand, pchip has no overshoots and less oscillation if the data are not smooth. These trade-offs, as well as those involving the other options, are explored in the following example. EXAMPLE 18.5 Trade-Offs Using interp1 Problem Statement. You perform a test drive on an automobile where you alternately accelerate the automobile and then hold it at a steady velocity. Note that you never decel- erate during the experiment. The time series of spot measurements of velocity can be tabulated as t 20 40 56 68 80 84 96 104 110 v 20 20 38 80 80 100 100 125 125 Use MATLAB’s interp1 function to fit these data with a linear interpolation, b near- est neighbor, c cubic spline with not-a-knot end conditions, and d piecewise cubic Hermite interpolation. Solution. a The data can be entered, fit with linear interpolation, and plotted with the following commands: t = [0 20 40 56 68 80 84 96 104 110]; v = [0 20 20 38 80 80 100 100 125 125]; tt = linspace0,110; vl = interp1t,v,tt; plott,v,o,tt,vl The results Fig. 18.8a are not smooth, but do not exhibit any overshoot. b The commands to implement and plot the nearest neighbor interpolation are vn = interp1t,v,tt,nearest; plott,v,o,tt,vn 18.5 PIECEWISE INTERPOLATION IN MATLAB 447 As in Fig. 18.8b, the results look like a series of plateaus. This option is neither a smooth nor an accurate depiction of the underlying process. c The commands to implement the cubic spline are vs = interp1t,v,tt,spline; plott,v,o,tt,vs These results Fig. 18.8c are quite smooth. However, severe overshoot occurs at several locations. This makes it appear that the automobile decelerated several times during the experiment. 140 120 100 80 60 40 20 20 40 60 80 100 120 a linear c spline d pchip b nearest neighbor 140 120 100 80 60 40 20 20 40 60 80 100 120 140 120 100 80 60 40 20 20 40 60 80 100 120 140 120 100 80 60 40 20 20 40 60 80 100 120 FIGURE 18.8 Use of several options of the interp1 function to perform piecewise polynomial interpolation on a velocity time series for an automobile. d The commands to implement the piecewise cubic Hermite interpolation are vh = interp1t,v,tt,pchip; plott,v,o,tt,vh For this case, the results Fig. 18.8d are physically realistic. Because of its shape-preserving nature, the velocities increase monotonically and never exhibit deceleration. Although the result is not as smooth as for the cubic splines, continuity of the first derivatives at the knots makes the transitions between points more gradual and hence more realistic.

18.6 MULTIDIMENSIONAL INTERPOLATION

The interpolation methods for one-dimensional problems can be extended to multidimen- sional interpolation. In this section, we will describe the simplest case of two-dimensional interpolation in Cartesian coordinates. In addition, we will describe MATLAB’s capabili- ties for multidimensional interpolation.

18.6.1 Bilinear Interpolation

Two-dimensional interpolation deals with determining intermediate values for functions of two variables z = f x i , y i . As depicted in Fig. 18.9, we have values at four points: f x 1 , y 1 , f x 2 , y 1 , f x 1 , y 2 , and f x 2 , y 2 . We want to interpolate between these points 18.6 MULTIDIMENSIONAL INTERPOLATION 449 f x, y f x i , y i x i , y i f x 1 , y 2 y 2 y i y 1 x 1 x i x 2 x y f x 2 , y 2 f x 2 , y 1 f x 1 , y 1 FIGURE 18.9 Graphical depiction of two-dimensional bilinear interpolation where an intermediate value filled circle is estimated based on four given values open circles. to estimate the value at an intermediate point f x i , y i . If we use a linear function, the re- sult is a plane connecting the points as in Fig. 18.9. Such functions are called bilinear. A simple approach for developing the bilinear function is depicted in Fig. 18.10. First, we can hold the y value fixed and apply one-dimensional linear interpolation in the x di- rection. Using the Lagrange form, the result at x i , y 1 is f x i , y 1 = x i − x 2 x 1 − x 2 f x 1 , y 1 + x i − x 1 x 2 − x 1 f x 2 , y 1 18.29 and at x i , y 2 is f x i , y 2 = x i − x 2 x 1 − x 2 f x 1 , y 2 + x i − x 1 x 2 − x 1 f x 2 , y 2 18.30 These points can then be used to linearly interpolate along the y dimension to yield the final result: f x i , y i = y i − y 2 y 1 − y 2 f x i , y 1 + y i − y 1 y 2 − y 1 f x i , y 2 18.31 A single equation can be developed by substituting Eqs. 18.29 and 18.30 into Eq. 18.31 to give f x i , y i = x i − x 2 x 1 − x 2 y i − y 2 y 1 − y 2 f x 1 , y 1 + x i − x 1 x 2 − x 1 y i − y 2 y 1 − y 2 f x 2 , y 1 18.32 + x i − x 2 x 1 − x 2 y i − y 1 y 2 − y 1 f x 1 , y 2 + x i − x 1 x 2 − x 1 y i − y 1 y 2 − y 1 f x 2 , y 2 FIGURE 18.10 Two-dimensional bilinear interpolation can be implemented by first applying one-dimensional linear interpolation along the x dimension to determine values at x i . These values can then be used to linearly interpolate along the y dimension to yield the final result at x i , y i . y 1 x 1 x 2 x i y 2 y i f x 1 , y 1 f x 2 , y 1 f x i , y 1 fx i , y i f x, y 2 f x 1 , y 2 f x 2 , y 2