Exponential fitting of data not working
4 views (last 30 days)
Show older comments
I have a vector of 1666 values with an exponential curve. Since this vector is the result of a experimental measurement I don't have the precise law that describes it but I need its horizontal asymptotic value. To get it I thought about an exponential fitting following the model a+b*exp(-c*t) but all the methods I tried result in either an error or a straight line. What am I missing?
I've attached a .mat file containing the T_A vector (temperature measurements). t_A vector is time and can be assumed as:
t_A=linspace(0,83.3,1666);
Edit: I included another vector T_B, with which fit or other methods don't work. You can assume t_B is defined the same way as t_A.
Things I've tried:
g = fittype('a-b*exp(-c*x)'); % a is asymptotic value
fA = fit(t_A,T_A,g,'StartPoint',[[ones(size(t_A)), -exp(-t_A)]\T_A; 1]);
Gives back "Inf computed by model function, fitting cannot continue.". I also tried to change the upper and lower bounds like it suggests but no luck. Changed starting points as well, no luck.
f = @(A,t_A) A(1)+A(2).*exp(-t_A.*(A(3)));
beta = lsqcurvefit(f,[[ones(size(t_A)), -exp(-t_A)]\T_A; 1],t_A,T_A);
Gives back "Local minimum possible" but the plot looks like a spike in 0 and then goes back to 0 and stays there
f = @(A,t_A) A(1)+A(2).*exp(-t_A.*(A(3)));
A_s = fminsearch(@(A) norm(T_A - f(A,t)), [-exp(-t_A); 1; ones(size(t_A)); 1]);
Runs infinitely, doesn't stop.
I also tried curveFitter but gives out the same results as fit (or an ugly exponential curve when it works), but it's not an optimal solution since I would have to do this numerous timesn and I'd like an automated approach.
I've never done curve fitting and I'm trying now since I need it. Would love a solution but also some insight as to why it doesn't work or how I can improve. Thank you very much to all of you.
0 Comments
Answers (2)
Cris LaPierre
on 9 Aug 2023
Edited: Torsten
on 9 Aug 2023
For attempts 2 and 3, what is A?
First one seems to work.
load T_A.mat
t_A=linspace(0,83.3,1666)';
g = fittype('a-b*exp(-c*x)'); % a is asymptotic value
fA = fit(t_A,T_A,g,'StartPoint',[[ones(size(t_A)), -exp(-t_A)]\T_A; 1])
plot(fA,t_A,T_A)
2 Comments
Cris LaPierre
on 9 Aug 2023
What happens when you try it? What does 'doesn't work' mean?
load T_B.mat
t_A=linspace(0,83.3,1666)';
g = fittype('a-b*exp(-c*x)'); % a is asymptotic value
fA = fit(t_A,T_B,g,'StartPoint',[[ones(size(t_A)), -exp(-t_A)]\T_B; 1])
plot(fA,t_A,T_B)
Torsten
on 9 Aug 2023
Edited: Torsten
on 9 Aug 2023
A = load("T_A.mat");
A = A.T_A;
A = [linspace(0,83.3,1666).',A];
plot(A(:,1),A(:,2),'o')
hold on
p0 = [25 1 1];
f = @(p)p(1)+p(2)*exp(-p(3)*A(:,1));
F = @(p)f(p)-A(:,2);
p = lsqnonlin(F,p0,[],[],optimset('MaxIter',1000000,'MaxFunEvals',1000000))
norm(F(p))
plot(A(:,1),f(p))
plot([0 100],[p(1),p(1)],"k")
hold off
2 Comments
Torsten
on 9 Aug 2023
Can you explain how your code works? Especially why you put the T_A vector inside another vector with the t_A vector. Couldn't I use them separately?
Yes, you could.
Also, I'm assuming that the asymptotic value is p(1), correct? How can I plot the obtained function to visualize the asymptote?
Done.
See Also
Categories
Find more on Fit Postprocessing 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!

