FIT function output structure

19 visualizzazioni (ultimi 30 giorni)
Bob Adams
Bob Adams il 6 Giu 2015
Commentato: Yanis Arencibia il 10 Giu 2023
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.

Risposte (1)

Sreeja Banerjee
Sreeja Banerjee il 8 Giu 2015
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 Commento
Yanis Arencibia
Yanis Arencibia il 10 Giu 2023
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.

Accedi per commentare.

Categorie

Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by