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.
0 件のコメント
回答 (2 件)
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
gof
Can we extract what you want now? Of course.
fitresult.a
fitresult.b
fitresult.c
gof.rsquare
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)
Do you see anything that might be useful? How about confint?
confint(fitresult)
5 件のコメント
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
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).
参考
カテゴリ
Help Center および File Exchange で Linear and Nonlinear Regression についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!