Fit curve to data, without extrapolation
14 views (last 30 days)
Show older comments
Hello,
I have two 1x391 vectors,
x = [1 1.100 1.200 ...
(increasing), and
y = [-0.0977 0.1177 0.3152...
when I plot a fit with
y_fit = fit(x',y','smoothingspline');
I get a nice fit, but it also fits for x<1. I would like the fit to start at x(1), regardless of what x(1) is as I would like to use it for multiple data sets later. I tried to use the 'StartPoint' fit option but apparently that is not possible for smoothing spline:
Error using curvefit.smoothoptions/set
The name 'StartPoint' is not an accessible property for an instance
of class 'smoothoptions'.
Does anyone how I could get a fit that only covers my data points? For visual purposes, I would like to make a plot with an interpolation connecting my data from x(1) to x(391) without any extrapolation on either side.
Thank you
1 Comment
Answers (1)
John D'Errico
on 20 Oct 2017
I think you misunderstand how things like this work.
A smoothing spline is defined on only the support of your data.
y = rand(size(x));
y_fit = fit(x',y','smoothingspline');
plot(y_fit)
pp = coeffvalues(y_fit)
pp =
struct with fields:
form: 'pp'
breaks: [1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2]
coefs: [10×4 double]
pieces: 10
order: 4
dim: 1
pp.breaks
ans =
1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2
So the spline is defined to live on the support of the data. It essentially lives between the limits of those breaks.
However, can you extrapolate the spline? Yes.
y_fit([-2 3])
ans =
-1076.7
17.006
You will get complete garbage. But that is just a reflection that you should not extrapolate a spline. Some people want to do so, despite the garbage that will almost always result. So the curve fitting toolbox has chosen to allow you to extrapolate.
In my own spline tools (my SLM toolbox), I provide an extrapolation mode as an option. Most of the choices for that option are to avoid extrapolation if at all possible.
3 Comments
John D'Errico
on 20 Oct 2017
Please don't add answers just to make a comment.
Sorry, but you cannot force that tool to not extrapolate.
At best, you could switch to use of my SLM toolbox. My spline toolbox (SLM Toolbox) has an option to tell how any extrapolation will be done. As I recall, the choices that I offer are:
- Generate an error message if you even try to extrapolate.
- Generate a warning message, then extrapolate as a constant function.
- Extrapolate as a constant function, so whatever the end point of the spline was, just return that value.
- Extrapolate linearly from the end of the spline.
- Extrapolate as a cubic polynomial.
- Return a NaN for any point that would have been extrapolated.
That seemed a relatively complete set of choices, all controllable by the person who fit the spline.
See Also
Categories
Find more on Splines in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!