%-----------------------------------------------------------------------
% MATLAB Project 1 (due 2/21) p.83(2)*, p.84(6)*, p.112(5)*
%       Answers --- Spring 2007
% E.A. Coutsias 
%-----------------------------------------------------------------------
% PROBLEM 1  <#2, (p.83)>
for n = [200,500,1000]
   A=round(10*rand(n)); b = (sum(A'))';
   z = ones(n,1);  %exact solution
   n

   tic
   x   = A\b; 
   toc   % not too accurate, but gives a sense on efficiency
   err0 = max(abs(x-z))

   tic
   y1  = inv(A)*b; 
   toc
   err1 = max(abs(y1-z))

   tic      
   U   = rref([A b]);
   y2  = U(:,n+1); toc
   err2 = max(abs(y2-z))
end
%--------------------------------------------
% MATLAB OUTPUT (pasted from command window)
%--------------------------------------------
n = 200

Elapsed time is 0.015967 seconds.
err0 = 3.8725e-13

Elapsed time is 0.026884 seconds.
err1 = 3.0127e-12

Elapsed time is 9.939272 seconds.
err2 = 0

------------------------
n = 500

Elapsed time is 0.215674 seconds.
err0 = 2.5231e-12

Elapsed time is 0.416951 seconds.
err1 = 8.1855e-12

Elapsed time is 67.244764 seconds.
err2 = 0

-------------------------
n = 1000

Elapsed time is 1.155755 seconds.
err0 = 1.4198e-12

Elapsed time is 2.558819 seconds.
err1 = 1.3195e-11

Elapsed time is 321.907868 seconds.
err2 = 0

% Notice that the first method of solving the system, the built-in 
% operation \ which solves the system by triangular factorization 
% is both faster and more accurate (slightly!); the third method 
% (added for comparison) is the slowest. Results will vary, as A is random
%-----------------------------------------------------------------------
%-----------------------------------------------------------------------
% PROBLEM 2 <#6, p. 94>
%-----------------------------------------------------------------------
(a) The adjacency matrix A is
%--------------------
>> 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 ]

%-----------------------------------------------------------------------
(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 =

     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 =

    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 =

   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 =

   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 =

     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 =

     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 =

     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 =

     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 =

     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 =

     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 =

    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 =

     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 =

     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 =

     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 =

    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) =

   1.0000e-10

>> det(U') =

  -2.3023e-08


>> det(U*U') =

   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)
%-----------------------------------------------------------------------
