April 2007
Cubic Spline Interpolation - Regression versus Interpolation


Regression is a mathematical process that attempts to find a best-fit curve through a set of data. The result is a function that represents the general trend and serves as an approximation to your data.

Interpolation takes a "connect-the-dots" approach and allows you to estimate values between measured data points. While it is often desirable to have a closed-form function describing your data, there are circumstances when interpolation is the only feasible option. This includes data that is described by an extremely complex function or data that cannot be described by a function at all.

 

 

Linear Interpolation

 

Linear interpolation is used when each data point is connected to the next by a straight line. The linear interpolation function linterp returns an interpolated value at point x given the vectors of data vx and vy. To find the interpolated value for a particular x,linterpconnects the two points between which the x value lies, and returns the corresponding y value that lies on the line connecting the points.

 

 

 

 

 


Although linear interpolation is quite straightforward, there are limitations. Since the curve is not smooth, there are no derivatives at the corners.   

 

Cubic Spline Interpolation

 

To obtain a smooth, differentiable curve, you can use polynomial interpolation where each section is represented by a cubic polynomial of the form:


Polynomial splines can be used to create well-behaved (differentiable) functions. Though they do not represent closed-form expressions, they are useful in many of the same kinds of mathematical analyses. 

 

The following conditions must be satisfied for two adjacent pieces:

 
1. Both pieces must contain the data point. In other words, the pieces must connect.
 


 
2. The first derivative of both pieces at the data point should be the same.
 

 

3. The second derivative of both pieces at the data point should be the same.

 

 

Each of the spline functions returns a vector that is the same length as your x and y data. This vector contains the values of the second derivative of the desired spline at each point of x.

 

Polynomial splines return very suspect values near the end points. For this reason, Mathcad has three options for the choice of boundary conditions:

 
  • lspline: The second derivative at both endpoints is equal to 0.
  • pspline: The second derivative at each endpoint is identical to that of the nearest point.
  • cspline: The secondderivative at each point is determined by the straight line through the nearest two points.
 

The vector of second derivatives returned by the spline routines is found by solving a linear system of equations. The arrays describing this system are set up below. Note that changing the type of endpoint derivatives only affects a few elements of the coefficient matrix.

 

Specify the data:

 

 

 

 

 

 

 

Specify the constant vector:

 

 

 

 

Specify the coefficient matrix:

 

 

 

 

 

 

Set the type of interpolation to perform (where 0 = lspline, 1 = pspline and 2 = cspline):

 

 

 

 

 

 

The value returned by the corresponding spline function is given by solving the system M * vs = b for vs.

 

 

 

Define the following function for comparison purposes:

 

  

 

Note: the first three values are used internally by Mathcad.

 
The interp Function
 

The interp function is used to construct the interpolated points from the output of the spline functions.

 

Pass the results of one of the spline functions, the x vector, the y vector and the point at which you would like an interpolated value to the interp function:

 

 

 

To create functions, make the last value in the call to interp a variable and assign the results to a function of that variable.

 

 

 

 

 

 


Once you have your data defined as an interpolated function, you can then use it as you would any other function. A common application of this is to integrate over a set of data:

 

 

The choice of lspline, pspline, or cspline is frequently just a matter of personal preference. The graphs of the three different splines which result from each choice for the example data set are shown below. Notice that the curves differ slightly at the endpoints, but appear similar over the middle portions. 

 

When using linterp, lspline, pspline, cspline, and interp remember:

 

1. The x and y data arguments must be vectors of the same size.

 

 

 

 

2. The data in the x-vector must be in strictly increasing order.

 

 

 

 

This means that no two x-values should be the same (identical or agree to Mathcad precision). The curves you are fitting correspond to functions, and the graph of a function cannot contain two points for the same x-value.

 

 

 

 

The Two-Dimensional Case

 

If you have a square matrix of values (meaning you have the same number of x points as y points in a grid), then you can use the cubic spline functions in two dimensions as well.

 

 
Create your x and y vectors. Remember, the row and column labels are used as your x and y values when you have a matrix of data.
 

 
 

 

 

Combine the x and y data so you have a 2 x n array, where n is the number of rows of data.

 

 

Pass the Mxy and Mz data to one of the spline functions.

 

 
As in the one-dimensional case, the results of the spline and the data are passed to the interp function. However, since this is a two-dimensional case, the last argument to interp should be a vector containing the x and y point at which you would like the interpolated value.
 

\

 

 




Right-click to download Mathcad file (version Mathcad 11). You must have Mathcad installed on your computer before you can view this file. 


Was this article interesting? Let us know.









[PRINTER FRIENDLY VERSION]
HOME

Top-Down Design in Motion
PTC Updates
Tips of the Month
Mathcad Methods
Knowledge Base Exclusive
Webcasts & Events
Cable Harness Design — Making the Connection
Understanding Ranges, Sequences and Vectors

Contact PTC | Privacy Policy | PTC Express Archive | Subscribe | Unsubscribe | Change Preferences | Edit Profile

This e-mail was sent to:   PTC, 140 Kendrick Street, Needham, MA 02494 USA
If you are unable to read this page correctly, please click here