function [ w_quad ] = linearreg( )

%data matrix X and output vector y
X = [ones(6,1),(3:8)']
y = [150;155;150;170;160;175]
%function which computes optimal weights in closed form
w_ls_opt = inline('inv(transpose(data)*data)*transpose(data)*output','data','output');

%compute optimal weights assuming linear relation
w_linear = w_ls_opt(X,y)

%extend data matrix by column of squares
Xquad = [X,X(:,2).^2]
%compute optimal weights assuming non-linear relation
w_quad = w_ls_opt(Xquad, y)

%define line and quadratic plots
xval = 0:0.01:10;
line = inline('w(1) + w(2) * xval','w','xval');
quadcurve = inline('w(1) + w(2)*xval+ w(3)*(xval.^2)','w','xval');
%plot linear
figure;
hold on
plot(xval,line(w_linear, xval),'b')
plot(X(:,2),y,'bo')
%plot quadratic
figure(2);
hold on
plot(xval,quadcurve(w_quad, xval),'r')
plot(X(:,2),y,'ro')

end

