Smoothing Splines
About Smoothing Splines
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 inFiltering and Smoothing Data.
The smoothing splinesis constructed for the specifiedsmoothing parameterpand the specified weightswi. The smoothing spline minimizes
If the weights are not specified, they are assumed to be 1 for all data points.
pis defined between 0 and 1.p= 0 produces a least-squares straight-line fit to the data, whilep= 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 ofpis often near 1/(1+h3/6) wherehis 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.
Note
The smoothing spline algorithm is based on thecsaps
function.
The nuclear reaction data from the filecarbon12alpha.matis 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” ofpcan be.
Select Smoothing Spline Fit Interactively
Load the data at the MATLAB®command line.
loadcarbon12alpha
Open the Curve Fitter app.
curveFitter
Alternatively, on theAppstab, in theMath, Statistics and Optimizationgroup, clickCurve Fitter.
On theCurve Fittertab, in theDatasection, clickSelect Data. In theSelect Fitting Datadialog box, select
angle
as theX datavalue and计数
as theY datavalue. For details, seeSelecting Data to Fit in Curve Fitter App.On theCurve Fittertab, in theFit Typesection, click the arrow to open the gallery, and clickSmoothing Splinein theSmoothinggroup.
In theFit Optionspane, you can specify theSmoothing Parametervalue.
The defaultSmoothing Parametervalue is close to 1. The app tries to select a default value appropriate for your data. You can change theSmoothing Parametervalue by doing one of the following:
To create a smoother fit further from the data, click the< Smoother按钮多次,直到显示smoothn的阴谋ess you want.
To create a rougher fit closer to the data, click theRougher >button until you are satisfied with the plot.
Alternatively, specify any value from 0 to 1 for the smoothing parameter. A value of 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). For the
carbon12alpha
data set, trySmoothing Parametervalues1
and0.95
.ClickDefaultto return to the initial value.
Fit Smoothing Spline Models Using thefit
Function
This example shows how to use thefit
function to fit a smoothing spline model to data.
Fit a Smoothing Spline Model
Load data and fit a smoothing spline model by specifying'smoothingspline'
when calling thefitfunction.
loadensof = fit(month,pressure,'smoothingspline'); plot(f,month,pressure)
View Calculated Smoothing Parameter
创建模型,使用第三output argument to view the calculated smoothing parameter. The smoothing parameter is thep
value in theout
structure. The default value depends on the data set.
[f,gof,out] = fit(month,pressure,'smoothingspline'); out.p
ans = 0.9000
Specify Smoothing Parameter using'SmoothingParam'
Specify the smoothing parameter for a new fit with the'SmoothingParam'
option. Its value must be between 0 and 1.
f = fit(month,pressure,'smoothingspline','SmoothingParam',0.07); plot(f,month,pressure)
Specify Smoothing Parameter usingfitoptions
Alternatively, usefitoptions
to specify a smoothing parameter before fitting.
options = fitoptions('Method','Smooth','SmoothingParam',0.07); [f,gof,out] = fit(month,pressure,'smooth',options); out.p
ans = 0.0700
For an alternative to'smoothingspline'
, you can use thecsapscubic smoothing spline function or other spline functions that allow greater control over what you can create. SeeIntroducing Spline Fitting.
Compare Cubic and Smoothing Spline Fit Using Curve Fitter
This example compares a cubic spline interpolant fit and a smoothing spline fit using the Curve Fitter app.
Create the data
x
andy
.x = (4*pi)*[0 1 rand(1,25)]; y = sin(x) + .2*(rand(size(x))-.5);
Open the Curve Fitter app.
curveFitter
Alternatively, on theAppstab, in theMath, Statistics and Optimizationgroup, clickCurve Fitter.
On theCurve Fittertab, in theDatasection, clickSelect Data. In theSelect Fitting Datadialog box, select
x
as theX datavalue andy
as theY datavalue.On theCurve Fittertab, in theFit Typesection, click the arrow to open the gallery, and clickInterpolantin theInterpolationgroup.
In theFit Optionspane, specify theMethodas
Cubic spline
. The Curve Fitter app fits and plots the cubic spline interpolant.Rename the fit. In theTable Of Fitspane, double-click theFit Namevalue and enter
CubicSplineFit
.View theResultspane. Some goodness-of-fit statistics, such as RMSE, are not defined forInterpolantfits and their value is NaN.
A cubic spline interpolation is defined as a piecewise polynomial that results in a structure of coefficients (
p
). 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 structurep
if you export your fit to the workspace by enteringCubicSplineFit.p
. For more information on the coefficient structure, seeConstructing and Working with ppform Splines.Create another fit to compare. Right-click the existing fit
CubicSplineFit
on theTable Of Fitstab and selectDuplicate "CubicSplineFit". Rename the new fit toSmoothingSplineFit
.On theCurve Fittertab, in theFit Typesection, select a
Smoothing Spline
fit.In theFit Optionspane, theSmoothing Parameterdefines the level of smoothness. The app calculates theSmoothing Parameterdepending on the data set. For this data set, the defaultSmoothing Parameteris close to 1, indicating that the smoothing spline is nearly cubic and comes very close to passing through each data point.
You can change the level of smoothing by specifyingSmoothing Parameteras a nonnegative scalar in the range [0 1]. SpecifySmoothing Parameteras 0 to create a linear polynomial fit. SpecifySmoothing Parameteras 1 to create a piecewise cubic polynomial fit that passes through all the data points.
See theResultspane for numerical results of the smoothing spline fit.
Compare the plots for the two fits (cubic spline interpolant fit and smoothing spline fit), which you created. The two fits are similar for interior points, but diverge at the end points.
Note
Your results depend on random start points and may vary from those described.