How to turn a curve fit into a function

86 visualizaciones (últimos 30 días)
mr
mr el 2 de Feb. de 2022
Comentada: Steven Lord el 2 de Feb. de 2022
I have been fitting some data generated in the lab. The best fit is by piecewise polynomial
I would now need to turn this into a function that I can then plug in a model (where I repeat this function with different time delays and then sum them up). What is the best way to do this? If I generate a fit wth the FIT function, I am then unable to use that object as a normal function. Any advice?
  2 comentarios
Steven Lord
Steven Lord el 2 de Feb. de 2022
As Matt J and Walter Roberson stated, you can evaluate the fit by treating it like it was a function. This documentation page shows this technique and also some of the other operations you can perform on the object representing the fitted curve.

Iniciar sesión para comentar.

Respuestas (2)

Matt J
Matt J el 2 de Feb. de 2022
If I generate a fit wth the FIT function, I am then unable to use that object as a normal function.
Sure you can.
x=1:5;
y=x.^2;
f=fit(x(:),y(:),'poly2')
f =
Linear model Poly2: f(x) = p1*x^2 + p2*x + p3 Coefficients (with 95% confidence bounds): p1 = 1 (1, 1) p2 = -1.619e-14 (-1.234e-13, 9.105e-14) p3 = 2.667e-14 (-1.141e-13, 1.674e-13)
f(2)
ans = 4.0000

Walter Roberson
Walter Roberson el 2 de Feb. de 2022
If you generate a fit with the fit function, then the result is usable as a function.
fitobj = fit(X(:), Y(:), 'pchip');
xq = linspace(min(X), max(X));
ypred = fitobj(xq); %notice fitobj has been used as if it is a function
plot(X, Y, '*', xq, ypred, '-')

Categorías

Más información sobre Linear and Nonlinear Regression en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by