How to get coefficient non linear fit?

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)

% 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 Comments

Thank John D'Errico, it was very usefull! I have one more question, I already know what is the meaning of 'a' and 'b' coefficent, and also I can see tha the coefficient 'c' is the less value of the adjust line curve. But I don't know exactly what is 'c' meaning. I looked it up here (http://cda.psych.uiuc.edu/matlab_pdf/curvefit.pdf) but it just explain that it is an unkown value. I also read that coefficient 'c' could be the asymptotic value of the exponencial decay. So Do you know or may you explain me what it is? Thanks in advanced.
Hello John D'Errico, Do you know how to change the name of the lines. I use 'Displayname', but was impossible.
plot( fitresult, buf', meanano(:,1),'-k','DisplayName',{'name1','name2'});
Two questions here:
First, in the model a*exp(-b*t) + c, you say that you don't know the "meaning" of c?
Perhaps a better way is to understand what property of the shape of the fitted model is described by c? For example, b could be described as a rate parameter, that describes the speed of the exponential decay. Or you could use b to tell how long it takes for the exponential decay to drop by 50%, sort of a half life. The units on b in that context are 1/time. And therefore you can express the half-life of the exponential model as a function of b. For example...
syms a b c t
halflife = solve(exp(-b*t) == 1/2,t)
halflife = 
So the effective time it takes that exponential to decay to 50% of its value at any time is log(2)/b.
Then what does c mean in context of that exponential model?
What happens to that model at a long time out? Can you infer the limit of the expression
a*exp(-b*t) + c
as t --> inf? As long as b has a positive value, the exponential term decays to zero. So what remains is c. You can think of c as the horizontal asymptote for that expression, as t --> inf.
Next, you want to change the names that appear in the legend box? I'm not sure why you would just make up the name of a property and hope it would work. Every once in a while, you will get lucky, but not often. For example:
x = rand(10,1);
y = rand(10,1);
mdl = fit(x,y,'poly1');
By default, the legend will show two names, "data" and "Fitted curve"
plot(mdl,x,y)
But we can set that to anything we wish. For example
H = get(gcf,'children')
H =
2×1 graphics array: Legend (data, fitted curve) Axes
So H is an array that contains two handles.
H(1)
ans =
Legend (data, fitted curve) with properties: String: {'data' 'fitted curve'} Location: 'northeast' Orientation: 'vertical' FontSize: 9 Position: [0.6893 0.8118 0.1963 0.0889] Units: 'normalized' Show all properties
We can set String as we wish.
H(1).String = {'My own very special data', 'Linear fit'};
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.
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.

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 Comments

Hello Image Analyst, yes, I already checked it in this link: https://la.mathworks.com/matlabcentral/answers/473517-solved-fitting-exponential-decay-function, and download the demo. But to be honest, I didn't understand how to adapt it to my data. I'm trying
It should be easy, but should give you something like John's solution, though I think mine has an additional parameter (a y offset). Why do you say John's solution is not a good fit?
Not, I am not saying that the Jhon's anwer is wrong. I am asking about my result. The problem is that it is my first time doing an exponential model decay. So I don't know if my result is good or wrong. Because I though that the coefficient (a) shoud intercept the Y axis, but my coefficient (a) is equal to 0.04334, and the curve fit intercept in the Y axis is higher than 0.13. Then I am not sure about what is the meaning of each coeeficient. I understood that a = Y axis intercept, b = decay rate and c = asymptotic value of the exponential trend.
I guess the question one asks ones self is does the model predict output values, for input values that are NOT in the training set, with reasonably believability? Do they seem reasonable? How would you answer that?
Image Analyst, yes, the model predict reasonable output values. I replaced the (x) variable in the equation and the results are rasonable. Thanks! But at the end, the coefficient (a) is just a fit value to the equation. Thank you!
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).
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.

Categories

Asked:

on 14 Nov 2021

Commented:

on 17 Nov 2021

Community Treasure Hunt

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

Start Hunting!