FIT function output structure
19 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Sreeja Banerjee
on 8 Jun 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:
1. CFIT Object: http://www.mathworks.com/help/curvefit/cfit.html 2. SFIT object: http://www.mathworks.com/help/curvefit/sfit.html
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 Comment
Yanis Arencibia
on 10 Jun 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.
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!