Clear Filters
Clear Filters

Determine best-fitting exponential

3 views (last 30 days)
Jack
Jack on 27 May 2013
I have an array of data which, when plotted, looks like this. http://s12.postimg.org/7ja47a6b1/temp.jpg
I need to use the polyfit command to determine the best fitting exponential for the time roughly between 1.7 and 2.3. I must also compare this exponential fit to a simple linear fit. I'm given the equation Temp(t) = Temp0 * exp(-(t-t0)/tau), where t0 is the time corresponding to temperature Temp0 (I can select where to begin my curve-fitting, but it must be confined to the area roughly between 1.7 and 2.3). Here is my attempt.
% Arbitrarily defined starting point
t0 = 1.71;
%Exponential fit
p = polyfit(time, log(Temp), 1)
tau = -1./p(1)
Temp0 = exp(p(2))
tm = 1.8:0.01:2.3;
Temp_t = Temp0*exp(-(tm)/tau);
plot(time, Temp, tm, Temp_t)
figure(2)
%Linear fit
p2 = polyfit(time, Temp, 1);
Temp_p = p2(1)*tm + p2(2);
plot(time, Temp, tm, Temp_p)
My exponential fit ends up looking like this http://s23.postimg.org/4kibibouj/exp.jpg My linear fit looks like this http://s24.postimg.org/f3awxyw0l/lin.jpg (virtually identical). Here is an overlap http://s22.postimg.org/cmx5ke5j5/overlap.jpg What am I doing incorrectly? Should the two fits be so similar? I am told that circshift may help, but I couldn't grasp the applicability of the command after reading the help file. Any help would be greatly appreciated. Thank you!
  4 Comments
Matt J
Matt J on 27 May 2013
Edited: Matt J on 27 May 2013
You should probably continue the discussion in that earlier thread. Just in passing, I'll mention that I don't see any obvious reason to distrust the fit. The exponential fit does lie above some data points, but below others. If you expected something more convex-looking, maybe you should try restricting the fit to t>=1.9. You could also try double-checking against a fit generated with FMINSEARCH, or better yet with FMINSPLEAS.
Jack
Jack on 27 May 2013
I see. Thank you very much for your help, Matt J.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 27 May 2013
What is time? Is that (t-t0)? You didn't define it. If so, you must put that back in the equation:
Temp_t = Temp0*exp(-(tm)/tau);
you can't use tm - you must subtract off t0 from it: tm-t0, or use "time".
Temp_t = Temp0*exp(-(tm-t0)/tau);
Did you upload your data anywhere so we can try some things?
  2 Comments
Image Analyst
Image Analyst on 27 May 2013
Edited: Image Analyst on 27 May 2013
By the way, here's the exponential fitting code from the help "Curve Fitting via Optimization":
function [estimates, model] = fitcurvedemo(xdata, ydata)
% Call fminsearch with a random starting point.
start_point = rand(1, 2);
model = @expfun;
estimates = fminsearch(model, start_point);
% expfun accepts curve parameters as inputs, and outputs sse,
% the sum of squares error for A*exp(-lambda*xdata)-ydata,
% and the FittedCurve. FMINSEARCH only needs sse, but we want
% to plot the FittedCurve at the end.
function [sse, FittedCurve] = expfun(params)
A = params(1);
lambda = params(2);
FittedCurve = A .* exp(-lambda * xdata);
ErrorVector = FittedCurve - ydata;
sse = sum(ErrorVector .^ 2);
end
end
Jack
Jack on 27 May 2013
Edited: Jack on 27 May 2013
I had attempted to use Temp_t = Temp0*exp(-(tm-t0)/tau), but the curve-fit ends up being far above the data points. Time and Temp each have 5,185 cells of data. Here is an excel file with the 2 columns of data: http://filebin.ca/ia7Gh9kHxod/data.xls

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!