- DSP log - http://www.dsplog.com -

Straight line fit using least squares estimate

Posted By Krishna Sankar On July 15, 2007 @ 2:22 am In DSP | 7 Comments

Two points suffice for drawing a straight line. However we may be presented with a set of data points (more than two?) presumably forming a straight line. How can one use the available set of data points to draw a straight line?

A probable approach is to draw a straight line which hopefully minimizes the error between the observed data points and estimated straight line.

where is the observed data points and is the points from estimated straight line.

To draw the estimated straight line , we need to estimate the slope, and the constant, .

Formulating as a matrix,

,

where,

= is the set of observations is a matrix of dimension ,

= is the set of coefficients is a matrix of dimension ,

is the slope and constant estimate of dimension ,

= is the noise is a matrix of dimension .

The least square estimate of the straight line is,

.

A simple MATLAB code for least squares straight line fit is given below:

% Least Squares Estimate
rand(‘state’,100); % initializing the random number generation
y = [5:3:50]; % observations, y_i
y = y + 5*rand(size(y)); % y_i with noise added
x = 1:length(y); % the x co-ordinates

% Formulating in matrix for solving for least squares estimate
Y = y.’;
X = [x.' ones(1,length(x)).'];
alpha = inv(X’*X)*X’*Y; % solving for m and c

% constructing the straight line using the estimated slope and constant
yEst = alpha(1)*x + alpha(2);

close all
figure
plot(x,y,’r.’)
hold on
plot(x,yEst,’b')
legend(‘observations’, ‘estimated straight line’)
grid on
ylabel(‘observations’)
xlabel(‘x axis’)
title(‘least squares straight line fit’)

least_squares_fit [1]

References:

[1] Details on Curve fitting toolbox from MathWorks(TM) website [2]


Article printed from DSP log: http://www.dsplog.com

URL to article: http://www.dsplog.com/2007/07/15/straight-line-fit-using-least-squares-estimate/

URLs in this post:

[1] Image: http://www.flickr.com/photos/8649381@N03/809038180/

[2] Details on Curve fitting toolbox from MathWorks(TM) website: http://www.mathworks.com/access/helpdesk_r13/help/toolbox/curvefit/ch_fitt5.html

[3] click here to SUBSCRIBE : http://www.feedburner.com/fb/a/emailverifySubmit?feedId=1348583&loc=en_US

Copyright © 2007-2012 dspLog.com. All rights reserved. This article may not be reused in any fashion without written permission from http://www.dspLog.com.