% Example 2.14
%Program Ex542.m Simple OLS Problem
%Solution of Example 5.4.2. Quantities: b = estimated parameter vector,
% beta = true parameter vector, bZ = errorless parameter vector estimate,
%e = residual vector, eps = random error vector,
% eta = errorless dependent variable, n = no. of measurements
% t = measurement vector of time, tT = transpose of t,
% X =sensitivity matrix, XTX = X'*X, y = measurement vector of the dependent variable,
% yZ = errorless measurement vector of dependent variable
clear all
%Introduce constants and set up
n=11; beta=[1 .1 -0.005]'; t=0:1:n-1; tT=t'; X=[ones(n,1) tT tT.^2];
XTX=X'*X; eta=X*beta; yZ =eta; bZ=X\yZ;
%Introducing random errors
rng(129); eps=0.02*randn(n,1); y=eta+eps;
%Solve for parameter vector
b=(X'*X)\X'*y; e=y-X*b; yhat=X*b;
%Display results
disp(' X eta y eps resid'), Xp=[X eta y eps e];
fprintf('%6.0f %6.0f %6.0f %10.4f %10.4f %10.4f %10.4f \n',Xp')
disp(' XTX bZ b'), bp=[XTX bZ b];
fprintf('%7.0f %7.0f %7.0f %10.4f %10.5f \n',bp')
plot(tT,eta,'k',tT,y,'ko',tT,yhat,'k+'), xlabel('t_i'), ylabel('y_i')
