How to get coefficient non linear fit?
Show older comments
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
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;
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 Comments
Fabian Moreno
on 14 Nov 2021
Fabian Moreno
on 14 Nov 2021
John D'Errico
on 14 Nov 2021
Edited: John D'Errico
on 14 Nov 2021
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)
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')
So H is an array that contains two handles.
H(1)
We can set String as we wish.
H(1).String = {'My own very special data', 'Linear fit'};
Fabian Moreno
on 16 Nov 2021
Fabian Moreno
on 17 Nov 2021
Image Analyst
on 17 Nov 2021
0 votes
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
Fabian Moreno
on 17 Nov 2021
Image Analyst
on 17 Nov 2021
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?
Fabian Moreno
on 17 Nov 2021
Image Analyst
on 17 Nov 2021
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?
Fabian Moreno
on 17 Nov 2021
Image Analyst
on 17 Nov 2021
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
on 17 Nov 2021
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

