This function creates spline of order 3 using the set of points and interpolates the input between them taking into account the least-squares fit
First and second derivatives from the spline can be calculated by using the derivative1D function.
A CubicSpline is a polynomial function of order 3, defined between an interval
.
When using CubicSplines for interpolation or for fitting, we essentially chain BSplines
of order 3 together so that each spline passes through the breakpoints in that interval.
A Cubic Spline is a specific case of BSpline that only uses polynomials of order 3 to define the spline functions.
Again, as with BSplines , there are certain conditions that must be fulfilled at each breakpoint such that the overall Spline is piecewise-smooth.
To demonstrate these conditions we can set up a basic CubicSpline with 3 breakpoints:
Our CubicSpline will be defined as the following:
Breakpoints :
Our piecewise functions can be written as and
To make our CubicSpline piecewise-smooth we must ensure that these conditions are satisfied:
This point of smoothness is represented by the red circle in the graph below of our CubicSpline function
Fitting using a CubicSpline fit function is different to interpolating with a CubicSpline as it requires the number of breakpoints to be less than the number of data points. This allows the CubicSpline to fit as close as possible to the dataset using a least-squares fit instead of passing through all data points as with interpolation. The fitted curve will pass through the breakpoints however it may not pass through all of the data points in the set.
A example of fitting with a CubicSpline has been provided below, with the breakpoints highlighted in green. The original data plot is in black and our fitted CubicSpline with four breakpoints is in red.
Name | Type | Default | Description |
---|---|---|---|
n | Integer | 3 | Number of breakpoints in Spline |
x0 | Double | - | Position of first exterior breakpoint |
x1 | Double | - | Position of the interior breakpoint |
x2 | Double | - | Position of the last exterior breakpoint |
Name | Default | Description |
---|---|---|
y0 | 0.0 | |
y1 | 0.0 | |
y2 | 0.0 |
Even though the number of fitting parameters is set to 3 by default, this number is based off .
Where
is the number of breakpoints in the fit,
is the order of the spline (stays as 3 for the cubic spline case) and the
accounts for our two exterior breakpoints
.
Increasing the number of breakpoints in your fit will increase the number of fitting parameters by the same amount.
Categories: FitFunctions | Background
C++ source: CubicSpline.cpp (last modified: 2018-10-05)
C++ header: CubicSpline.h (last modified: 2018-10-05)