%% Simple Linear Regression
% Fit a simple linear regression model to a set of discrete 2-D data points.
%
% Create a few vectors of sample data points (x,y). Fit a first degree polynomial to the data.

x = 1:50; 
y = -0.3*x + 2*randn(1,50); 
p = polyfit(x,y,1); 

%
% Evaluate the fitted polynomial p at the points in x. Plot the resulting linear regression model with the data.

f = polyval(p,x); 
plot(x,y,'o',x,f,'-') 
legend('data','linear fit') 

% Copyright 2012 The MathWorks, Inc.