|   | Output from imleqn.sas | Source 0 Graphs | 
|---|
NOTE: %INCLUDE (level 1) file n:\psy6140\examples\iml\imleqn.sas is file
      n:\psy6140\examples\iml\imleqn.sas.
416 +title 'IMLEQN: Solution of Systems of Equations'; 417 + *-- ------- ---------------------------------; 418 +proc iml;
IML Ready
419 + reset print log fuzz fw=5; 420 + %include iml(matlib);
NOTE: %INCLUDE (level 2) file IML(matlib) is file c:\sasuser\iml\matlib.SAS.
NOTE: Module R defined. NOTE: Module COFACTOR defined. NOTE: Module PROJ defined. NOTE: Module LEN defined. NOTE: Module DEV defined. NOTE: Module CORR defined. NOTE: %INCLUDE (level 2) ending. NOTE: %INCLUDE (level 1) resuming.
506  +*-- [1.] 3 EQUATIONS IN 3 UNKNOWNS (Consistent);
507  +A= {1 1 -4, 1 -2 1, 1 1 1};
                A             3 rows      3 cols    (numeric)
                                  1     1    -4
                                  1    -2     1
                                  1     1     1
508  +b= {2, 1, 0};
                B             3 rows      1 col     (numeric)
                                        2
                                        1
                                        0
509  +xx = t('X1' : 'X3');
           XX            3 rows      1 col     (character, size 2)
                                      X1
                                      X2
                                      X3
510 +print A '*' xx '=' b;
                            A               XX       B
                            1     1    -4 * X1 =     2
                            1    -2     1   X2       1
                            1     1     1   X3       0
511 +* they are consistent, since r(A) = r(A b); 512 +print (r(A)) (r(A || b));
                              #TEM1001 #TEM1003
                                     3        3
513 +*-- A solution exists: use inv() or solve(); 514 +x = inv(A) * b;
                X             3 rows      1 col     (numeric)
                                    0.733
                                    -0.33
                                     -0.4
515 +x = solve(A,b);
                X             3 rows      1 col     (numeric)
                                    0.733
                                    -0.33
                                     -0.4
516 +print A ' * ' x '=' (A * x) '=' b;
                    A                     X   #TEM1001       B
                    1     1    -4  *  0.733 =        2 =     2
                    1    -2     1     -0.33          1       1
                    1     1     1      -0.4          0       0
517 +*-- Echelon form of (A || b) shows solution; 518 +r = echelon(A || b);
                R             3 rows      4 cols    (numeric)
                               1     0     0 0.733
                               0     1     0 -0.33
                               0     0     1  -0.4
519  +*-- [2.] 4 EQUATIONS IN 3 UNKNOWNS (Consistent);
520  +A= {1 1 -4, 1 -2 1, 1 1 1, 2 -1 2};
                A             4 rows      3 cols    (numeric)
                                  1     1    -4
                                  1    -2     1
                                  1     1     1
                                  2    -1     2
521  +b= {2, 1, 0, 1};
                B             4 rows      1 col     (numeric)
                                        2
                                        1
                                        0
                                        1
522 +* they are consistent, since r(A) = r(A b); 523 +print (r(A)) (r(A || b));
                              #TEM1001 #TEM1003
                                     3        3
524 +*-- A solution exists, but more equations than unknowns, 525 + so use ginv; 526 +x = ginv(A) * b;
                X             3 rows      1 col     (numeric)
                                    0.733
                                    -0.33
                                     -0.4
527 +print A ' * ' x '=' (A * x) '=' b;
                    A                     X   #TEM1001       B
                    1     1    -4  *  0.733 =        2 =     2
                    1    -2     1     -0.33          1       1
                    1     1     1      -0.4          0       0
                    2    -1     2                    1       1
528 +*-- Echelon form of (A || b) shows solution; 529 +r = echelon(A || b);
                R             4 rows      4 cols    (numeric)
                               1     0     0 0.733
                               0     1     0 -0.33
                               0     0     1  -0.4
                               0     0     0     0
530  +*-- [3.] 3 EQUATIONS IN 3 UNKNOWNS (Inconsistent);
531  +A={1 3 1, 1 -2 -2, 2 1 -1};
                A             3 rows      3 cols    (numeric)
                                  1     3     1
                                  1    -2    -2
                                  2     1    -1
532  +b={2,3,6};
                B             3 rows      1 col     (numeric)
                                        2
                                        3
                                        6
533 +*-- inconsistent, since r(A) < r(A b); 534 +print (r(A)) (r(A || b));
                              #TEM1001 #TEM1003
                                     2        3
535 +r = echelon(A || b);
                R             3 rows      4 cols    (numeric)
                               1     0  -0.8     0
                               0     1   0.6     0
                               0     0     0     1
536 +*-- inv() fails, since A is singular; 537 +x = inv(A) * b;
ERROR: (execution) Matrix should be non-singular.
 operation : INV      at line   537 column   8
 operands  : A
A             3 rows      3 cols    (numeric)
     1     3     1
     1    -2    -2
     2     1    -1
 statement : ASSIGN          at line   537 column   1
538 +* g-inverse provides approx. solution; 539 +x = ginv(A) * b;
                X             3 rows      1 col     (numeric)
                                    1.947
                                     0.54
                                    -1.23
540 +print A '*' x ' = ' (A * x);
                        A                   X     #TEM1001
                        1     3     1 * 1.947  =     2.333
                        1    -2    -2    0.54        3.333
                        2     1    -1   -1.23        5.667
541  +*-- change rhs to give consistent set;
542  +b = {2,3,5};
                B             3 rows      1 col     (numeric)
                                        2
                                        3
                                        5
543 +r = r(A || b);
                R             1 row       1 col     (numeric)
                                        2
544 +r = echelon(A || b);
                R             3 rows      4 cols    (numeric)
                               1     0  -0.8   2.6
                               0     1   0.6  -0.2
                               0     0     0     0
545 +*-- since r(A) < ncol(a) solution is not unique; 546 +* but ginv finds a solution; 547 +x = ginv(A) * b;
                X             3 rows      1 col     (numeric)
                                     1.72
                                     0.46
                                     -1.1
548 +print A '*' x ' = ' (A * x) '=' b;
                    A                   X     #TEM1001       B
                    1     3     1 *  1.72  =         2 =     2
                    1    -2    -2    0.46            3       3
                    2     1    -1    -1.1            5       5
549  +*-- Equivalent way of producing a solution;
550  +A11 = A[{1 2},{1 2}];
                A11           2 rows      2 cols    (numeric)
                                     1     3
                                     1    -2
551  +A12 = A[{1 2}, 3];
                A12           2 rows      1 col     (numeric)
                                        1
                                       -2
552  +b1  = b[{1 2},];
                B1            2 rows      1 col     (numeric)
                                        2
                                        3
553 +x2 = -1.1;
                X2            1 row       1 col     (numeric)
                                     -1.1
554 +* solve for 2 independent unknowns; 555 +x1 = inv(A11) * b1 - inv(A11) * A12 * x2;
                X1            2 rows      1 col     (numeric)
                                     1.72
                                     0.46
556 +quit;
Exiting IML.
NOTE: The PROCEDURE IML used 0.38 seconds. NOTE: %INCLUDE (level 1) ending.