Main Content

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

p i w i ( y i s ( x i ) ) 2 + ( 1 p ) ( d 2 s d x 2 ) 2 d x

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 thecsapsfunction.

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

  1. Load the data at the MATLAB®command line.

    loadcarbon12alpha
  2. Open the Curve Fitter app.

    curveFitter

    Alternatively, on theAppstab, in theMath, Statistics and Optimizationgroup, clickCurve Fitter.

  3. On theCurve Fittertab, in theDatasection, clickSelect Data. In theSelect Fitting Datadialog box, selectangleas theX datavalue and计数as theY datavalue. For details, seeSelecting Data to Fit in Curve Fitter App.

  4. On theCurve Fittertab, in theFit Typesection, click the arrow to open the gallery, and clickSmoothing Splinein theSmoothinggroup.

  5. In theFit Optionspane, you can specify theSmoothing Parametervalue.

    Fit Options pane for smoothing spline fit

    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 thecarbon12alphadata set, trySmoothing Parametervalues1and0.95.

    • ClickDefaultto return to the initial value.

Fit Smoothing Spline Models Using thefitFunction

This example shows how to use thefitfunction 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)

Figure contains an axes object. The axes object with xlabel x, ylabel y contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, fitted curve.

View Calculated Smoothing Parameter

创建模型,使用第三output argument to view the calculated smoothing parameter. The smoothing parameter is thepvalue in theoutstructure. 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)

Figure contains an axes object. The axes object with xlabel x, ylabel y contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, fitted curve.

Specify Smoothing Parameter usingfitoptions

Alternatively, usefitoptionsto 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.

  1. Create the dataxandy.

    x = (4*pi)*[0 1 rand(1,25)]; y = sin(x) + .2*(rand(size(x))-.5);

  2. Open the Curve Fitter app.

    curveFitter

    Alternatively, on theAppstab, in theMath, Statistics and Optimizationgroup, clickCurve Fitter.

  3. On theCurve Fittertab, in theDatasection, clickSelect Data. In theSelect Fitting Datadialog box, selectxas theX datavalue andyas theY datavalue.

  4. On theCurve Fittertab, in theFit Typesection, click the arrow to open the gallery, and clickInterpolantin theInterpolationgroup.

  5. In theFit Optionspane, specify theMethodasCubic spline. The Curve Fitter app fits and plots the cubic spline interpolant.

    Fit Options pane for cubic spline interpolant fit

  6. Rename the fit. In theTable Of Fitspane, double-click theFit Namevalue and enterCubicSplineFit.

  7. View theResultspane. Some goodness-of-fit statistics, such as RMSE, are not defined forInterpolantfits and their value is NaN.

    Results pane for cubic spline interpolant fit

    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 structurepif you export your fit to the workspace by enteringCubicSplineFit.p. For more information on the coefficient structure, seeConstructing and Working with ppform Splines.

  8. Create another fit to compare. Right-click the existing fitCubicSplineFiton theTable Of Fitstab and selectDuplicate "CubicSplineFit". Rename the new fit toSmoothingSplineFit.

  9. On theCurve Fittertab, in theFit Typesection, select aSmoothing Splinefit.

    Fit Options pane for 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.

  10. See theResultspane for numerical results of the smoothing spline fit.

    Results pane for smoothing spline fit

  11. 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.

See Also

Apps

Functions

Related Topics

Baidu
map