Interpolation involves using existing data points to predict values between these data points. Mathcad allows you to either connect the data points with straight lines (linear interpolation) or to connect them with sections of a cubic polynomial (cubic spline interpolation).

Unlike the regression functions discussed in the next section, these interpolation functions return a curve which must pass through the points you specify. Because of this, the resulting function is very sensitive to spurious data points. If your data is noisy, you should consider using the regression functions instead.

Linear prediction involves using existing data values to predict values beyond the existing ones. Mathcad provides a function which allows you to predict future data points based on past data points.

Whenever you use arrays in any of the functions described in this section, be sure that every element in the array contains a data value. Since every element in a array must have a value, Mathcad assigns 0 to any elements you have not explicitly assigned

**Linear interpolation**

In linear interpolation, Mathcad connects the existing data points with straight lines. This is accomplished by the linterp function described below.

To find the interpolated value for a particular x, Mathcad finds the two points between which the value falls and returns the corresponding y alue on the straight line between the two points .

For x values before the first point in vx, Mathcad extrapolates the straight line between the first two data points. For x values beyond the last point in vx, Mathcad extrapolates the straight line between the last two data points.

For best results, the value of x should be between the largest and smallest values in the vector vx. The linterp function is intended for interpolation. not extrapolation. Consequently, computed values for x outside this range are unlikely to be useful. Figure 14-5 shows some examples of linear interpolation.

**Cubic spline lnterpolatlon**

Cubic spline interpolation lets you pass a curve through a set of points in such a way that the first and second derivatives of the curve are continuous across each point. This curve is assembled by taking three adjacent points and constructing a cubic polynomial passing through those points. These cubic polynomials are then strung together to form the completed curve.

To fit a cubic spline curve througb a set of points:

• Create the vectors vx and vy containing the x and y coordinates through which you want the cubic spline to pass. The elements of vx should be in ascending order. (Although we use the names vx, vy and vs, there is nothing special about these variable names; you can use whatever names you prefer in your own work.)

• Generate the vector vs := cspline(vx, vy). The vector vs is a vector of intermed iate results designed to be used with interp.

• To evaluate the cubic spline at an arbitrary point, say xO, evaluate

(interp(vs, vx, vy, xO) where vs, vx and vy are the vectors described earlier.

Note that you could have accomplished the same task by evaluating:

As a practical matter, though, you’ll probably be evaluating interp for many different points. Since the call to cspline can be time-consuming, and since the result won’t change from one point to the next, it makes sense to do it once and just reuse the result as described above.

Figure 14-6 shows how to compute the spline curve for the example in Figure 14-5.

Note that the vs array needs to be computed only once, even for multiple interpolations. Since the spline calculations that lead to vs are time-consuming, it is more efficient to store these intermediate results as a vector than it is to recalculate them as needed. In addition to cspline, Mathcad comes with two other cubic spline functions, as described below:

These three functions differ only in the boundary conditions:

• The !spline function generates a spline curve that approaches a straight line at the endpoints.

• The pspline function generates a spline curve that approaches a parabola at the endpoints.

•The cspline function generates a spline curve that can be fully cubic at the endpoints.

To find the interpolated value for a particular x, Mathcad finds the two points between which it falls. It then returns the y value on the cubic section enclosed by these two points. For x values before the first point in vx, Mathcad extrapolates the cubic section connecting the first two points of vx. Similarly, for x values beyond the last point in vx, Mathcad extrapolates the cubic section connecting the last two points of vx.

For best results, do not use the interp function on values of x far from the fitted points. Splines are intended for interpolation, not extrapolation. Consequently, computed values for such x values are unlikely to be useful.

**Interpolating a vector of points**

You can use the vectorize operator to return a whole vector of interpolated values corresponding to a vector of data points. This works with both interp and linterp.

Figure 14-7 shows how to perform this operation. To apply the vectorize operator to the function, click on thefunction name and press [Space] until the function is between the two editing lines. Then press [Ctrl]- (hold down the [Ctrl] key and press the minus sign).

**Multivariate cubic spline interpolation**

Mathcad handles two dimensional cubic spline interpolation in much the same way as the one-dimensional case discussed earlier. Instead of passing a curve through a set of points in such a way that the first and second derivatives of the curve are continuous across each point, Mathcad passes a surface through a grid of points. This surface corresponds to a cubic polynomial in x and y in which the first and second partial derivatives are continuous in the corresponding direction across each grid point.

The first step in two-dimensional spline interpolation is exactly the same as that in the one-dimensional case: specify the points through which the surface is to pass. The procedure, however, is more complicated because you now have to specify a grid of points:

• Create the n x 2 matrix Mxy whose elements, MXYi 0 and MXYi I’ specify the x and Y coordinates along the diagonal of a rectangular grid. This matrix plays the exactly the same role as vx in the one-dimensional case described earlier. Since these points describe a diagonal, the elements in each column of Mxy be in ascending order (MxYi, k < MXYj, k whenever i <j).

• Create the n x n matrix Mz whose ijth element is the z coordinate corresponding to the point x = MXYi, 0 and Y = MXYj, I . This plays exactly the same role as vy in the one-dimensional case described earlier.

• Generate the vector vs:= cspline(Mxy, Mz). The vector vs is a vector of intermediate results designed to be used with interp.

• To evaluate the cubic spline at an arbitrary point, say (xO, yO) , evaluate

For best results, do not use the interp function on values of x and y far from the grid points. Splines are intended for interpolation, not extrapolation. Consequently, computed values for such x and y values are unlikely to be useful.

**Linear prediction**

The functions described so far in this section allow you to find data points lying between existing data points. However, you may need to find data points that lie beyond your existing ones. Mathcad provides the function predict which uses some of your existing data to predict data points lying beyond the existing ones. This function uses a linear prediction algorithm which is useful when your data is smooth and oscillatory, though not necessarily periodic. Linear prediction can be seen as a kind of extrapolation method but should not be confused with linear or polynomial extrapolation.

Returns n predicted values based on m consecutive values from the data vector v. Elements in v should represent samples taken at equal intervals

The predict function uses the last m of the original data values to compute prediction coefficients. Once it has these coefficients, it uses the last m points to predict the coordinates of the (m + 1)st point, in effect creating a moving window m points wide.