%-----------------------------------------------------------------------
% MATLAB Project 3 (due 12/4) p.308(3)*, p.310(5)*
%       Answers --- Fall 2003
% E.A. Coutsias 
%-----------------------------------------------------------------------
% PROBLEM 1  <#3, (p.308)>
%--------------------------------------------
p = [1:10]';
T = [222;227;223;233;244;253;260;266;270;266];
A = vander(p);
%--------------------------------------------
% (1) find least squares solution to linear fit
%     T = c1*p + c2*1
   V = A(:,9:10);
   c = V\T;
%--------------------------------------------
% (2) test goodness of fit:
   q = 1:.1:10;
   z = polyval(c,q);
   plot(q,z,p,T,'x')
%--------------------------------------------
% (3) Now try a cubic fit
%     T = c1*p^3 + c2*p^2 + c3*p + c4*1
   V3 = A(:,7:10)
   c3 = V3\T
   z3 = polyval(c3,q);
   plot(q,z3,p,T,'x')
%--------------------------------------------
% (4) Now try 6th-order fit
%     T = c1*p^6 + ... + c6*p + c7*1
   V6 = A(:,4:10)
   c6 = V6\T
   z6 = polyval(c6,q);
   plot(q,z6,p,T,'x')
%--------------------------------------------
%--------------------------------------------
% MATLAB OUTPUT (pasted from command window)
%--------------------------------------------
>> p = [1:10]';
T = [222;227;223;233;244;253;260;266;270;266];
A = vander(p);
>>    V = A(:,9:10)

V =

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

>> c = V\T

c =

    6.0727
  213.0000
%--------------------------------------------
>>    q = 1:.1:10;
   z = polyval(c,q);
   plot(q,z,p,T,'x')

(figure pr3_1.eps)

%--------------------------------------------
>> V3 = A(:,7:10)

V3 =

           1           1           1           1
           8           4           2           1
          27           9           3           1
          64          16           4           1
         125          25           5           1
         216          36           6           1
         343          49           7           1
         512          64           8           1
         729          81           9           1
        1000         100          10           1
>> c3 = V3\T

c3 =

   -0.2339
    3.7302
  -10.3087
  230.2333
>>    z3 = polyval(c3,q);
   plot(q,z3,p,T,'x')
( figure pr3_2.eps )
%--------------------------------------------
>>    V6 = A(:,4:10)
   c6 = V6\T

V6 =

  Columns 1 through 6 

           1           1           1           1           1           1
          64          32          16           8           4           2
         729         243          81          27           9           3
        4096        1024         256          64          16           4
       15625        3125         625         125          25           5
       46656        7776        1296         216          36           6
      117649       16807        2401         343          49           7
      262144       32768        4096         512          64           8
      531441       59049        6561         729          81           9
     1000000      100000       10000        1000         100          10

  Column 7 

           1
           1
           1
           1
           1
           1
           1
           1
           1
           1


c6 =

   -0.0091
    0.3074
   -4.0528
   26.0274
  -82.4740
  121.0567
  161.3000
   z6 = polyval(c6,q);
   plot(q,z6,p,T,'x')
( figure pr3_3.eps )
%--------------------------------------------
%-----------------------------------------------------------------------
% PROBLEM 2 <#5, p. 310>
%-----------------------------------------------------------------------
%--------------------------------------------
% (1) A(5,2); since columns random, expect rank = 2; then
%     dim R(A)  = dim R(A') = 2
%     dim N(A)  = 5-2 = 3
%     dim N(A') = 5-2 = 3
      A = rand(5,2)*rand(2,5);
      rank(A)
      Z = null(A);
      rank(Z)    % this will give 3, the nullity of A
%--------------------------------------------
% (2) perform Gram-Schmidt on A
      Q = orth(A)
      W = null(A')
      S = [Q W]
      S*S' - eye(5)
% S is orthogonal: Q is an ortho. basis of R(A)
%                  W is an ortho. basis of N(A')
%                  R(A) and N(A') are orthogonal complements of each other
%  in particular, W is a basis for N(A') and all its elements are orthogonal
%     to the elements of A, hence W^T * A = A^T * W = 0
      W'*A 
      A'*W
%--------------------------------------------
% (3) Q*Q'+W*W' - eye(5) =? 0
      Q*Q'+W*W' - eye(5)
% since S = [Q W] is orthogonal:
%   S'*S = I = S*S' = [Q W]*[Q']  = ([Q 0] + [0 W])*( [ Q'] + [ 0 ]
%                           [W']                      [ 0 ]   [ W'])
%     ==> I = Q*Q' + W*W'
%     Q*Q'*A-A =? 0
      Q*Q'*A-A
% since Q projects onto R(A), it leaves R(A) unchanged 
%--------------------------------------------
% (4) Q*Q'*b =? b
      b = A*rand(5,1);
      Q*Q'*b - b
% this also establishes that any element in R(A) projects onto itself
%--------------------------------------------
% (5) c in R5:  Q*Q'*c = ? q, proj onto R(A)
      c = rand(5,1);
      rq =  c - Q*Q'*c; % ortho. of R(A) (should be in N(A'))
      A'*rq  % should be zero if rc in N(A')
% here an arbitrary vector c is projected onto R(A); the difference
% between c and its projection onto R(A), c-Q*Q'*c, should be in the
% orthogonal complement to R(A), i.e. in N(A')
%--------------------------------------------
% (6) W*W'*c = ? projection onto N(A');
      rw = W*W'*c;
      rq - rw          % they should be identical
% here W*W' projects onto N(A'); projecting the random vector c directly
% onto N(A') should give the same result as subtracting out its projection
% to R(A), the orthogonal complement to N(A'):
%      c = W*W'*c + Q*Q'*c
% gives the unique decomposition of c into a sum of elements of N(A') and R(A)
%--------------------------------------------
% (7) Y = orth(A'); then Y*Y' is projection onto R(A^T)
      Y = orth(A');
      U = Y*Y';
      y = U*b;
      y-U*y
% since y is the projection onto R(A^T), further projection won't change it
      s = b - y;
      A*s
% subtracting from b its projection onto R(A^T) leaves s = b-y
% as an element of N(A), the orthogonal complement of R(A^T).
%     hence A*s = 0
%--------------------------------------------
% (8) Z = null(A).
      Z = null(A);
      V = Z*Z';
      V*b-s
% since we found above that s is the projection of b onto N(A),
% it is not surprising that projecting directly with the help of Z
% gives the same result.
%--------------------------------------------
%--------------------------------------------
% MATLAB OUTPUT (pasted from command window)
%--------------------------------------------
>> A = rand(5,2)*rand(2,5)

A =

    1.1883    1.4384    0.4767    1.5876    1.0709
    0.5037    0.5500    0.2259    0.6348    0.5028
    0.3881    0.5731    0.1145    0.5846    0.2655
    0.9496    1.0544    0.4189    1.2078    0.9334
    0.9007    1.1499    0.3375    1.2415    0.7631

>> rank(A)

ans =

     2

>> Z = null(A)

Z =

    0.6976    0.4454    0.3132
    0.0988   -0.6320    0.2030
    0.3697   -0.3303   -0.7760
   -0.4462    0.4915   -0.3984
   -0.4098   -0.2271    0.3158
>> rank(Z)

ans =

     3
%--------------------------------------------
>>       Q = orth(A)

Q =

   -0.6335    0.0430
   -0.2615   -0.3480
   -0.2191    0.6465
   -0.4950   -0.5487
   -0.4872    0.3975

>> W = null(A')

W =

    0.7328    0.0159   -0.2442
    0.0928   -0.1111    0.8886
   -0.1724   -0.7000    0.1191
   -0.5040   -0.2862   -0.3436
   -0.4131    0.6445    0.1361

>> S = [Q W]

S =

   -0.6335    0.0430    0.7328    0.0159   -0.2442
   -0.2615   -0.3480    0.0928   -0.1111    0.8886
   -0.2191    0.6465   -0.1724   -0.7000    0.1191
   -0.4950   -0.5487   -0.5040   -0.2862   -0.3436
   -0.4872    0.3975   -0.4131    0.6445    0.1361

>> S*S'-eye(5)

ans =

   1.0e-15 *

   -0.4441   -0.2446    0.3627   -0.3358    0.2707
   -0.2446    0.2220   -0.3750    0.2423   -0.2648
    0.3627   -0.3750   -0.1110   -0.0838   -0.2484
   -0.3358    0.2423   -0.0838    0.2220   -0.1027
    0.2707   -0.2648   -0.2484   -0.1027   -0.3331

% establishes S is orthogonal
>> W'*A

ans =

   1.0e-15 *

   -0.0912   -0.0165   -0.0423   -0.0151   -0.1279
   -0.0794   -0.0329   -0.0309   -0.0948   -0.0463
   -0.1850   -0.3034   -0.0790   -0.3025   -0.1844

>> A'*W

ans =

   1.0e-15 *

   -0.0912   -0.0794   -0.1850
   -0.0165   -0.0329   -0.3034
   -0.0423   -0.0309   -0.0790
   -0.0151   -0.0948   -0.3025
   -0.1279   -0.0463   -0.1844
%--------------------------------------------
>> Q*Q'+W*W' - eye(5)

ans =

   1.0e-15 *

   -0.4441   -0.2498    0.3608   -0.3331    0.2776
   -0.2498    0.2220   -0.3608    0.2220   -0.2654
    0.3608   -0.3608   -0.1110   -0.0833   -0.2776
   -0.3331    0.2220   -0.0833    0.2220   -0.1041
    0.2776   -0.2654   -0.2776   -0.1041   -0.2220

>> Q*Q'*A-A

ans =

   1.0e-15 *

   -0.4441   -0.6661   -0.2220   -0.6661   -0.4441
   -0.1110   -0.2220   -0.0278   -0.2220         0
   -0.1665   -0.1110   -0.0694   -0.2220   -0.1665
   -0.3331   -0.4441   -0.1110   -0.4441   -0.2220
   -0.2220   -0.2220   -0.1110   -0.2220   -0.2220

%--------------------------------------------
>> b = A*rand(5,1);
      Q*Q'*b - b

ans =

   1.0e-15 *

   -0.4441
   -0.1110
   -0.1665
   -0.3331
   -0.2220
%--------------------------------------------
>> c = rand(5,1);
      rq =  c - Q*Q'*c;
>> A'*rq

ans =

   1.0e-15 *

    0.6616
    0.8091
    0.2150
    0.8671
    0.5037
%--------------------------------------------
>> rw = W*W'*c;
      rq - rw

ans =

   1.0e-15 *

    0.2220
   -0.1665
    0.4996
   -0.2776
    0.5551
%--------------------------------------------
>> Y = orth(A');
      U = Y*Y';
      y = U*b;
      y-U*y

ans =

   1.0e-15 *

    0.1110
         0
    0.0555
         0
    0.1110

>>s = b - y;
  A*s

ans =

   1.0e-15 *

    0.2567
    0.1457
    0.0651
    0.2443
    0.1761
%--------------------------------------------
>>Z = null(A);
      V = Z*Z';
      V*b-s

ans =

   1.0e-15 *

   -0.3886
   -0.2220
   -0.0416
    0.4510
   -0.1388
%--------------------------------------------
%-----------------------------------------------------------------------
