% Demonstration about least-squares solution in the case k>n.
%
% Samuli Siltanen Feb 2017

% Pick a measurement matrix
A = [[1 0];[0 .5];[0 0]];

% Pick a measurement vector
m = [0;0;0];

% Create evaluation points for our function
M   = 1;
Nf  = 256;
tmp = linspace(-M,M,Nf);
[f1,f2] = meshgrid(tmp);
f1 = f1(:);
f2 = f2(:);

% Evaluate || A*f - m || at the grid points
E = zeros(size(f1));
for iii = 1:length(f1)
    E(iii) = norm(A*[f1(iii);f2(iii)]-m).^2;
    
    % Monitor the run
    if mod(iii,100)==0
       disp([iii length(f1)]) 
    end
end
f1 = reshape(f1,Nf,Nf);
f2 = reshape(f2,Nf,Nf);
E = reshape(E,Nf,Nf);

% Take a look
figure(1)
clf
%plot(f1,f2,'b.')
mesh(f1,f2,E)
axis equal