Can we get functions from the curve fitting toolbox?

3 次查看(过去 30 天)
I am using the curve fitting toolbox and I have some data that I want to create a function from. I am using a spline function to fir the data to and it looks very nice. What I would like to do, is create a function from that data that I can simply call as a function. Is there a way I can do this easily? It would make my life so easy if I could.
  4 个评论
Mathieu NOE
Mathieu NOE 2023-1-19
I don't know exactly if you want just the fitted data or the spline parameters ?

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2023-1-19
编辑:John D'Errico 2023-1-19
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...
x = 0:20;
y = cos(x);
spl = spline(x,y)
spl = struct with fields:
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.
format long
spl.coefs
ans = 20×4
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) % evaluate the spline spl at the points [1 2 3]
ans = 1×3
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 = 1×3
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 = 1×3
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.
integral(splfn,0,5)
ans =
-0.948978382574896
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.
F(5.43)
ans =
0.589821881493688
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 = struct with fields:
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.

更多回答(1 个)

Steven Lord
Steven Lord 2023-1-19
You can evaluate a fit as shown by the sections on this documentation page and this documentation page whose names start with "Evaluate the fit". If you want to generate MATLAB code from the fit in the Curve Fitter app see this documentation page.

类别

Help CenterFile Exchange 中查找有关 Spline Postprocessing 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by