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
Todd
Todd 2024년 5월 14일
편집: Todd 2024년 5월 14일
This is excellent!! Thank you for such a detailed example.

댓글을 달려면 로그인하십시오.

추가 답변 (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