how to fit linear trend in time series

i have this time series liner fitted figure but i plotted this figure with the help of matlab tool box((basic fitting)(liner fit)) but i want to do same thing from matlab code.thank you in advance.

 Accepted Answer

Star Strider
Star Strider on 26 Apr 2014
Edited: Star Strider on 27 Apr 2014
See if this does what you want:
% Create Data
yrs = linspace(1,100,100);
rainfall = 0.3*yrs + 350*rand(1,100);
% Fit Data
b = polyfit(yrs,rainfall, 1);
fr = polyval(b, yrs);
% Plot Data & Fit
figure(1)
plot(yrs, rainfall, '-b')
hold on
plot(yrs, fr, '-r')
hold off
h1 = legend('Data', 'Linear', 'Location','NE')
set(h1, 'FontSize',8)
[xlim ylim]
textposx = diff(xlim)*0.50+min(xlim);
textposy = diff(ylim)*0.95+min(ylim);
text(textposx,textposy, sprintf('y = %.2f*x %c %0.2f', b(1), char(45-(sign(b(1))+1)), abs(b(2))), 'FontSize',8)
xlabel('Years')
ylabel('Rainfall (mm)')
EDIT — Added textposx and textposy.

6 Comments

sir when i change something in code it mean when i inter my data in this code then equation does not show in the figure.sir my data size is rainfall_count(1x95).
% Create Data
%yrs = linspace(1,100,100);
yrs = linspace(1,95,95);
%rainfall = 0.3*yrs + 350*rand(1,100);
rainfall = rainfall_count;
% Fit Data
b = polyfit(yrs,rainfall, 1);
fr = polyval(b, yrs);
% Plot Data & Fit
figure(1)
plot(yrs, rainfall, '-b')
hold on
plot(yrs, fr, '-r')
hold off
h1 = legend('Data', 'Linear', 'Location','NE');
set(h1, 'FontSize',8)
text(50,350, sprintf('y = %.2f*x %c %0.2f', b(1), char(45-(sign(b(1))+1)),
abs(b(2))), 'FontSize',8)
xlabel('Years')
ylabel('Rainfall (mm)')
I edited the code in my original Answer and added an adaptive position for the equation position. It should work without problems now.
devendra
devendra on 27 Apr 2014
Edited: devendra on 30 Apr 2014
Thank u so much sir this is working perfectly but little bit change in answer
y=0.04*x+10.80 wh
y=0.045*x+11 when i use matlab tool
why little bit difference in answer ?
My pleasure!
The MATLAB fitting function uses a different format in the presentation of the equation in the plot than I used.
The data are all available in full precision in my code. The slope is b(1) and the intercept is b(2).
@Star Strider I have plot my timeseries plot through this method
myfile=load('time.txt');
xx = myfile(:,2);
dataa=myfile(:,1);
x=xx.';
ts1 = timeseries(x,1:length(x));
ts1.Name = 'Daily Count';
ts1.TimeInfo.Units = 'days';
ts1.TimeInfo.StartDate = '01-Jan-2003'; % Set start date.
ts1.TimeInfo.Format = 'mmm dd, yy'; % Set format for display on x-axis.
ts1.Time = ts1.Time - ts1.Time(1); % Express time relative to the start date.
plot(ts1)
How can i use ploy fit against the time and ts1.Data? My data is a vector but date is in date format. Can you tell me how?
You need to convert the dates and times to date numbers using the datenum function. Use polyfit with 3 outputs so that it will do centring and scaling. Be sure to include all the outputs in your polyval call as well. Use the datetick function to plot the x-axis as the dates and times you want.

Sign in to comment.

More Answers (1)

p=polyfit(y,x);
doc polyfit % for more details
doc polyval % to evaluate

Categories

Find more on Descriptive Statistics and Insights 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!