0% found this document useful (0 votes)
37 views1 page

Code

This document demonstrates a simple linear regression model. Random variables are generated for x1 through x4 and a target y is created as a linear combination of those variables with added random noise. The linear regression algorithm is then used to estimate the coefficients that minimize the error between the predicted y and actual y. A plot is generated comparing the desired curve to the estimated curve from the model.

Uploaded by

vinh quoc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views1 page

Code

This document demonstrates a simple linear regression model. Random variables are generated for x1 through x4 and a target y is created as a linear combination of those variables with added random noise. The linear regression algorithm is then used to estimate the coefficients that minimize the error between the predicted y and actual y. A plot is generated comparing the desired curve to the estimated curve from the model.

Uploaded by

vinh quoc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

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');

You might also like