How do I fit an exponential curve to my data?
Afficher commentaires plus anciens
Say, I have the following data: x=[1,2,4,6,8],y=[100,140,160,170,175]. How do I fit an exponential curve of the form y=a-b*exp(-c*x) to my data? Is there any Matlab function to do that? Thanks in advance.
2 commentaires
Arturo Gonzalez
le 1 Sep 2020
Modifié(e) : Arturo Gonzalez
le 1 Sep 2020
clear all;
clc;
% get data
dx = 0.02;
x = (dx:dx:1.5)';
y = -1 + 5*exp(0.5*x) + 4*exp(-3*x) + 2*exp(-2*x);
% calculate integrals
iy1 = cumtrapz(x, y);
iy2 = cumtrapz(x, iy1);
iy3 = cumtrapz(x, iy2);
% get exponentials lambdas
Y = [iy1, iy2, iy3, x.^3, x.^2, x, ones(size(x))];
A = pinv(Y)*y;
lambdas = eig([A(1), A(2), A(3); 1, 0, 0; 0, 1, 0]);
lambdas
%lambdas =
% -2.9991
% -1.9997
% 0.5000
% get exponentials multipliers
X = [ones(size(x)), exp(lambdas(1)*x), exp(lambdas(2)*x), exp(lambdas(3)*x)];
P = pinv(X)*y;
P
%P =
% -0.9996
% 4.0043
% 1.9955
% 4.9999
Nice solution!
I implemented a generalized function to handle n exponential functions.
function [lambdas, c, yhat] = ExpFunFit(Data, n, intcpt)
%Fits an exponential summation function to Data = [t, y].
% y = c0 + Sum{c_i * exp(lambda_i * t)}; i = 1, ..., n.
% H.Sh.G. - 2022
% See Prony's method for exponential function fitting.
if nargin<3, intcpt = 0; end
x = Data(:,1);
nx = size(x, 1);
y = Data(:,2);
% Calculate integrals
yi = [y, zeros(nx, n)];
xi = [zeros(nx, n-1), x];
for i = 2:n+1
yi(:,i) = cumtrapz(x, yi(:,i-1));
if i<=n
xi(:,i-1) = x.^(n+2-i);
end
end
% Get exponentials' lambdas
Y = [yi(:,2:n+1), xi];
if intcpt, Y = [Y, ones(size(x))]; end
A = pinv(Y)*y;
lambdas = eig([A(1:n)'; ...
eye(n-1), zeros(n-1,1)]);
% Get exponentials' multipliers
X = ones(nx, n+1);
for i = 1:n
X (:, i+1) = exp(lambdas(i)*x);
end
if ~intcpt, X = X(:, 2:end); end
c = pinv(X)*y;
yhat = X * c;
Hamed.
Réponse acceptée
Plus de réponses (2)
Image Analyst
le 21 Nov 2018
3 votes
You could also use fitnlm (Fit Non Linear Model):

Tamara Schapitz
le 20 Nov 2018
I tried it with the form
'a*exp(-((x/b)^c))'
I got this warning messsage:
Warning: Rank deficient, rank = 1, tol = 4.019437e-14.
and it only plots the data, but not the fit... What am I doing wrong?
9 commentaires
Torsten
le 20 Nov 2018
How many data points do you have ? I hope at least three, not only one.
Tamara Schapitz
le 20 Nov 2018
Hi,
I have about 20 data points. Is this maybe too much?
thanks for your help!
Torsten
le 21 Nov 2018
What do you get if you use the code
xdata = [...]; %row vector, should contain positive values
ydata = [...]; %row vector
lb = [-Inf,0,-Inf];
ub = [Inf,Inf,Inf];
fun=@(p,xdata) p(1)*exp(-((xdata/p(2)).^p(3)));
p0 = [1 1 1];
p = lsqcurvefit(fun,p0,xdata,ydata,lb,ub)
Best wishes
Torsten.
Tamara Schapitz
le 21 Nov 2018
Thank you Thorsten,
the error disappeared, but for
p = lsqcurvefit(fun,p0,xdata,ydata,lb,ub)
I get the answer
p =
1 1 1
Torsten
le 21 Nov 2018
Start with p0 = [1 1000 1]
Tamara Schapitz
le 21 Nov 2018
Now it worked! Thanks for your help! (y)
Tamara Schapitz
le 9 Avr 2020
Again, I have to fit exponential data and get the coefficients. I thought it should work with my old code, but apparently, I am doing something wrong, but I don't see my mistake... Excel retuns an exponential function of 150e-0.115x, so I took this as starting values for the coefficients p.
The last row returns 31.3705881793848 for all values! why?
x=[7.48673807584469;7.48673807584469;9.52211367803178;9.52211367803178;9.52211367803178;11.2093975148163;11.2093975148163;11.2093975148163;15.8637542852664;15.8637542852664;15.8637542852664;17.5649842553087;17.5649842553087;17.5649842553087;26.3442681704923;26.3442681704923;26.3442681704923];
y=[61.8;78.6;63.1;53.8;52.5;31.4;20.8;27.2;28.4;17.2;25.2;16.8;9.5;12.7;13.6;9.2;11.5];
g = fittype('a-b*exp(-c*x)');
f0 = fit(x,y,g,'StartPoint',[[ones(size(x)), -exp(-x)]\y; 1]);
xx = linspace(8,30,50);
plot(x,y,'*',xx,f0(xx),'r-'); %fit looks good
hold on %because I want to plot the data with the determined coefficients to check
lb = [-Inf,0,-Inf];
ub = [Inf,Inf,Inf];
fun=@(p,x) p(1)-p(2)*exp(-(x*p(3)));
p0 = [0 100 0.155];
p = lsqcurvefit(fun,p0,x,y,lb,ub)
y1=p(1)-p(2)*exp(-(x*p(3))); %returns 31.3705881793848 for all values?
plot(x,y1,'g-'); %Yerks!
Image Analyst
le 9 Avr 2020
That code requires the Curve Fitting Toolbox, which I don't have, so I can't run it. But I did plot(x,y) and noticed that several of your x all have the same value. Perhaps it could be fixed by making the x all unique values by adding a very tiny amount of random noise to them (but not enough to affect the fit), like
x=[7.48673807584469;7.48673807584469;9.52211367803178;9.52211367803178;9.52211367803178;11.2093975148163;11.2093975148163;11.2093975148163;15.8637542852664;15.8637542852664;15.8637542852664;17.5649842553087;17.5649842553087;17.5649842553087;26.3442681704923;26.3442681704923;26.3442681704923];
x = x + 0.001 * rand(size(x))
If it complains that the x must be sorted in ascending order, you can sort x and y like this:
x=[7.48673807584469;7.48673807584469;9.52211367803178;9.52211367803178;9.52211367803178;11.2093975148163;11.2093975148163;11.2093975148163;15.8637542852664;15.8637542852664;15.8637542852664;17.5649842553087;17.5649842553087;17.5649842553087;26.3442681704923;26.3442681704923;26.3442681704923];
y=[61.8;78.6;63.1;53.8;52.5;31.4;20.8;27.2;28.4;17.2;25.2;16.8;9.5;12.7;13.6;9.2;11.5];
x = x + 0.001 * rand(size(x))
[x, sortOrder] = sort(x, 'ascend');
y = y(sortOrder);
Tamara Schapitz
le 9 Avr 2020
Thanks for your answer! But the similar x-values are not the problem, there is no errror message or such. It DOES fit the data (as I can see in the plot), but the coefficients that are found, are not the correct ones...
I tried it with your fitNonLinearModel.m et voilà! With some small modifications, it works! And the coefficients are totally different from the other ones...
The above code returns:
p= 31.3705881793848 97.355156245024 6.39477241747793
coefficients =
10.6643318631924
398.728521237987
0.248005408824638
But I still don't see my mistake... Very strange... also because it worked for the old case two years ago... the only difference is the form of the exponential fit
'a-b*exp(-c*x)'
instead of
'a*exp(-((x/b)^c))'
Maybe I have to change some Syntax, too... Or change the starting value, like last time? Anyway... I have a solution for my plot, but if someone finds the error, I still would like to know...
Thanks!
Catégories
En savoir plus sur Time Series Events dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
