How to get coefficient non linear fit?

8 views (last 30 days)
Fabian Moreno
Fabian Moreno on 14 Nov 2021
Commented: Fabian Moreno on 17 Nov 2021
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.

Answers (2)

John D'Errico
John D'Errico on 14 Nov 2021
Edited: John D'Errico on 14 Nov 2021
% 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;
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 =
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 = 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.
ans = 0.3052
ans = 2.3151
ans = 0.9905
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 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?
ans = 2×3
0.2746 1.6862 0.9544 0.3358 2.9440 1.0266
Fabian Moreno
Fabian Moreno on 17 Nov 2021
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

Sign in to comment.

Image Analyst
Image Analyst on 17 Nov 2021
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.
Fabian Moreno
Fabian Moreno on 17 Nov 2021
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by