Does the curvefit toolbox return the equation for the best fit line (or curve)?

2 Ansichten (letzte 30 Tage)
For example, let's say that a 5th order polynomial fits the data nicely. Does the curvefit results include the equation, or at least the coefficients, of the 5th order polynomial fit?
Perhaps a silly question, but you might be surprized at how many so-called curve fitting programs are out there that only care about displaying the fit.
Thank you for considering my question.
Todd

Akzeptierte Antwort

the cyclist
the cyclist am 14 Mai 2024
Yes, you can get the coefficients of the best-fit equation. @Torsten's comment illustrates where you can see them in the UI, and you can also get them programatically using the fit function.
  3 Kommentare
Steven Lord
Steven Lord am 14 Mai 2024
And if you export the fit from the app using the last button on the toolstrip, you'll get a cfit or sfit object. You can use a number of different functions to post-process them.
load census
format longg
f = fit(cdate, pop, 'poly2')
f =
Linear model Poly2: f(x) = p1*x^2 + p2*x + p3 Coefficients (with 95% confidence bounds): p1 = 0.006541 (0.006124, 0.006958) p2 = -23.51 (-25.09, -21.93) p3 = 2.113e+04 (1.964e+04, 2.262e+04)
formula(f)
ans = 'p1*x^2 + p2*x + p3'
coeffnames(f)
ans = 3x1 cell array
{'p1'} {'p2'} {'p3'}
coeffvalues(f)
ans = 1x3
1.0e+00 * 0.00654113049421954 -23.5097459954225 21129.5921192301
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Note that not all types of fit give formulas you can use to evaluate the fit.
f2 = fit(cdate, pop, 'spline')
f2 =
Cubic spline interpolant: f2(x) = piecewise polynomial computed from p with cubic extrapolation Coefficients: p = coefficient structure
formula(f2)
ans = 'piecewise polynomial'
But then again you don't need to use the formula to evaluate it if you have the object.
populationFromQuadratic = f(1849)
populationFromQuadratic =
22.8952484620604
populationFromSpline = f2(1849)
populationFromSpline =
22.3548286873149
Todd
Todd am 14 Mai 2024
Bearbeitet: Todd am 14 Mai 2024
This is excellent!! Thank you for such a detailed example.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by