Extraction of values from a cfit object

253 visualizaciones (últimos 30 días)
Antonio
Antonio el 3 de Abr. de 2013
Comentada: David Ebert el 8 de Feb. de 2023
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 comentarios
John D'Errico
John D'Errico el 27 de Jul. de 2020
@pinar: read my answer.
David Ebert
David Ebert el 8 de Feb. de 2023
@Yougu Yes! Perfect! Thank you! :)

Iniciar sesión para comentar.

Respuestas (2)

John D'Errico
John D'Errico el 27 de Jul. de 2020
Editada: John D'Errico el 27 de Jul. de 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.
  1 comentario
Davide Dal Bosco
Davide Dal Bosco el 15 de Feb. de 2022
Editada: Davide Dal Bosco el 15 de Feb. de 2022
The amount of passive agressiveness in this answer is off the charts

Iniciar sesión para comentar.


Igor Varfolomeev
Igor Varfolomeev el 25 de Nov. de 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 :).

Categorías

Más información sobre Fit Postprocessing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by