New users of splines ask this question so many times. I can understand the question. But a spline fit is not a simple thing where you can easily write down the coefficients and use them. A spline fit is often many, many sets of coefficients, all in tiny pieces of the curve. It is easy for a computer to use. But there is nothing you want to write down or use. No simple function. For example...
spl = spline(x,y)
spl =
form: 'pp'
breaks: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
coefs: [20×4 double]
pieces: 20
order: 4
dim: 1
So the coefficients of this curve are a list of 80 double precision numbers. And you want all of those digits, else you can see strange things happen.
spl.coefs
ans =
0.154084130101602 -0.710628114446516 0.096846290213054 1.000000000000000
0.154084130101601 -0.248375724141711 -0.862157548375173 0.540302305868140
0.108934280137394 0.213876666163093 -0.896656606353790 -0.416146836547142
-0.062230197223020 0.540679506575276 -0.142100433615422 -0.989992496600445
-0.169251096445444 0.353988914906214 0.752567987866068 -0.653643620863612
-0.122520052724908 -0.153764374430117 0.952792528342165 0.283662185463226
0.037352878990571 -0.521324532604840 0.277703621307207 0.960170286650366
0.162750414411966 -0.409265895633125 -0.652886806930759 0.753902254343305
0.138551779282275 0.078985347602773 -0.983167354961111 -0.145500033808614
-0.013040630732633 0.494640685449598 -0.409541321908740 -0.911130261884677
Each row there represents a specific cubic polynomial, on a specific domain. As well, in order to use those coefficients, you need to know the list of breakpoints in the spline.
So splines are a great thing for computers to use. They can evaluate the things easily enough. But a spline is just not something where you want to write down the coefficients, looking at them and hope they make sense.
Now, IF your goal is to be able to simply call that spline as a function, that part is absolutely trivial! Here, for example, there are several things you might do.
Simple option 1:
fnval(spl,1:3)
ans =
0.540302305868140 -0.416146836547142 -0.989992496600445
You can pass spl around to other functions. Use the function fnval to evaluate it at any point. You could do the same like this too, where I will use ppval:
ppval(spl,1:3)
ans =
0.540302305868140 -0.416146836547142 -0.989992496600445
So ppval will do the same thing as fnval, if you lack fnval, or have a really old release of MATLAB. (fnval comes from the curve fitting toolbox.)
If you want something that looks even simpler, just do this:
splfn = @(x) fnval(spl,x);
Now you can pass splfn around as if it was a variable. You can use splfn as a function.
splfn(1:3)
ans =
0.540302305868140 -0.416146836547142 -0.989992496600445
If you want to integrate it, you cannot use int, because this is not a symbolic tool, but integral is quite happy. As you can see here, I just pass the variable splfn to integral.
Oh, I just remembered that you want to extract the functions FROM the curve fitting toolbox. You don't need to, since you can use the result returned as a function itself.
F = fit(x',y','smoothingspline')
F =
Smoothing spline:
F(x) = piecewise polynomial computed from p
Coefficients:
p = coefficient structure
Now you can use that itself as a function to evaluate at any point or list of points.
You can also extract it from the variable returned. Since I don't remember the specific way to do that, learn to use the methods utility!
methods(F)
Methods for class cfit:
argnames cfit coeffvalues dependnames feval formula integrate numargs plot probnames setoptions
category coeffnames confint differentiate fitoptions indepnames islinear numcoeffs predint probvalues type
That suggests coeffvalues might help.
spl = coeffvalues(F)
ans =
form: 'pp'
breaks: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
coefs: [20×4 double]
pieces: 20
order: 4
dim: 1
and now spl is again in a pp spline form. fnval can evaluate it, or ppval.