% script for Lecture 8
randn('state',0)
m = 10
n = 3
sigma = blkdiag(8*eye(3),16*eye(3),24*eye(4))
mtrue = [10,100,9.8]'
G = [ones(m,1), (1:m)', -0.5*(1:m)'.^2]
datatrue = G*mtrue;
data = datatrue + sigma*randn(m,1);
G = [ones(m,1), (1:m)', -0.5*(1:m)'.^2]
datatrue = G*mtrue;
data = datatrue + sigma*randn(m,1);
% compute the least squares solution without 
% reference to sigma, then do the scaled least squares
% and compare....also do some graphs
W = inv(sigma);
% ignore variance issue
mapprox1 = G\data
% now pay attention
mapprox2 = (W*G)\(W*data)
% for the plotting
tnodes = 0:.1:m+5;
% plot true curve in green, with approx1 in red, approx2 in blue
plot(tnodes,mtrue(1) + mtrue(2)*tnodes - 0.5*mtrue(3)*tnodes.^2,'g')
hold on
plot(tnodes,mapprox1(1) + mapprox1(2)*tnodes - 0.5*mapprox1(3)*tnodes.^2,'r')
plot(tnodes,mapprox2(1) + mapprox2(2)*tnodes - 0.5*mapprox2(3)*tnodes.^2,'b')

