MATLAB Answers

Extraction of values from a cfit object

319 views (last 30 days)
Antonio
Antonio on 3 Apr 2013
Commented: John D'Errico on 27 Jul 2020
Dear, I used the fit function to interpolate two set of experimental data (X,Y) with a function fun previously defined with fittype. Fun contains 3 constant A, k1 and k2. After i estimates three constants with by using fit as follow: [fitresult, gof]=fit(X,Y,fun) I obitaining an object containing the fit the followin results: fitresult =
General model:
fitresult(x) = 562.16*exp(-k2*x)+(k1*A/(k2-k1))*(exp(-k1*x)-exp(-k2*x))
Coefficients (with 95% confidence bounds):
A = 1202 (481.5, 1921)
k1 = 5 (-119, 129)
k2 = 0.2374 (-0.1202, 0.595)
This is a cfit object. My question is: How can extract the values of A, k1 and k2 from the above object. I need to use the estimated value for different steps of my script.
Thaks
Antonio

  3 Comments

Yougu
Yougu on 11 Apr 2013
use: fitresult.A; fitresult.k1; fitresult.k2;
is it what you need?
pinar
pinar on 27 Jul 2020
Hello, I was having the same question, but in addition to accessing the values of A and k1, k2, I also want to access the confidence bounds. When I use fitresult.A alone it only gives the value of A without the confidence bounds. Do you have any idea how this could be done?
Thanks in advance :)

Sign in to comment.

Answers (2)

Igor Varfolomeev
Igor Varfolomeev on 25 Nov 2018
The answer by @Yougu
fitresult.A; fitresult.k1; fitresult.k2;
is correct.
For me it was a bit counter-intuitive at first as well :).

  0 Comments

Sign in to comment.


John D'Errico
John D'Errico on 27 Jul 2020
Edited: John D'Errico on 27 Jul 2020
When you do not know how to pull some information from a class, the best thing is to try using methods to learn what you can do.
mdl = fit(rand(10,1),rand(10,1),'poly2')
mdl =
Linear model Poly2:
mdl(x) = p1*x^2 + p2*x + p3
Coefficients (with 95% confidence bounds):
p1 = -0.8558 (-4.362, 2.651)
p2 = 0.6407 (-3.374, 4.655)
p3 = 0.4551 (-0.5222, 1.432)
We have here a very simple fit result. As you see, this works, but it may not be clear how to have known it would work. Sometimes just trying things is sufficient.
mdl.p1
ans =
-0.855805940768612
mdl.p2
ans =
0.640653729424836
mdl.p3
ans =
0.455095631339414
And as much as I want to say this should be the logical thing to try, or that it is shown in the documentation, a reading of the docs for fit does not show me a spot where that is explicitly stated.
But if we want a clear list of the functions we can use to work with mdl, or extract information from it, we use methods.
methods(mdl)
Methods for class cfit:
argnames coeffnames dependnames fitoptions integrate numcoeffs probnames type
category coeffvalues differentiate formula islinear plot probvalues
cfit confint feval indepnames numargs predint setoptions
Hmm, so coeffnames is a method we can use. do you see, I was able to extract the names of the coefficients using one of the methods?
coeffnames(mdl)
ans =
3×1 cell array
{'p1'}
{'p2'}
{'p3'}
One of the other methods listed was coeffvalues. The name alone should give me a hint that coeffvalues might help in our quest.
coeffvalues(mdl)
ans =
-0.855805940768612 0.640653729424836 0.455095631339414
So we can simply and programmatically use the coeffvalues method to do what we wanted, and there WAS a simple way to have learned that.
A followup question asked how to extact the confidence intervals. Again, LOOK AT THE METHODS. One of them is named confint. Does that help? I think it might.
confint(mdl)
ans =
-4.36215292171444 -3.37378195209185 -0.522170718930676
2.65054104017722 4.65508941094152 1.4323619816095
The point is, when you have a class that you have never seen before, you can learrn much by using methods on the class object.
If you wish to get direct help, then we could have done this:
help cfit/coeffvalues
coeffvalues Coefficient values.
coeffvalues(FUN) returns the values of the coefficients of the
CFIT object FUN as a row vector.
The information is there on how to extract what you want.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!