% MATLAB Recitation Demo for Monday, September 15.
% File: rdemo2a
%
% Example 1:
% Recall definition for reduced row echelon form.
% In MATLAB, write Ax = b as an augmented matrix.
% Use MATLAB commands to convert [A b] into [R d].
%
% Obtain solution x by "inspection" -- d is what
% linear combination of the columns of R ?
% Or, if you prefer, visualize backsubstitution 
% for Rx = d.
%
% Example 2: 
% Same b but change (3,3) entry in A.
%
>> diary rdemo2

>> b = [9; 29; 33]
b =
     9
    29
    33

>> A = [1 0 3; 3 2 9; 1 4 8]
A =
     1     0     3
     3     2     9
     1     4     8

>> Z = [A b]
Z =
     1     0     3     9
     3     2     9    29
     1     4     8    33

>> Z(2,:) = Z(2,:) - (3) * Z(1,:)
Z =
     1     0     3     9
     0     2     0     2
     1     4     8    33

>> Z(3,:) = Z(3,:) - (1) * Z(1,:)
Z =
     1     0     3     9
     0     2     0     2
     0     4     5    24

>> Z(3,:) = Z(3,:) - (2) * Z(2,:)
Z =
     1     0     3     9
     0     2     0     2
     0     0     5    20

>> Z(3,:) = (1/5) * Z(3,:)
Z =
     1     0     3     9
     0     2     0     2
     0     0     1     4

>> Z(2,:) = (1/2) * Z(2,:)
Z =
     1     0     3     9
     0     1     0     1
     0     0     1     4

>> Z(1,:) = Z(1,:) - (3) * Z(3,:)
Z =
     1     0     0    -3
     0     1     0     1
     0     0     1     4

% Z is now in reduced row echelon form; see definition.
% d is the last column of Z.
% R is the first 3 columns of Z.
% We can solve Rx = d by inspection: d is obviously a
% linear combination of columns of R.
% Let's verify!

>> x = [-3; 1; 4]
x =
    -3
     1
     4

>> b
b =
     9
    29
    33

>> A * x
ans =
     9
    29
    33

%%%%%%%%%%%%%%%%%
% Example 2
%%%%%%%%%%%%%%%%%
% Let's change the (3,3) entry of A to 3.
>> A(3,3) = 3
A =
     1     0     3
     3     2     9
     1     4     3

>> Z = [A b]
Z =
     1     0     3     9
     3     2     9    29
     1     4     3    33

>> Z = ref(Z)
Z =
     1     0     3     0
     0     1     0     0
     0     0     0     1

% d is the last column of Z.
% R is the first 3 columns of Z.
% BAD news: Rx = d is not solvable because
% d cannot be a linear combination of columns of R.
% (Any linear combination of columns of R gives a vector
%  whose last component is zero.)
% Or, note that the last equation of Rx = d is not
% solvable because 0 * each "unknown" cannot add up
% to 1.
>> diary off