% A least squares solution example. Here we have three linear equations for
% only two variables, and there is no solution. However, we can use the "\"
% operator to find a least squares solution.
%
% Samuli Siltanen October 2015
% Construct system matrix
A = [[1 1];[2 1];[0 1]];
% Construct the right hand side
b = [95;125;-10];
% Solve the problem
tmp = A\b;
x = tmp(1);
y = tmp(2);
% Plot the result.
t = [-50 150]; % Evaluation points
line1 = -t+95;
line2 = -2*t+125;
line3 = -10*ones(size(t));
figure(1)
clf
plot(t,line1) % First line
hold on
plot(t,line2) % Second line
plot(t,line3) % Third line
plot(x,y,'r.','markersize',20) % Least squares solution
set(gca,'xtick',[(min(t)-20):30:(max(t)+20)],'fontsize',20)
axis([-10 100 -20 70])
axis equal