%-----------------------------------------------------------------------
% MATLAB Project 1 (due 9/30) p.92(2)*, p.94(6)*, p.122(5)*
%       Answers --- Fall 2003
% E.A. Coutsias 
%-----------------------------------------------------------------------
% PROBLEM 1  <#2, (p.92)>
A=round(10*rand(8));
b=round(10*rand(8,1));
% flops(0) % Obsolete command
tic
x=A\b;
toc  % not extremely accurate, but one can get a sense on efficiency
% flops;
tic % flops(0);
U=rref([A b]);
y = U(:,9);
toc %flops
format short
x-y
r=b-A*x;% best answer
s=b-A*y;% worse answer
%--------------------------------------------
% MATLAB OUTPUT (pasted from command window)
%--------------------------------------------
>> A=round(10*rand(8))

A =

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

>> b=round(10*rand(8,1))

b =

     8
     7
     3
     3
     3
     5
     7
     3

>> tic
>> x=A\b

x =

   -2.5388
    1.7280
    0.4568
   -4.4718
    0.9337
    1.2454
    2.6135
   -0.4036

>> toc

elapsed_time =

   15.6752

>> tic
>> U=rref([A b])

U =

1.0000       0       0       0       0       0       0       0 -2.5388
     0  1.0000       0       0       0       0       0       0  1.7280
     0       0  1.0000       0       0       0       0       0  0.4568
     0       0       0  1.0000       0       0       0       0 -4.4718
     0       0       0       0  1.0000       0       0       0  0.9337
     0       0       0       0       0  1.0000       0       0  1.2455
     0       0       0       0       0       0  1.0000       0  2.6135
     0       0       0       0       0       0       0  1.0000 -0.4036

>> y = U(:,9)

y =

   -2.5388
    1.7280
    0.4568
   -4.4718
    0.9337
    1.2455
    2.6135
   -0.4036

>> toc

elapsed_time =

   20.8900

>> format short
x-y
r=b-A*x;% best answer
s=b-A*y;% worse answer

ans =

   1.0e-04 *

    0.1027
   -0.0232
    0.1903
    0.1316
    0.1462
   -0.0831
    0.0268
   -0.0945

>> r=b-A*x

r =

   1.0e-14 *

    0.3553
         0
   -0.0444
    0.1332
    0.1776
    0.0888
    0.2665
         0

>> s=b-A*y

s =

   1.0e-03 *

    0.2308
    0.2608
    0.0854
    0.2704
    0.1413
    0.1283
    0.0998
    0.1140

>> 
% Notice that the first method of solving the system, the built-in operation \
% which solves the system by triangular factorization is both faster and much more
% accurate
%-----------------------------------------------------------------------
%-----------------------------------------------------------------------
% PROBLEM 2 <#6, p. 94>
%-----------------------------------------------------------------------
(a) The adjacency matrix A is
A=[ 0 1 0 1 0 0 0 1 ;
    1 0 1 0 0 0 1 0 ;
    0 1 0 1 0 0 0 0 ;
    1 0 1 0 1 0 0 0 ;
    0 0 0 1 0 1 0 0 ;
    0 0 0 0 1 0 1 0 ;
    0 1 0 0 0 1 0 1 ;
    1 0 0 0 0 0 1 0 ]
%--------------------
>> clear all
>> A=[ 0 1 0 1 0 0 0 1 ;
    1 0 1 0 0 0 1 0 ;
    0 1 0 1 0 0 0 0 ;
    1 0 1 0 1 0 0 0 ;
    0 0 0 1 0 1 0 0 ;
    0 0 0 0 1 0 1 0 ;
    0 1 0 0 0 1 0 1 ;
    1 0 0 0 0 0 1 0 ]

A =

     0     1     0     1     0     0     0     1
     1     0     1     0     0     0     1     0
     0     1     0     1     0     0     0     0
     1     0     1     0     1     0     0     0
     0     0     0     1     0     1     0     0
     0     0     0     0     1     0     1     0
     0     1     0     0     0     1     0     1
     1     0     0     0     0     0     1     0
%-----------------------------------------------------------------------
(b) A^2: 
# of walks, length 2   |  From     |    To     |  Matrix element
        2              |   V1      |    V7     |    A^2(1,7)=A^2(7,1)
        1              |   V4      |    V8     |    A^2(4,8)=A^2(8,4)
        0              |   V5      |    V6     |    A^2(5,6)=A^2(6,5)
        0              |   V8      |    V3     |    A^2(8,3)=A^2(3,8)
%--------------------------------------------------------------------
>> A^2

ans =

     3     0     2     0     1     0     2     0
     0     3     0     2     0     1     0     2
     2     0     2     0     1     0     1     0
     0     2     0     3     0     1     0     1
     1     0     1     0     2     0     1     0
     0     1     0     1     0     2     0     1
     2     0     1     0     1     0     3     0
     0     2     0     1     0     1     0     2
%-----------------------------------------------------------------------
(c) A^4, A^6, A^8: 
# length 4,   6,   8   |  From     |    To     | 
        15  103  577   |   V1      |    V7     |
        10   73  501   |   V4      |    V8     |
         0    0    0   |   V5      |    V6     |
         0    0    0   |   V8      |    V3     |
%-----------------------------------------------
>> A^4

ans =

    18     0    13     0     9     0    15     0
     0    18     0    15     0     9     0    13
    13     0    10     0     7     0    10     0
     0    15     0    15     0     8     0    10
     9     0     7     0     7     0     8     0
     0     9     0     8     0     7     0     7
    15     0    10     0     8     0    15     0
     0    13     0    10     0     7     0    10

>> A^6

ans =

   119     0    86     0    64     0   103     0
     0   119     0   103     0    64     0    86
    86     0    63     0    47     0    73     0
     0   103     0    93     0    56     0    73
    64     0    47     0    38     0    56     0
     0    64     0    56     0    38     0    47
   103     0    73     0    56     0    93     0
     0    86     0    73     0    47     0    63

>> A^8

ans =

   799     0   577     0   436     0   697     0
     0   799     0   697     0   436     0   577
   577     0   418     0   316     0   501     0
     0   697     0   614     0   381     0   501
   436     0   316     0   243     0   381     0
     0   436     0   381     0   243     0   316
   697     0   501     0   381     0   614     0
     0   577     0   501     0   316     0   418
%-----------------------------------------------------------------------
(d) A^3, A^5, A^7: 
# length 3,   5,   7   |  From     |    To     | 
         0    0    0   |   V1      |    V7     |
         0    0    0   |   V4      |    V8     |
         3   15   94   |   V5      |    V6     |
         3   23  159   |   V8      |    V3     |
%-----------------------------------------------
>> A^3

ans =

     0     7     0     6     0     3     0     5
     7     0     5     0     3     0     6     0
     0     5     0     5     0     2     0     3
     6     0     5     0     4     0     4     0
     0     3     0     4     0     3     0     2
     3     0     2     0     3     0     4     0
     0     6     0     4     0     4     0     5
     5     0     3     0     2     0     5     0

>> A^5

ans =

     0    46     0    40     0    24     0    33
    46     0    33     0    24     0    40     0
     0    33     0    30     0    17     0    23
    40     0    30     0    23     0    33     0
     0    24     0    23     0    15     0    17
    24     0    17     0    15     0    23     0
     0    40     0    33     0    23     0    30
    33     0    23     0    17     0    30     0

>> A^7

ans =

     0   308     0   269     0   167     0   222
   308     0   222     0   167     0   269     0
     0   222     0   196     0   120     0   159
   269     0   196     0   149     0   232     0
     0   167     0   149     0    94     0   120
   167     0   120     0    94     0   149     0
     0   269     0   232     0   149     0   196
   222     0   159     0   120     0   196     0
A^k is checkerboard: entry (i,j) is zero if i+j+k is even
The way the graph is numbered, we start off with only paths of
even length connecting even (corresp. odd) numbered rows to other even 
(corresp. odd) numbered rows. Even (corresp. odd) numbered rows are connected 
by paths of odd length to odd (corrsp. even) numbered rows.
That is, A begin as a "checkerboard" matrix, and this structure is
preserved by any power of A.
%-----------------------------------------------------------------------
(e) B = A; B(3,6) = 1; B(6,3) = 1; B(5,8) = 1; B(8,5) = 1; B
%-----------------------------------------------------------
>> B = A;
B(3,6) = 1; B(6,3) = 1; B(5,8) = 1; B(8,5) = 1;
B

B =

     0     1     0     1     0     0     0     1
     1     0     1     0     0     0     1     0
     0     1     0     1     0     1     0     0
     1     0     1     0     1     0     0     0
     0     0     0     1     0     1     0     1
     0     0     1     0     1     0     1     0
     0     1     0     0     0     1     0     1
     1     0     0     0     1     0     1     0


>> B^2

ans =

     3     0     2     0     2     0     2     0
     0     3     0     2     0     2     0     2
     2     0     3     0     2     0     2     0
     0     2     0     3     0     2     0     2
     2     0     2     0     3     0     2     0
     0     2     0     2     0     3     0     2
     2     0     2     0     2     0     3     0
     0     2     0     2     0     2     0     3

>> B^3

ans =

     0     7     0     7     0     6     0     7
     7     0     7     0     6     0     7     0
     0     7     0     7     0     7     0     6
     7     0     7     0     7     0     6     0
     0     6     0     7     0     7     0     7
     6     0     7     0     7     0     7     0
     0     7     0     6     0     7     0     7
     7     0     6     0     7     0     7     0

>> B^4

ans =

    21     0    20     0    20     0    20     0
     0    21     0    20     0    20     0    20
    20     0    21     0    20     0    20     0
     0    20     0    21     0    20     0    20
    20     0    20     0    21     0    20     0
     0    20     0    20     0    21     0    20
    20     0    20     0    20     0    21     0
     0    20     0    20     0    20     0    21

>> B^5

ans =

     0    61     0    61     0    60     0    61
    61     0    61     0    60     0    61     0
     0    61     0    61     0    61     0    60
    61     0    61     0    61     0    60     0
     0    60     0    61     0    61     0    61
    60     0    61     0    61     0    61     0
     0    61     0    60     0    61     0    61
    61     0    60     0    61     0    61     0
Conjecture still valid, because we only connected odd-even rows by paths
of (odd) length 1
--------------
%-----------------------------------------------------------------------
(f) C=B;C(6,8) = 1; C(8,6) = 1; 
>> C=B; C(6,8) = 1; C(8,6) = 1;C

C =

     0     1     0     1     0     0     0     1
     1     0     1     0     0     0     1     0
     0     1     0     1     0     1     0     0
     1     0     1     0     1     0     0     0
     0     0     0     1     0     1     0     1
     0     0     1     0     1     0     1     1
     0     1     0     0     0     1     0     1
     1     0     0     0     1     1     1     0

>> C^2

ans =

     3     0     2     0     2     1     2     0
     0     3     0     2     0     2     0     2
     2     0     3     0     2     0     2     1
     0     2     0     3     0     2     0     2
     2     0     2     0     3     1     2     1
     1     2     0     2     1     4     1     2
     2     0     2     0     2     1     3     1
     0     2     1     2     1     2     1     4

>> C^3

ans =

     0     7     1     7     1     6     1     8
     7     0     7     0     6     2     7     2
     1     7     0     7     1     8     1     6
     7     0     7     0     7     2     6     2
     1     6     1     7     2     8     2     8
     6     2     8     2     8     4     8     7
     1     7     1     6     2     8     2     8
     8     2     6     2     8     7     8     4

>> C^4

ans =

    22     2    20     2    21    11    21     8
     2    21     2    20     4    22     4    22
    20     2    22     2    21     8    21    11
     2    20     2    21     4    22     4    22
    21     4    21     4    23    13    22    13
    11    22     8    22    13    31    13    26
    21     4    21     4    22    13    23    13
     8    22    11    22    13    26    13    31
Now C is no longer checkerboard, and structure is destroyed.
%-----------------------------------------------------------------------
%-----------------------------------------------------------------------
% PROBLEM 3 <#5, p. 122>
%-----------------------------------------------------------------------
U  = round(100*rand(10))
U  = triu(U,1) + 0.1*eye(10)
>> clear all
>> U  = round(100*rand(10))

U =

    44    72     5    31    16    43    49    69    42    57
    35    89     8    37    87    89    82    65    75    63
    15    27    64    39    24    73    46    98    79    23
    68    25    19    59    65    69    46    55    92    55
    70    87    84    12    97    35    45    40    84    93
    73    23    17     4    66    17    41    20    37    34
    48    80    17    46    87    16    90    63    62    66
    55    91    99    87     1    19     1    73    73    39
    12    23    44    93    14    42    30    38    19    63
    45    24    34    26    82    86     5     1    90    70

>> U  = triu(U,1) + 0.1*eye(10)

U =

0.1000 72.0000  5.0000 31.0000 16.0000 43.0000 49.0000 69.0000 42.0000 57.0000
     0  0.1000  8.0000 37.0000 87.0000 89.0000 82.0000 65.0000 75.0000 63.0000
     0       0  0.1000 39.0000 24.0000 73.0000 46.0000 98.0000 79.0000 23.0000
     0       0       0  0.1000 65.0000 69.0000 46.0000 55.0000 92.0000 55.0000
     0       0       0       0  0.1000 35.0000 45.0000 40.0000 84.0000 93.0000
     0       0       0       0       0  0.1000 41.0000 20.0000 37.0000 34.0000
     0       0       0       0       0       0  0.1000 63.0000 62.0000 66.0000
     0       0       0       0       0       0       0  0.1000 73.0000 39.0000
     0       0       0       0       0       0       0       0  0.1000 63.0000
     0       0       0       0       0       0       0       0       0  0.1000

>> det(U)

ans =

   1.0000e-10

>> det(U')

ans =

  -2.3023e-08


>> det(U*U')

ans =

   6.8871e+10

>> 
% the roundoff involved in the computation of U*U' and, especially, 
det(U*U') destroyed the determinant; the result is wild, nowhere near
Det(U)*det(U') = 10-20
(even det(U') came back as 10^-8, not 10^-10)
%-----------------------------------------------------------------------
