% lineqsys.m
% Illustration of linear systems of equations
clear

A=[1 2 ; 1 0.5];
b=[6 ; 3];

% The analytical solution
x=A\b

% One row at a time
figure(1), clf, axis equal, hold on
u=[0 3];
%eq1
v=1/A(1,2)*([b(1) b(1)]-A(1,1)*u);
plot(u,v,'r')
%eq2
v=1/A(2,2)*([b(2) b(2)]-A(2,1)*u);
plot(u,v,'g')
% plot analytical solution
plot(x(1),x(2),'b*')
title('The row equations and the solution')

% One column at a time
figure(2), clf, axis equal, hold on
%plot column vectors
c1=A(:,1); c2=A(:,2);
plot([0 c1(1)],[0 c1(2)],'r',c1(1),c1(2),'rp')
plot([0 c2(1)],[0 c2(2)],'g',c2(1),c2(2),'gp')
% Now use the analytical solution
v1=x(1)*c1; v2=x(2)*c2;
plot([0 v1(1)],[0 v1(2)],'r--')
plot([v1(1) v1(1)+v2(1)],[v1(2) v1(2)+v2(2)],'g--')
% plot the location of b
plot([0 b(1)],[0 b(2)],'b',b(1),b(2),'bp')
title('The column vectors and the "b-vector"')

% Make the unique change to a22 to give a system without solution
% Be sure you understand what happens in figures 1 & 2
% Do not worry about the error printout generated by Matlab. There
% is an error, so the printout just shows how good Matlab is.
% With this changed a22, make the unique change to b2 that gives
% a system with infinitely many solutions.