FIT function output structure

조회 수: 19 (최근 30일)
Bob Adams
Bob Adams 2015년 6월 6일
댓글: Yanis Arencibia 2023년 6월 10일
I am using the fit function for the first time, and I notice that all the examples pass the returned object into the PLOT function. However I am not interested in plotting the results, but rather I need to get the fitted curve into an array. I don't see any documentation that show how to do this.

답변 (1개)

Sreeja Banerjee
Sreeja Banerjee 2015년 6월 8일
Hi Bob,
The function FIT returns a cfit object for curves or a sfit object for surfaces. You can learn more about the objects in the documentation:
In short, the CFIT object will return the model formula and the coefficients. For example,
>> load census;
>> 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)
I assume from your question that you want to extract the coefficients into an array. You can do that by using the following syntax:
>> coeff = coeffvalues(f)
coeff =
1.0e+04 *
0.0000 -0.0024 2.1130
You will need use the COEFFVALUES function, details of which you can find in the following link: http://www.mathworks.com/help/curvefit/coeffvalues.html
If you also need to extract the formula and the coefficient names then you may use the following functions:
>> formula(f)
ans =
p1*x^2 + p2*x + p3
>> coeffnames(f)
ans =
'p1'
'p2'
'p3'
  댓글 수: 1
Yanis Arencibia
Yanis Arencibia 2023년 6월 10일
If I may add to Sreeja's answer, a fast and nice way to access the coefficients is as follows:
>> load census;
>> f=fit(cdate,pop,'poly2');
Now you can call (or reuse in another function) the coeffs just typing:
>> f.p1
>> f.p2
>> f.p3
I hope this helps.

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

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by