interpolation with curve fitting toolbox
interpolation is a process for estimating values that lie between known data points.
interpolation involves creating of a function f that matches given data values yi at given data sites xi where f(xi) = yi, for all i.
most interpolation methods create the interpolant f as the unique function of the formula
where the form of the functions fj depends on the interpolation method.
for spline interpolation, the fj are the n consecutive b-splines bj(x) = b(x|tj,...,tj k), j = 1:n, of order k for a knot sequence t1 ≤ t2 ≤ ... ≤ tn k.
about interpolation methods
curve fitting toolbox™ supports the interpolation methods described in the following table.
method | description |
---|---|
linear | linear interpolation. this method fits a different linear polynomial between each pair of data points for curves, or between sets of three points for surfaces. |
nearest neighbor | nearest neighbor interpolation. this method sets the value of an interpolated point to the value of the nearest data point. |
cubic spline | cubic spline interpolation. this method fits a different cubic polynomial between each pair of data points for curves, or between sets of three points for surfaces. |
shape-preserving (pchip) | piecewise cubic hermite interpolation (pchip). this method preserves monotonicity and the shape of the data (for curves only). |
biharmonic (v4) | matlab® 4 method. this method fits surfaces that also extrapolate well (for surfaces only). |
thin-plate spline | thin-plate spline interpolation. this method fits smooth surfaces that also extrapolate well (for surfaces only). |
interpolant surface fits use the matlab function for the linear and nearest neighbor methods, and the matlab function for the cubic spline and biharmonic methods. the thin-plate spline method uses the function.
the interpolant method you use depends on several factors, including the characteristics of the data being fit, the required smoothness of the curve, speed considerations, and post-fit analysis requirements. the linear and nearest neighbor methods fit models efficiently, and the resulting curves are not very smooth. the cubic spline, shape-preserving, and biharmonic methods take longer to fit models, and the resulting curves are very smooth.
for example, the following plot shows a nearest neighbor interpolant fit and a
shape-preserving (pchip) interpolant fit for the nuclear reaction data from the
carbon12alpha.mat
sample data set. the nearest neighbor
interpolant is not as smooth as the shape-preserving interpolant.
note
goodness-of-fit statistics, prediction bounds, and weights are not defined for interpolants. additionally, the fit residuals are always 0 (within computer precision) because interpolants pass through the data points.
biharmonic interpolant fits consist of radial basis function interpolants. all other interpolants supported by curve fitting toolbox are piecewise polynomials and consist of multiple polynomials defined between data points. for cubic spline and pchip interpolation, four coefficients describe each piece. curve fitting toolbox uses a cubic (third-degree) polynomial to calculate the four coefficients. refer to the following for more information:
for cubic spline interpolation
for shape-preserving (pchip) interpolation, and for a comparison of pchip and cubic spline interpolation
, , and for surface interpolation
it is possible to fit a single polynomial interpolant to data, with a degree one less than the number of data points. however, the behavior of such fits is unpredictable between data points. piecewise polynomials with lower-order segments do not diverge significantly from the fitting data domain, so they are useful for analyzing a wider range of data sets.
selecting an interpolant fit
select interpolant fit interactively
open the curve fitter app by entering curvefitter
at the
matlab command line. alternatively, on the apps tab, in the math, statistics and
optimization group, click curve fitter.
on the curve fitter tab, in the fit type section, select an interpolant fit. the app fits an interpolating curve or surface that passes through every data point.
in the fit options pane, you can specify the interpolation method value.
for curve data, you can set interpolation method to
linear
, nearest neighbor
,
cubic spline
, or shape-preserving
(pchip)
. for surface data, you can set interpolation
method to nearest neighbor
,
linear
, cubic spline
,
biharmonic (v4)
, or thin-plate
spline
.
for surfaces, the interpolant fit uses the function for
the linear
and nearest neighbor
methods,
the function for the
cubic spline
and biharmonic (v4)
methods, and the function for the
thin-plate spline
method. try the thin-plate
spline
method when you require both smooth surface interpolation
and good extrapolation properties.
tip
if your data variables have very different scales, select and clear
the center and scale check box to see the
difference in the fit. normalizing the inputs can influence the results
of the piecewise linear
and cubic
spline
interpolation, and nearest
neighbor
surface interpolation methods.
fit linear interpolant model using the fit
function
load the census
sample data set.
load census
the variables pop
and cdate
contain data for the population size and the year the census was taken, respectively.
you can use the fit
function to fit any of the interpolant models described in . in this case, fit a linear interpolant model using the 'linearinterp'
option, and then plot the result.
f = fit(cdate,pop,'linearinterp');
plot(f,cdate,pop);
compare linear interpolant models
load the carbon12alpha
sample data set. create both nearest neighbor and pchip interpolant fits using the 'nearestinterp'
and 'pchip'
options.
load carbon12alpha f1 = fit(angle,counts,'nearestinterp'); f2 = fit(angle,counts,'pchip');
compare the fitted curves f1
and f2
by plotting them in the same figure.
p1 = plot(f1,angle,counts); xlim([min(angle),max(angle)]) hold on p2 = plot(f2,'b'); hold off legend([p1;p2],'counts per angle','nearest neighbor','pchip',... 'location','northwest')