%Input:
%   x is a column vector of domain (input) variables
%   y is a column vector of range (output) variables
%
%Output:
%   m is the slope of the line
%   b is the intercept of the line

function [m,b] = ls_normal (x,y)

P = size(x,1)

%sum y_i
sy = sum(y);
%sum x_i
sx = sum(x);
%y dot product x
dp_xy = x'*y;
x_sq = sum(x.*x);%sum x_i^2 term
denom = sx^2 - P*x_sq;


m = (sy*sx-P*dp_xy)/denom
b = (-sy*x_sq+sx*dp_xy)/denom

%plot results
plot(x,y,'o')
hold
plot(x,m*x+b,'--v')
legend('data','model')