Using the NAG Toolbox for MATLAB Part 2 - The E01 Chapter - Interpolation through a set of points

The E01 Chapter - Interpolation through a set of points

In the previous example, when s = 0, we have a cubic spline curve that passes through all of the data points, and could be used to determine function values at any given x within the range of the supplied data points. This is interpolation, and the cubic spline can be a useful interpolating function for some data sets. However, as we have already seen in our discussion of the trade-off when determining values for the smoothness parameter, the interpolant can exhibit unwanted fluctuations in between the data points (see, for example, the Smoothness = 0 curve in Figure 3).

To illustrate this further, consider Figure 4, which shows a cubic spline interpolant, as fitted to another dataset.


Figure 4: Interpolating through data points using a piecewise cubic Hermite interpolant.

Here, we see that while the cubic spline appears to be a reasonable interpolant for low x values, it shows large fluctuations near the endpoints. These might be undesirable - for example, if the data represented some physical quantity whose value could not exceed 1, the behaviour of the cubic spline would be unacceptable.

However, other interpolants are available - for example, in chapter E01 of the NAG Toolbox, the e01be routine fits a piecewise cubic Hermite interpolant through a set of data points. This preserves monotonicity - that is, it will always increase, or decrease, over the same intervals as the data. As can be seen from Figure 4, this interpolant has no fluctuations, and might be more physically valid for our data set.

Here's the code for the e01be example. Once again, the NAG Toolbox provides an supplementary routine - here, e01bf - which can be used to evaluate the interpolant at any point, given the results from e01be.

   
% Here's the (x,y) data to be fitted.  Note that y is 
% monotonically increasing.
x = [7.5;
8.09;
8.19;
8.69;
9.19;
10;
12;
15;
20];
y = [0;
0;
0.04375;
0.16918;
0.46943;
0.94374;
0.99864;
0.99992;
0.99999];

% Now call the NAG routine e01be to compute the Hermite interpolant
% through the data.
[d, ifail] = e01be(x, y);

% Set up the points at which the interpolant is to be calculated.
px = [x(1) : 0.01 : x(9)];

% Call the NAG routine e01bf to calculate the interpolant, and plot it.
[pf, ifail] = e01bf(x, f, d, px);
plot (px, pf, '-b', 'LineWidth', 2);

The MATLAB script for this demo is available as the file NAGToolboxDemos/Interpolation/e01be_demo.m, distributed in this archive.