How can I automatically save the coefficients of fit functions?
23 views (last 30 days)
Show older comments
Dong-Gyu Jang
on 4 Apr 2011
Commented: Walter Roberson
on 9 Mar 2021
Hello.
Since I'm a graduate student under phsics laboratory, I have used matlab to fit the experimental datas with the appropriate functions so many times.
Everytime I fit the data with the matlab, It is very inconvenient to save the coefficients of the fit function(eg : A and B of A*x + b*x^2 fit function) by 'copy and paste' way. The situation becames worse when the number of datas to be analyzed is about several hundreds!
I frequently use the commands about fit - fit, fittype, fitoptions, etc. The matlab just displays the results similar to the masseage below
fit_options = fitoptions('Method','NonlinearLeastSquares','Lower',[0,0,0,0,5600,0,6500,10],'Upper',[1.50e+04,100,1.50e+04,100,5800,5.00e+04,6600,300],'Startpoint',[0,0,0,0,5600,0,6500,10]);
fun = fittype('I * ( width^2 / ( 4*(x - center)^2 + width^2 ) ) + A - B * x + C*exp(-D*(x-E))','options',fit_options); fit_func = fit(x_alpha,y_alpha,fun)
fit_func =
General model:
fit_func(x) = I * ( width^2 / ( 4*(x - center)^2 + width^2 ) ) + A - B *
x + C*exp(-D*(x-E))
Coefficients (with 95% confidence bounds):
A = 116.2 (-1.703e+004, 1.726e+004)
B = 0.0002194 (-1.468, 1.469)
C = 633.8 (-4.822e+008, 4.822e+008)
D = 0.00039 (-0.002925, 0.003706)
E = 5603 (-1.951e+009, 1.951e+009)
I = 4138 (4109, 4166)
center = 6554 (6554, 6555)
width = 89.73 (88.69, 90.77)
I really need to save coefficient of 'width', for example, in automatic way, not several times of clicking!
Please help to break through the situation.
Thank you for reading my question.
0 Comments
Accepted Answer
Arnaud Miege
on 4 Apr 2011
How about:
fit_func.width
Does that answer your question?
HTH,
Arnaud
0 Comments
More Answers (1)
Isabelle Brichetto
on 9 Mar 2021
Use the function
vector= coeffvalues(fittedmodel)
for collecting them into a vector. The function's instruction here https://it.mathworks.com/help/curvefit/cfit.coeffvalues.html;jsessionid=6259a965c461b0a3b0a6c96b1b35
Isabelle
1 Comment
Walter Roberson
on 9 Mar 2021
Note that if you do this, the order of coefficients might not be what you are expecting. You can ask for the coefficient names, and the values will be in the same order, but the coefficient names will be presented as a cell array of character vectors, so to ask about a particular coefficient you would need to search the cell array to find the index. Or do something like sort() the cell array of names and use the result to index the coefficients to put them into the same order, after which you would be able to count on the relative order (until, that is, you change the names of coefficients.)
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!