%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% clean up the workspace, and generate some data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all ; close all ; clc
randn('seed',2);

N = 10;
m = 2
b = 3

x = (1:N)';
y = m*x + b + x .* randn(N,1);

figure();
plot(x , y , 'o' , x , m*x+b , 'k');
xlim([0 N+1]);
xlabel('x');
ylabel('y');
grid on;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fit a line to the data using OLS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = [x , ones(size(x))];
zols = A \ y;
mols = zols(1)
bols = zols(2)

hold on;
    plot(x , mols * x + bols , 'r');
hold off;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fit a line to the data using WLS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
W = diag(1./x);
zwls = (A' * W * A) \ (A' * W * y);
mwls = zwls(1)
bwls = zwls(2)

hold on;
    plot(x , mwls * x + bwls , 'g');
hold off;
legend('data' , 'truth' , ...
       'OLS estimate' , 'WLS estimate' , ...
       'Location' , 'Northwest');