How to get coefficient non linear fit?

19 次查看(过去 30 天)
Hello I am using the following method to get the equation (y) to non linear model:
ft = fittype( 'a*exp(-b*x)+c', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
% Fit model to data.
[fitresult, gof] = fit( ll', meanperiodos(:,1), ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, ll, meanperiodos(:,1) );
But I would like to get the coeeficients values to the equation and the coefficient interval and Rsquared, and I don´t know how to get it. Could any one help me? To be honest, I did it in the curve fitting tool and after that I generate the code, but I don´t know how to get the coefficientes.

回答(2 个)

John D'Errico
John D'Errico 2021-11-14
编辑:John D'Errico 2021-11-14
% Make up some random data since you gave us none
x = rand(50,1);
y = 0.3*exp(-2.5*x) + 1 + randn(size(x))/50;
plot(x,y,'.')
The fit
ft = fittype( 'a*exp(-b*x)+c', 'independent', 'x' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares','start',[1 1 1]);
% Fit model to data.
% I should give more intelligent starting values here, but they
% are good enough for this simple problem
[fitresult, gof] = fit(x,y, ft, opts);
Now, look at what fit returns.
fitresult
fitresult =
General model: fitresult(x) = a*exp(-b*x)+c Coefficients (with 95% confidence bounds): a = 0.3052 (0.2746, 0.3358) b = 2.315 (1.686, 2.944) c = 0.9905 (0.9544, 1.027)
gof
gof = struct with fields:
sse: 0.0155 rsquare: 0.9505 dfe: 47 adjrsquare: 0.9483 rmse: 0.0181
Can we extract what you want now? Of course.
fitresult.a
ans = 0.3052
fitresult.b
ans = 2.3151
fitresult.c
ans = 0.9905
gof.rsquare
ans = 0.9505
How would you get things like confidence intervals on the parameters? When you don't know how to interact with an object, try this:
methods(fitresult)
Methods for class cfit: argnames cfit coeffvalues dependnames feval formula integrate numargs plot probnames setoptions category coeffnames confint differentiate fitoptions indepnames islinear numcoeffs predint probvalues type
Do you see anything that might be useful? How about confint?
confint(fitresult)
ans = 2×3
0.2746 1.6862 0.9544 0.3358 2.9440 1.0266
  5 个评论
Fabian Moreno
Fabian Moreno 2021-11-16
Thank you for your answers John D'Errico. Yes now I understand better, actually the real model that I would like to use is this one --> ΔC= a* e^(−1/2*b*x) + Ca. But I took away the value of (1/2) because I really don't know why the autors use. So I use the other model because it was the one that most resembled. Any way, thank you a lot.
Fabian Moreno
Fabian Moreno 2021-11-17
Hello John D'Errico, I have one more question. I already did the model, but I think that the fit is not good. I looked up in the MATLAB questions and I saw that I have to do something to initialized values. Do you have any idea about how to do that. I don't know exactly is the result is good or wrong. Here is my data.
x = 1:1:7;
y = [0.136789337201433 0.124938236792317 0.123046574055485 0.119853128755638 0.120078147979848 0.119958893800178 0.117245675006059] ;
and the equation with the plot are the follow:
y = 0.04334*exp(-0.896*x) +0.1189

请先登录,再进行评论。


Image Analyst
Image Analyst 2021-11-17
For what it's worth, attached is my demo of fitting an exponential decay to a noisy signal. Adapt as needed. Also attaching a demo for exponential growth.
  7 个评论
Image Analyst
Image Analyst 2021-11-17
Well yes of course -- the model is coefficients of the equation that best fits your training data.
I was just wondering because you said to John that the fit is not good. But it looks like you changed your mind and now say that the predicted values seem reasonable. Of course more training data could get you a better model.
My demo uses fitnlm() while John's uses fit(). It could be that one is just a wrapper for the other (meaning the one function calls the other internally).
Fabian Moreno
Fabian Moreno 2021-11-17
thanks @Image Analyst for your explanation. I change my mind after you made me realized about the predicted value, and obviously the result fitted good. Only I wasn't pretty sura about the result, because it is my first time doing it. But any way, thank you for your help, and also thank to @John D'Errico.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by