% Example  of LS approximation
% Suppose that the data is produced according to
% y_i = a + b*x_i + c*(x_i)^2
% For each x_i that we choose, we make a noisy measurement of y_i.
% We show how to obtain a LS approximation of the vector lambda = [a,b,c]'

close all;
clear all;

% We fix the model:
lambda = [10 20 -3]'; % model parameters, to be estimated

% Create the data points
N = 100;
x = 10*rand(N,1); % inputs
x = sort(x); % sorting x for the purpose of plotting
var = 20; % noise variance

% Let us produce the observations
S = [ones(length(x),1), x, x.^2];
y_noiseless = S*lambda; 
z = sqrt(var)*randn(length(y_noiseless),1); % noise 
y = y_noiseless + z; % noisy observable

figure(1)
subplot(2,1,1)
plot(x,y_noiseless,x,y,'*')
title('Model function and noisy observations')
xlabel('x'); ylabel('y_{noiseless} and y'); legend('Model function', 'Noisy observations');

% We estimate lambda assuming that x is known and y is the noisy
% observation

lambda_hat = inv(S'*S)*S'*y
lambda_hat = (S'*S)\(S'*y)
subplot(2,1,2)
plot(x,S*lambda,x,S*lambda_hat)
title('Actual and estimated model function')
xlabel('x'); ylabel('y and y_{hat}'); legend('Actual model function','Estimated model function');