% MATLAB Recitation Demo for Monday, September 15.
% File: rdemo2b
%
% *** Linear combination of matrices ***
%
% MATLAB can help answer many questions concerning
% matrices and vectors.
% For example:
%   Express b as a linear combination of vectors (b = Ax),
%   Express A as a product of matrices (A = LU), or
%   Express A as a linear combination of other matrices.
%
% For the latter, we can "reshape" matrices into vectors
% with m*n components -- and then check if vec(A) is a 
% linear combination of vec(S), vec(T), etc.


>> diary rdemo2b

>> A = [17 14; 11 8]
A =
    17    14
    11     8

>> S = [1 2; 3 4]
S =
     1     2
     3     4

>> T = [5 6; 7 8]
T =
     5     6
     7     8

% The MATLAB command reshape(S, 4, 1) makes S into a matrix 
% whose shape has 4 rows and 1 column (i.e., a vector).
% Important: Reshape takes entries from column 1, then 
% column 2, etc.

>> v1 = reshape(S,4,1)
v1 =
     1
     3
     2
     4

>> v2 = reshape(T,4,1)
v2 =
     5
     7
     6
     8

>> b = reshape(A,4,1)
b =
    17
    11
    14
     8

>> Z = [v1 v2 b]
Z =
     1     5    17
     3     7    11
     2     6    14
     4     8     8

% Use elementary row operations to convert Z into
% reduced row echelon form.
% Recall that such elementary row operations yield
% an equivalent linear system that can be solved by
% backsubstitution or "inspection".

>> Z = ref(Z)
Z =
     1     0    -8
     0     1     5
     0     0     0
     0     0     0

% As usual, Z is now the augmented matrix for Rx = d.
%
% By inspection, we see that the vector d is an
% obvious linear combination of the columns of R.
% x = [-8; 5]
%
% Let's verify!

>> x = [-8; 5]
x =
    -8
     5

>> -8 * S + 5 * T
ans =
    17    14
    11     8

>> A
A =
    17    14
    11     8