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

1 次查看(过去 30 天)
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

采纳的回答

the cyclist
the cyclist 2024-5-14
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 个评论
Steven Lord
Steven Lord 2024-5-14
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by