Extracting values from a smoothing spline fit

42 views (last 30 days)
I've created a spline fit using curve fitting
xdat = [1;2;3;4;5;6;7;8;9;10]
ydat = [2;3;5;7;9;14;24;46;57;58]
SplineFit = fit(xdat, ydat, 'smoothingspline');
I can plot this using simply
plot(fitA)
However, what I really want to do is use this plot to find the y values for x values that are not input x values. Sort of an interpolation, but using the spline fit I used.
i.e. I want to find ydat values where xdat=[1.2; 3.6; 6.2; 7.8], using SplineFit
If I were using polynomials, I know I would use the polyval function for this but I don't know what to use considering I've used a smoothing spline.
Any help appreciated

Accepted Answer

John D'Errico
John D'Errico on 2 Jun 2015
Edited: John D'Errico on 2 Jun 2015
feval(SplineFit,xdat)
ans =
2.1463
5.9731
15.993
40.672
You could have known this by looking at what was returned. What is it?
whos SplineFit
Name Size Bytes Class Attributes
SplineFit 1x1 2088 cfit
>> help cfit
cfit Curve Fit Object
A cfit object encapsulates the result of fitting a curve to data.
Use the FIT function or CFTOOL to create cfit objects.
You can treat a cfit object as a function to make predictions or
evaluation the curve at values of X. For example to predict what
the population was in 1905, then use
load census
cf = fit( cdate, pop, 'poly2' );
yhat = cf( 1905 )
cfit methods:
coeffvalues - Get values of coefficients.
confint - Compute prediction intervals for coefficients.
differentiate - Compute values of derivatives.
feval - Evaluate at new observations.
integrate - Compute values of integral.
plot - Plot a curve fit.
predint - Compute prediction intervals for a curve fit or for
new observations.
probvalues - Get values of problem parameters.
See also: fit, fittype, sfit.
Reference page in Help browser
doc cfit
>> methods cfit
Methods for class cfit:
argnames coeffnames dependnames fitoptions integrate numcoeffs probnames type
category coeffvalues differentiate formula islinear plot probvalues
cfit confint feval indepnames numargs predint setoptions
In that list of methods, you would have seen the feval method, clearly what you need.
You would also have seen that SplineFit itself can be used as a function. Thus...
SplineFit(xdat)
ans =
2.1463
5.9731
15.993
40.672
So, read the help! It tells you a lot.
  3 Comments
Mohamed
Mohamed on 9 Nov 2023
May i please know how to use the cfit object in the simulink interface to predict output for random input values ?

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 2 Jun 2015
I think that's what my attached demo does.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!