Extraction of values from a cfit object

조회 수: 223 (최근 30일)
Antonio
Antonio 2013년 4월 3일
댓글: David Ebert 2023년 2월 8일
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
  댓글 수: 4
John D'Errico
John D'Errico 2020년 7월 27일
@pinar: read my answer.
David Ebert
David Ebert 2023년 2월 8일
@Yougu Yes! Perfect! Thank you! :)

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

답변 (2개)

John D'Errico
John D'Errico 2020년 7월 27일
편집: John D'Errico 2020년 7월 27일
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.
  댓글 수: 1
Davide Dal Bosco
Davide Dal Bosco 2022년 2월 15일
편집: Davide Dal Bosco 2022년 2월 15일
The amount of passive agressiveness in this answer is off the charts

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


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

카테고리

Help CenterFile Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by