# Fit an exponential function to time-series data

20 views (last 30 days)
Deepa Maheshvare on 13 Apr 2021
Commented: Mathieu NOE on 13 Apr 2021
Hi All,
I have a time series data and I would like to fit an exponential curve using the following expression to the data points and estimate the relaxation time B The dataset can be found here.
I saw some examples of curve fitting here https://in.mathworks.com/help/matlab/math/example-curve-fitting-via-optimization.html . However it is not clear to me how to fit the above function and estimate B. Could someone please help me with this? I would like to find time scale at which C reaches 100 % of C0, C0 = 7.5.
Side note, I've the optimization toolbox and curve fitting tool box installed.

Mathieu NOE on 13 Apr 2021
hello
this is the poor man solution without any toolbox
data was stored in txt file
your B = -1/b_sol from my code below
a_sol = 8.3470
b_sol = -0.7241
c_sol = 0.3345
d_sol = -0.8432 numericData = importdata('data.txt');
start = 25; % use data only starting at this index
x = numericData(start:end,1);
y2fit = numericData(start:end,2);
% exponential fit method
% code is giving good results with template equation : % y = a.*(1-exp(b.*(x-c))) + d;
f = @(a,b,c,d,x) a.*(1-exp(b.*(x-c))) + d;
obj_fun = @(params) norm(f(params(1), params(2), params(3), params(4),x)-y2fit);
sol = fminsearch(obj_fun, [y2fit(end)-y2fit(1),0,0,y2fit(1)]);
a_sol = sol(1)
b_sol = sol(2)
c_sol = sol(3)
d_sol = sol(4)
y_fit = f(a_sol, b_sol,c_sol ,d_sol, x);
figure
plot(x,y2fit,'r',x,y_fit,'-.k');
legend('data','exp fit');
Mathieu NOE on 13 Apr 2021
hello again
1/ you see that my fit function has not exactly the same form as yours
I used x as the time variable and the "b" are used differently :
my function : y = a.*(1-exp(b.*(x-c))) + d ;
your function : = a.*(1-exp(-x/B)) ;
we don't care about c and d here , and it's quite obvious now that : B (your formula) = -1/b (mine, and solution in the code once the fit is done , is b_sol);
2/ B is a decay rate - see definition here : Exponential decay - Wikipedia
a very often used relationship : to reach 95% of the asymptote, it takes t = 3 x decay rate = 3 * 1.38 = 4.14 s
theoretically, you never reach 100% (the asymptote) , you can only get very close

R2019b

### Community Treasure Hunt

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

Start Hunting!