Reputation: 67
How would I go about finding a linear trend from a time series?
I know the time series is y = {y1,...yn}
or for the first case {1.000000000000000 0.917457418407746 0.683456229182811}
. It's assumed that each element yi
was sampled at time ti = i
.
The polynomial it needs to be fitted to is of degree 1.
What I've attempted is below.
function [ linCoeffs ] = getLinearTrend(y)
y = [1.000000000000000,0.917457418407746,0.683456229182811];
x = ;
linCoeffs = polyfit(y,x,1);
end
It's not much as I'm stumped on what to do for the x value. Normally y would be an input value but for testing I just left it in.
Upvotes: 1
Views: 1764
Reputation: 4396
Looks like you've provided the arguments to polyfit
in the wrong order. The first argument takes the values of the x axis which, in this case, correspond to the sampling times given by i. The second argument takes the y values. So you'd want something like:
function linCoeffs = getLinearTrend(y)
linCoeffs = polyfit(1:size(y,2), y, 1);
end
which, given your example y, results in:
y = [1.000000000000000,0.917457418407746,0.683456229182811];
getLinearTrend(y)
ans =
-0.15827 1.18351
Upvotes: 2