If your data is noisy, you might want to fit it using a smoothing spline. Alternatively, you can use one of the smoothing methods described in Filtering and Smoothing Data.
If the weights are not specified, they are assumed to be 1 for all data points.
p is defined between 0 and 1. p = 0 produces a least-squares straight-line fit to the data, while p = 1 produces a cubic spline interpolant. If you do not specify the smoothing parameter, it is automatically selected in the "interesting range." The interesting range of p is often near 1/(1+h3/6) where h is the average spacing of the data points, and it is typically much smaller than the allowed range of the parameter. Because smoothing splines have an associated smoothing parameter, you might consider these fits to be parametric in that sense. However, smoothing splines are also piecewise polynomials like cubic spline or shape-preserving interpolants and are considered a nonparametric fit type in this guide.
The smoothing spline algorithm is based on the
The nuclear reaction data from the file carbon12alpha.mat is shown here with three smoothing spline fits. The default smoothing parameter (p = 0.99) produces the smoothest curve. The cubic spline curve (p = 1) goes through all the data points, but is not quite as smooth. The third curve (p = 0.95) misses the data by a wide margin and illustrates how small the "interesting range" of p can be.
In the Curve Fitting app, select
Smoothing Spline from
the model type list.
You can specify the following options:
To make a smoother fit further from the data, click the < Smoother button repeatedly until the plot shows the smoothness you want.
To make a rougher fit closer to the data, click the Rougher > button until you are satisfied with the plot.
Alternatively, specify any value from 0 to 1 for the smoothing parameter. 0 produces a linear polynomial fit (a least-squares straight-line fit to the data), while 1 produces a piecewise cubic polynomial fit that passes through all the data points (a cubic spline interpolant).
Click Default to return to the initial value. The toolbox attempts to select a default value appropriate for your data. See About Smoothing Splines.
Load the data in About Smoothing Splines by entering:
In the Curve Fitting app, select
angle for X
counts for Y data.
Smoothing Spline fit
Try smoothing parameter values
and the default value (0.99).
Specify the model type
you call the
For example, to load some data and fit a smoothing spline model:
load enso f = fit(month, pressure, 'smoothingspline') plot(f, month, pressure)
[f,gof,out] = fit( month, pressure, 'smoothingspline')
pvalue in the
out.p = 0.9. The default value depends on the data set.
You can specify the smoothing parameter for a new fit with the
Its value must be between 0 and 1.
For example, to specify a smoothing parameter:
f = fit(month, pressure, 'smoothingspline', 'SmoothingParam', 0.6) plot(f, month, pressure)
fitoptionsto specify a smoothing parameter before fitting:
options = fitoptions('Method','Smooth','SmoothingParam',0.07) [f,gof,out] = fit(month,pressure,'smooth',options)
For an alternative to
you can use the
smoothing spline function or other spline functions that allow greater
control over what you can create. See About Splines in Curve Fitting Toolbox.
This example fits some data using a cubic spline interpolant and several smoothing splines.
Create the variables in your workspace:
x = (4*pi)*[0 1 rand(1,25)]; y = sin(x) + .2*(rand(size(x))-.5);
Open the Curve Fitting app by entering:
the X data and Y data lists.
The Curve Fitting app fits and plots the data.
Fit the data with a cubic spline interpolant by selecting Interpolant fit
type and the Method
The Curve Fitting app fits and plots the cubic spline interpolant.
Enter the Fit name
View the Results pane. Goodness-of-fit
statistics such as RMSE are not defined (shown as
A cubic spline interpolation is defined as a piecewise polynomial
that results in a structure of coefficients (
The number of "pieces" in the structure is one less
than the number of fitted data points, and the number of coefficients
for each piece is four because the polynomial degree is three. You
can examine the coefficient structure
p if you
export your fit to the workspace (e.g., enter
For information on the structure of coefficients, see Constructing and Working with ppform Splines.
Create another fit to compare. Right-click your fit in the Table of Fits and select Duplicate ‘cubicsp'.
Fit the data with a smoothing spline by selecting Smoothing Spline.
The level of smoothness is given by the Smoothing Parameter. The default smoothing parameter value depends on the data set, and is automatically calculated by the toolbox.
For this data set, the default smoothing parameter is close to 1, indicating that the smoothing spline is nearly cubic and comes very close to passing through each data point.
Name the default smoothing parameter fit
If you do not like the level of smoothing produced by the default
smoothing parameter, you can specify any value from 0 to 1. 0 produces
a linear polynomial fit, while 1 produces a piecewise cubic polynomial
fit that passes through all the data points.
The numerical results for the smoothing spline fit are shown here.
For comparison purposes, create another smoothing
spline fit. Right-click your fit in the Table of Fits and
select Duplicate ‘smooth1'.
Change the smoothing parameter to
0.5 and name
Compare the plots for your three fits. Explore the fit behavior beyond the data limits by increasing the default abscissa scale. You change the axes limits with Tools > Axes Limit Control menu item.
Note: Your results depend on random start points and may vary from those described.
Note that the default smoothing parameter produces a curve that is smoother than the interpolant, but is a good fit to the data. In this case, decreasing the smoothing parameter from the default value produces a curve that is smoother still, but is not a good fit to the data. As the smoothing parameter increases beyond the default value, the associated curve approaches the cubic spline interpolant. The cubic spline and default smoothing spline are similar for interior points, but diverge at the end points.