MATLAB: Polyfit with time vector.

MATLAB

Hi, Im encountered some problems while detrending some data. I have data for example:
15:02:13 0.24
15:02:14 0.25
15:02:16 0.27
and I am trying to find the linear fit to this data to remove the drift.
But when using polyfit and then polyval I get NaN values in polyval and Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT.
My code:
%[x y] = ginput(5) % get points with a click 5 points
dxp = [ 7.3633
7.3633
7.3633
7.3633
7.3633]*1.0e+05;
dyp =[0.1380
0.1380
0.1344
0.1344
0.1292];
degree = 1; % to be adjusted
[pol, S, mu] = polyfit(dxp, dyp, degree);
xp = polyval(pol, t1, [], mu);
dI1 = I1 - xp; % detrended phase
The time vector is the the form above with 10^5 something. I think this is causing the problem. The question is how I can find the slope of the curve to remove the drift .. and plot the detrended curve? I don't know how to solve this. Any suggestions?

Best Answer

You think you have 5 points.
In reality, you have ONE point, with 5 replicates.
dxp = [ 7.3633
7.3633
7.3633
7.3633
7.3633]*1.0e+05;
ONE point. One single value for x.
How many points determine a line? TWO points, at least. With only one point, no line is possible. EVER. PERIOD.
Yes, I know, but datenum gave you those numbers!!!!
datenum('15:02:13 0.2')
ans =
7.3633e+05
datenum('15:02:14 0.25')
ans =
7.3633e+05
Did it give you those numbers? REALLY?
format long g
datenum('15:02:13 0.2')
ans =
736330.626539352
datenum('15:02:14 0.25')
ans =
736330.626550926
They ARE different. You just chose to supply the wrong numbers when you used polyfit.
You needed to save the EXACT results of datenum in a vector.
By the way, if you tried to use a higher order polyfit than 1 (linear), polyfit will start to fail here again. But that is a completely different story.
Garbage in, garbage out.