next up previous index
Next: Givens Rotations Up: The linear equation solving Previous: Rectangular system: QR decomposition   Index

Examples

Least Squares in Two different ways:
>> t = (1900:10:1990)';
>> y = [75.995 91.972 105.711 123.203 131.669 ...
     150.697 179.323 203.212 226.505 249.633]';
>> X=[t ones(10,1)] 
X =     1900           1
        1910           1
        1920           1
        1930           1
        1940           1
        1950           1
        1960           1
        1970           1
        1980           1
        1990           1
>> bhat=X\p
bhat =    1.93
      -3594.01
>>yhat=polyval(bhat,t);
>>[polyval(bhat,t) y]
ans =    67.08         76.00
         86.35         91.97
        105.62        105.71
        124.89        123.20
        144.16        131.67
        163.43        150.70
        182.70        179.32
        201.96        203.21
        221.23        226.50
        240.50        249.63

>> norm(yhat-y)
ans =
   23.5794
>> [Q,RC]=qr([X y]);
RC =
      -6151.30         -3.16       -488.86
             0         -0.05        167.82
             0             0         23.58
             0             0             0
             0             0             0
             0             0             0
             0             0             0
             0             0             0
             0             0             0
             0             0             0
>>  R1=RC(1:2,1:2)
R1 =    -6151.30         -3.16
              0          -0.05
>> C1=RC(1:2,3)
C1 = -488.8643
      167.8181
>> R1\C1
ans =     1.93
      -3594.01
>> C2=RC(3,3)
C2 = 23.5794
>>[Q*[C1;zeros(8,1)] yhat]
ans      67.08         67.08
         86.35         86.35
        105.62        105.62
        124.89        124.89
        144.16        144.16
        163.43        163.43
        182.70        182.70
        201.96        201.96
        221.23        221.23
        240.50        240.50



Susan Holmes 2002-01-12