% CLASS6DEC  Quick demo of row operations on an augmented matrix.

% system of 3 equations in 3 unknowns (A x = b) as augmented matrix
Ab = [ 2 -1.5  3  1;
      -1    0  2  3;
       4 -4.5  5  1]

more off  % dump solution at command line

% Gauss elimination by row operations
Ab(2,:) = Ab(2,:) - (- 0.5) * Ab(1,:)
Ab(3,:) = Ab(3,:) - 2 * Ab(1,:)
Ab(3,:) = Ab(3,:) - 2 * Ab(2,:)

% back substitution
x = zeros(3,1);   % space for solution
x(3) = Ab(3,4) / Ab(3,3)
x(2) = (Ab(2,4) - Ab(2,3) * x(3)) / Ab(2,2)
x(1) = (Ab(1,4) - Ab(1,2) * x(2) - Ab(1,3) * x(3)) / Ab(1,1)

% recompute solution using backslash
A = Ab(:,1:3);
b = Ab(:,4);
A \ b
