Can we get functions from the curve fitting toolbox?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Matthew Hunt
am 19 Jan. 2023
Kommentiert: Matthew Hunt
am 14 Mär. 2023
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 Kommentare
Mathieu NOE
am 19 Jan. 2023
I don't know exactly if you want just the fitted data or the spline parameters ?
Akzeptierte Antwort
John D'Errico
am 19 Jan. 2023
Bearbeitet: John D'Errico
am 19 Jan. 2023
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)
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
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]
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)
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)
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)
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')
Now you can use that itself as a function to evaluate at any point or list of points.
F(5.43)
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)
That suggests coeffvalues might help.
spl = coeffvalues(F)
and now spl is again in a pp spline form. fnval can evaluate it, or ppval.
0 Kommentare
Weitere Antworten (1)
Steven Lord
am 19 Jan. 2023
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Spline Postprocessing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!