Spline Toolbox    
splinetool

Experiment with some spline approximation methods

Syntax

Description

splinetool is a graphical user interface (GUI), whose initial menu provides you with various choices for data including the option of importing some data from the workspace.

splinetool(x,y) brings up the GUI with the specified data x and y, which are vectors of the same length.

Remarks

The Spline Tool is shown below comparing cubic spline interpolation with a smoothing spline on sample data created by adding noise to the cosine function.

Approximation Methods

The approximation methods and options supported by the GUI are shown below.

Approximation Method
Option
Cubic Interpolating Spline
Adjust the type and values of the end conditions.
Smoothing Spline
Choose between cubic (order 4) and quintic (order 6) splines. Adjust the value of the tolerance and/or smoothing parameter. Adjust the weights in the error and roughness measures.
Least-Squares Approximation
Vary the order from 1 to 14. The default order is 4, which gives cubic approximating splines. Modify the number of polynomial pieces. Add and move knots to improve the fit. Adjust the weights in the error measure.
Spline Interpolation
Vary the order from 2 to 14. The default order is 4, which gives cubic spline interpolants. If the default knots supplied are not satisfactory, you can move them around to vary the fit.

Graphs

You can generate and compare several approximations to the same data. One of the approximations is always marked as "current" using a thicker line width. The following displays are available:

By default, the error is the difference between the given data values and the value of the approximation at the data sites. In particular, the error is zero (up to round-off) when the approximation is an interpolant. However, if you provide the data values by specifying a function, then the error displayed is the difference between that function and the current approximation. This also happens if you change the y-label of the data graph to the name of a function.

Menu Options

You can annotate and print the graphs with the File -> Print Graph menu.

You can export the data and approximations to the workspace for further use or analysis with the File -> Export Data and File -> Export Spline menus, respectively.

You can create, with the File -> Save M-file menu, a function M-file that you can use to generate, from the original data, any or all graphs currently shown. This M-file also provides you with a written record of the spline toolbox commands used to generate the current graph(s).

You can save, with the Replicate button, the current approximation before you experiment further. If, at a later time, you click on the approximation so saved, splinetool restores everything to the way it was, including the data used in the construction of the saved approximation. This is true even if, since saving this approximation, you have edited the data while working on other approximations.

You can add, delete, or move data, knots, and breaks by right-clicking in the graph, or selecting the appropriate item in the Edit menu.

Examples

Exploring End Conditions For Cubic Spline Interpolation

The purpose of this example is to explore the various end conditions available with cubic spline interpolation:

  1. Type splinetool at the command line.
  2. Select Provide your own data from the initial screen, and accept the default function. You should see the following display.
  1. The default approximation shown is the cubic spline interpolant with the not-a-knot end condition.

    The vector x of data sites is linspace(0,2*pi,21) and the values given by cos(x). This differs from simply providing the vector y of values in that the cosine function is explicitly recorded as the underlying function. Therefore, the error shown in the graph is the error in the spline as an approximation to the cosine rather than as an approximation to the given values. Notice the resulting relatively large error, about 5e-5, near the endpoints.

  1. For comparison, follow these steps:
  1. This procedure results in the display shown below. Note that the right end slope is zero only up to round-off. Bottomline tells you that the toolbox function csape was used to create the spline.

    Be impressed by the improvement in the error, which is only about 5e-6.

  1. For further comparison, follow these steps:
  1. Note the deterioration of the approximation near the ends, an error of about 2e-3, which is much worse than with the not-a-knot end conditions.

  1. For a final comparison, follow these steps:
  1. Note the dramatic improvement in the approximation, back to an error of about 5e-6, particularly compared to the 'natural' end conditions.

Estimating the Second Derivative at an Endpoint

This example uses cubic spline interpolation and least-squares approximation to determine an estimate of the initial acceleration for a drag car:

  1. Type splinetool at the command line or if the GUI is already running, click on File->Restart.
  2. Choose Richard Tapia's drag racing data. These data show the distance traveled by a drag car as a function of time.
  3. In Approximation method, select complete from the list of End conditions.
  4. Adjust the initial speed by changing the first derivative at the left endpoint to zero.
  5. Look for the value of the initial acceleration, which is given by the value of the second derivative at the left endpoint. You can toggle between the first derivative and the second derivative at this endpoint by clicking on the left end button. The value of the second derivative should be around 187 in the units chosen. Choose View->Show 2nd Derivative to see this graphically.
  6. For comparison, click on New, then choose Least-Squares Approximation as the Approximation method. With this method, you can no longer specify end conditions. Instead, you may vary the order of the method. Verify that the initial acceleration is close to the cubic interpolation value.
  1. The results of this procedure are shown below.

Least-Squares Approximation

This example encourages you to place five interior knots in such a way that the least-squares approximation to these data by cubic splines has an absolute error no bigger than .04 everywhere:

  1. Type splinetool at the command line or if the GUI is already running, click on File->Restart.
  2. Choose Titanium heat data.
  3. Select Least-Squares Approximation as the Approximation method.
  4. Notice how poorly this approximates the data since there are no interior knots. To view the current knots and add new knots, choose knots from Data, breaks/knots, weights. The knots are now listed in knots, and also displayed in the data graph as vertical lines. Notice that there are just the two end knots, each with multiplicity 4.
  5. Right-click in the data graph and choose Add Knot. This brings up crosshairs for you to move with the mouse. Its precise horizontal location is shown in the edit field below the list of knots. A mouse click places a new knot at the current location of the crosshairs. One possible strategy is to place the additional knot at the place of maximum absolute error, as shown in the auxiliary graph below.
  1. If you right-click and choose Replicate Knot, you will increase the multiplicity of the current knot, which is shown by its repeated occurrence in knots. If you don't like a particular knot, you can delete it. To delete a specific knot, you must first select it in either the list of knots or the data graph, and then right-click in the graph and choose Delete Knot.

  1. You could also ask for an approximation using six polynomial pieces, which corresponds to five interior knots. To do this, enter 6 as # pieces in Data, breaks/knots, weights.
  2. After you have the five interior knots, try to make the error even smaller by moving the knots. To do this, select the knot you want to move by clicking on its vertical line in the graph, then use the interface control below knots in Data, breaks/knots, weights and observe how the error changes with the movement of the knot. You can also use the edit field to overwrite the current knot location. You could also try adjust, which redistributes the current knot sequence.
  3. Use Replicate in List of approximations to save any good knot distribution for later use. Rename the replicated approximation to lstsqr using Rename. To return to the original approximation, click on its name in List of Approximations.

Smoothing Spline

This example experiments with smoothing splines:

  1. Type splinetool at the command line or, if the GUI is already running, click on File->Restart.
  2. Choose Titanium heat data.
  3. In Approximation method, choose Smoothing Spline.
  4. Vary parameter between 0 and 1, which changes the approximation from the least-squares straight-line approximation to the "natural" cubic spline interpolant.
  5. Vary tolerance between 0 and some large value, even inf. The approximation changes from the best possible one, the "natural" cubic spline interpolant, to the least-squares straight-line approximation.
  6. As you increase the parameter value or decrease the tolerance value, the error decreases. However, a smaller error corresponds to more roughness, as measured by the size of the second derivative. To see this, choose View->Show 2nd Derivative and vary the parameter and tolerance values once again.
  7. This step modifies the weights in the error measure to force the approximation to pass through a particular data point.
  8. This step modifies the weights in the roughness measure to force the approximation to balance the error with the smoothness of the second derivative.

See Also

csape, csapi, csaps, spap2, spapi, spaps


  spcrv splpp, sprpp