Curve-fitting part of a data set

5 views (last 30 days)
Jack
Jack on 27 May 2013
Hello. I have an array of data which 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.25. 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 is confined to the area roughly between 1.7 and 2.3). Here is my attempt.
%'time' vector ranges from 1.5 to 2.5
p = polyfit(time, log(Temp), 1);
%Arbitrary starting point
t0 = 1.71;
tau = -1./p(1)
Temp0 = exp(p(2))
tp = 1.7:0.01:2.3;
Temp_t = Temp0*exp(-(tm-t0)/tau);
plot(time, Temp, tp, Temp_t)
My curve ends up looking like this http://s7.postimg.org/igces77pn/temp2.jpg What am I doing incorrectly? How can I align the curve-fit line and the data points. I am told that circshift may help, but I couldn't grasp the function of the command after reading the help file. Any help would be greatly appreciated. Thank you!

Answers (1)

David Sanchez
David Sanchez on 27 May 2013
If I am not wrong, you are doing this:
% tau = -1./p(1)
% Temp0 = exp(p(2))
% Temp_t = Temp0*exp(-(tm-t0)/tau); % ->
% Temp_t = exp(p(2))*exp((tm-t0)*p(1)); % ->
Temp_t = exp( p(2)+(tm-t0)*p(1) );
Instead, I think you should just grab your Temp amd time data in your region of interest (1.7 to 2.3) taking care on grabbing an Temp/time array with the same length than your tp array. Then:
p = polyfit(time, Temp, 1);
Temp_p = p(1)*tp + p(2);
plot(time, Temp, tp, Temp_t)

Categories

Find more on Structures 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!