%% Decomposition of vectors

1*[1 0 0] + 2*[0 1 0] + 3*[0 0 1]

1*[1 1 1] + 1*[0 1 0] + 2*[0 0 1]

-1*[1 -1 0] + -1*[0 1 -1] + 2*[1 1 1]

%% matrix inversion
% define a matrix
x = [1 1; 1 -1]

% multiply data with inverse matrix
inv(x)*[1 1]'
inv(x)

%% trivial example
x = [2 0; 0 3];
inv(x)

%% parameter estimation - forward model
x = 0:0.1:8*pi; % abscissa values

figure;
y1 = x';         % straight line (note transpose)
plot(x, y1);

figure;
y2 = sin(x)';    % sine curve
plot(x, y2);

figure;
y3 = exp(-x/2)'; % exponential function
plot(x, y3);

% Generate data with some noise
figure;
y = y1/5 + y2 + 5*y3 + 0.5*randn(length(y1),1);
plot(x, y);

whos
%% Estimating the parameters using minimum least-squares (MNLS)
% creating the design matrix
M = [y1 y2 y3];
whos

% check correlation among basis functions
corr(M)

% (pseudo)inverting the design matrix
Minv = pinv(M);
whos

% getting the solution
b = Minv*y

% checking how solution predicts the data
figure;
ypred = b(1)*y1 + b(2)*y2 + b(3)*y3;
plot(x, ypred)

% compare with "ground truth"
hold on;    % don't overwrite figure
yreal = y1/5 + y2 + 5*y3;
plot(x, yreal, 'r');

% plot the difference between measured and predicted data
figure;
ydiff = y - ypred; 
plot(x, ydiff);

% variance of difference
var(ydiff)


