MATLAB curve, exponential and linear

I have an array of data that looks like this when building.

http://s12.postimg.org/7ja47a6b1/temp.jpg

I need to use the polyfit command to determine the best exponential exponent in a time between approximately 1.7 and 2.3 . I also have to compare this exponential correspondence with a simple linear fit.

I have been given the equation Temp(t) = Temp0 * exp(-(t-t0)/tau) , where t0 is the time corresponding to the temperature Temp0 (I can choose where to start my curve selection, but it should be limited to an area of โ€‹โ€‹about 1.7 up to 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 looks like exponential fit . My linear fit looks like linear fit . (almost identical). What am I doing wrong? Should two seizures be similar? I was told that circshift might help, but I could not understand the applicability of the command after reading the help file.

+7
source share
4 answers

Things behave as you expect. The problem is that the function you are trying to install is not a good approximation of the data. Observing the curve, it seems that the exponential part of the curve asymptotically tends to a value of about 16; but the function that you use will ultimately tend to a temperature of 0. Thus, adapting to the part that goes from 22 to 16, you will get an almost linear connection. To illustrate this, I wrote a few lines of code that roughly coincide with your data points - and this shows how various functions (which tend to 0 and others that tend to 16) will give you a completely different curve shape. The first (your original function) is almost linear between the values โ€‹โ€‹of T 22 and 16 - so it will look like a linear fit.

I suggest you think about the โ€œrightโ€ form of a function to match โ€” what is the basic physics that makes you choose a particular form? Obtaining this right is important ...

Here is the code:

 time = linspace(1.5, 2.5, 200); t0 = 1.7; t1 = 2.3; tau = 2.0; % define three sections of the function: s1 = find(time < t0); s2 = find(time >= t0 & time < t1); s3 = find(time > 2.3); % compute a shape for the function in each section: tData(s1) = 28 - 50*(time(s1)-1.5).^2; tData(s2) = 22*exp(-(time(s2)-t0)/tau); tData(s3) = tData(s2(end)) + (s3 - s3(1))*12 / numel(s3); figure plot(time, tData) % modify the equation slightly: assume equilibrium temperature is 16 % with a bit of effort one could fit for this as a second parameter Teq = 16; tData2 = tData; tau2 = tau / 8; % decay more strongly to get down to approx the same value by t1 tData2(s2) = (22 - Teq) * exp( - (time(s2) - t0) / tau2) + Teq; tData2(s3) = tData2(s2(end)) + (s3 - s3(1))*12 / numel(s3); hold on; plot(time, tData2, 'r') 

The result is the following graph:

enter image description here

In conclusion, I conclude that the main reason your stories look so similar is that the function you are trying to pick up is almost linear over your domain of choice - the best choice of function will be better.

+4
source

As I mentioned in the comments, there is a difference between installing a linear model in the log space compared to fitting a non-linear model (as in the sense of least squares).

There is a nice demo in the Statistics toolbar that explains the situation. I am adapting the code below:

 %# sample data x = [5.72 4.22 5.72 3.59 5.04 2.66 5.02 3.11 0.13 2.26 ... 5.39 2.57 1.20 1.82 3.23 5.46 3.15 1.84 0.21 4.29 ... 4.61 0.36 3.76 1.59 1.87 3.14 2.45 5.36 3.44 3.41]'; y = [2.66 2.91 0.94 4.28 1.76 4.08 1.11 4.33 8.94 5.25 ... 0.02 3.88 6.43 4.08 4.90 1.33 3.63 5.49 7.23 0.88 ... 3.08 8.12 1.22 4.24 6.21 5.48 4.89 2.30 4.13 2.17]'; xx = linspace(min(x), max(x), 100); %# linear regression in log-space %# y = p2 * exp(p1*x) %# => log(y) = log(p2) + p1*x p_exp = polyfit(x, log(y), 1); yy1 = exp(p_exp(2)) .* exp(xx .* p_exp(1)); %# linear regression p_lin = polyfit(x, y, 1); yy2 = polyval(p_lin, xx); %# non-linear regression (using previous result as initial coeff) f = @(p,x) p(2)*exp(p(1)*x); p_nonlin = nlinfit(x, y, f, [p_exp(1) exp(p_exp(2))]); yy3 = f(p_nonlin, xx); plot(x,y,'o', xx,yy1,'-', xx,yy2,'-', xx,yy3,'-') legend({'data points','linear in log-space','linear','non-linear'}) 

regression

+4
source

If I understand correctly, the time and Temp variables that you use in polyfit contain the entire value (from 1.5 to 2.5). Thus, you can limit the time and Temp from 1.71 to 2.3 before calculating the polyphyte (right now it is calculating the polyphyte from 1.5 to 2.5, therefore, why the line is not aligned with the data points).

 p = polyfit(time, log(Temp), 1) 
+2
source

Using

 polyfit(x,y,n) 

in the Matlab curve toolbox.

0
source

All Articles