function Generallinearmodel
clc
clear all
close all
5 x1 = randn (1);
x2 = randn (1);
x3 = randn (1);
x4 = randn (1);
x = [x1 x2 x3 x4];
10 B = [1 2 -3 2.5];
y = 1*x1 + 2*x2 - 3*x3 + 2.5*x4;
e=randn(size(y)); % random noise
y = y + e;
%Linear model: Y=X*beta'+e, where beta is the parameter needs to be
15 %estimated
x = [x' ones(length(x),1)]; %second column treated as all ones since
x_2=1
y = y';
beta = int(X'*X)*X'*y %Least Squares Estimator
20 figure('color',[1 1 1]);
plot(x,y,'*'); %desired curve
hold on;
plot(x, beta(1)*x1+beta(2)*x2+beta(3)*x3+beta(4)*x4); %estimated curve
legend('y = x1 + 2*x2 - 3*x3 + 2.5*x4',['y_h_a_t=' num2str(beta(1))
25 '*x1+'...
num2str(beta(2)) '*x2+' num2str(beta(3)) '*x3+' num2str(beta(4)) '*x4'])
legend boxoff
title('Least Squares Estimate');
xlabel('time (sec)');
30 ylabel('Amplitude');