%-----------------------------------------------------------------------
% MATLAB Project 2 (due 11/6) p.184(1)*, p.185(3)*
%       Answers --- Fall 2003
% E.A. Coutsias 
%-----------------------------------------------------------------------
% PROBLEM 1  <#1, (p.184)>
U=round(20*rand(4))-10;
V=round(10*rand(4));
b=ones(4,1);
% compute rank; if columns independent then rank = 4
rank(U);
rank(V);
% U * c = b ==> c = inv(U) * b and the inverse of U gives transition
% matrix from standard basis to (u1,u2,u3,u4)
A = inv(U);
c = A*b;
err1 = b-U*c;
% similarly, % V * d = b ==> d = inv(V) * b and the inverse of V gives 
% transition matrix from standard basis to (v1,v2,v3,v4)
B = inv(V);
d = B*b;
err2 = b-V*d;
% Transition from vi to ui
%      U*c = b = V*d ==> c = A*V*d = inv(U)*V*d
   c1 = A*V*d;
   error3 = c1 - c;
% Transition from ui to vi
%      U*c = b = V*d ==> d = B*U*c = inv(V)*U*c  
   d1 = B*U*c;
   error4 = d1 - d;
%--------------------------------------------
% MATLAB OUTPUT (pasted from command window)
%--------------------------------------------
>> U=round(20*rand(4))-10

U =

    -1     7     7     7
     9     1   -10     0
    -1    -6     4     4
    -2     3    -2    -1

>> V=round(10*rand(4))

V =

     3     3     4     5
     2     5     9     9
     2     2     9     8
     7     7     6     6

>> b=ones(4,1);
>> rank(U)

ans =

     4

>> rank(V)

ans =

     4
>> A = inv(U)

A =

    0.0249    0.0312   -0.1215   -0.3115
    0.0561   -0.0013   -0.0948    0.0134
    0.0280   -0.0721   -0.1188   -0.2790
    0.0623    0.0779    0.1963    0.2212

>> c = A*b

c =

   -0.3769
   -0.0267
   -0.4419
    0.5576

>> err1 = b-U*c

err1 =

   1.0e-15 *

    0.5551
   -0.8882
    0.4441
         0
>> B = inv(V)

B =

    0.1803   -0.3333    0.1803    0.1093
   -0.2787    0.3333   -0.2787    0.1038
   -0.7213    0.0000    0.2787    0.2295
    0.8361   -0.0000   -0.1639   -0.3115

>> d = B*b

d =

    0.1366
   -0.1202
   -0.2131
    0.3607

>> err2 = b-V*d

err2 =

   1.0e-15 *

         0
         0
         0
    0.1110

>> c1 = A*V*d

c1 =

   -0.3769
   -0.0267
   -0.4419
    0.5576
>> error3 = c1 - c

error3 =

   1.0e-16 *

    0.5551
   -0.1388
         0
         0

>> d1 = B*U*c

d1 =

    0.1366
   -0.1202
   -0.2131
    0.3607

>> error4 = d1 - d

error4 =

   1.0e-15 *

   -0.4718
    0.4302
    0.4718
   -0.4441

%-----------------------------------------------------------------------
% PROBLEM 2 <#3, p. 185>
%-----------------------------------------------------------------------
B = round(10*rand(8,4));
X = round(10*rand(4,3));
C = B*X;
A = [B C];
% (a) The columns of C belong to the column space of B, so { R(C) subset R(B) }
% The rank of A is expected to be 4, the rank of B (which is 4 because 4
% random vectors in R^4 will be linearly independent with high probability)
rank(A);
% (b) The first 4, since we know they are a basis.
AE = rref(A);
% In the reduced row echelon form the first four columns should contain unit 
% vectors, and the last four rows should be zero.
% rank = 4 => dim(R(A)) = dim(R(A')) = 4
%             dim(N(A)) = 7-4 = 3
%             dim(N(A'))= 8-4 = 4
% (c) To show that in general, if we start with
%     [ B B*X ] ---> [ I X ]  (if m=n)
%                    [ 0 0 ]  (if m>n)
E = round(10*rand(6,4));
Y = round(10*rand(4,2));
F = E*Y;
D = [E F];
rank(D);
DE = rref(D);
% the reason is that in the process of eliminating we multiply
% by various matrices on the left; the product of these matrices, DM,
% multiplied times E produces a matrix with unit entries
% i.e. DM * E = [e1, e2, e3, e4] (these all belong to R6 here).
% By the same token, the elimination on the last three columns
% of A, E*Y, is represented by left multiblication by the same matrix,
% so DM*E*Y = [e1, e2, e3, e4]*Y 
%--------------------------------------------
% MATLAB OUTPUT (pasted from command window)
%--------------------------------------------
>> B = round(10*rand(8,4))

B =

     8     8     8     9
     7     6    10     7
     3     4     5     1
     3     7     9     0
     3     5     2     9
     5     4    10     2
     7     7     3     3
     3     6     3     7

>> X = round(10*rand(4,3))

X =

     3     6     4
     5     4     2
     1     5     6
    10     3     8

>> C = B*X

C =

   162   147   168
   131   137   156
    44    62    58
    53    91    80
   126    75   106
    65   102   104
    89    94    84
   112    78    98

>> A = [B C]  

A =

     8     8     8     9   162   147   168
     7     6    10     7   131   137   156
     3     4     5     1    44    62    58
     3     7     9     0    53    91    80
     3     5     2     9   126    75   106
     5     4    10     2    65   102   104
     7     7     3     3    89    94    84
     3     6     3     7   112    78    98

>> rank(B)

ans =

     4

>> rank(A)

ans =

     4


>> E = rref(A)

E =

     1     0     0     0     3     6     4
     0     1     0     0     5     4     2
     0     0     1     0     1     5     6
     0     0     0     1    10     3     8
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
% Note that the matrix X has appeared here!
% To test that this is always the case:
>> clear all
>> E = round(10*rand(6,4))

E =

     4    10     0     4
     1     1     6     6
     6     4     7     8
     8     5    10     1
     7     3     8     9
    10     6     7     9

>> Y = round(10*rand(4,2))

Y =

     6     7
     3     4
     9    10
     5     1

>> F = E*Y

F =

    74    72
    93    77
   151   136
   158   177
   168   150
   186   173

>> D = [E F]

D =

     4    10     0     4    74    72
     1     1     6     6    93    77
     6     4     7     8   151   136
     8     5    10     1   158   177
     7     3     8     9   168   150
    10     6     7     9   186   173

>> rank(D)

ans =

     4

>> DE = rref(D)

DE =

     1     0     0     0     6     7
     0     1     0     0     3     4
     0     0     1     0     9    10
     0     0     0     1     5     1
     0     0     0     0     0     0
     0     0     0     0     0     0


%-----------------------------------------------------------------------
