Exercise 3.5 Solutions

3a.

A=(({{-5, 2, -1}, {1, 0, 3}, {3, 1, 6}}))

LandU[A, 3]

4b.

A=(({{1, -2, 7}, {4, 2, 1}, {2, 5, -2}}))

LandU[A, 3]

5.

L=(({{1, 0, 0, 0}, {1/4, 1, 0, 0}, {1/4, 2/3, 1, 0}, {1/4, 1/3, -1/2, 1}}))

U=(({{4, 8, 4, 0}, {0, 3, 3, -3}, {0, 0, 4, 4}, {0, 0, 0, 1}}))

ForeSub[L, {8, -4, 10, -4}, 4]

BackSub[U, {8, -2, 12, 2}, 4]

ForeSub[L, {28, 13, 23, 4}, 4]

BackSub[U, %, 4]

6.

A=(({{1, 1, 0, 4}, {2, -1, 5, 0}, {5, 2, 1, 2}, {-3, 0, 2, 6}}))

LandU[A, 4]

Functions Used in this notebook

<< LinearAlgebra`MatrixManipulation`

$Post=If[MatrixQ[#],MatrixForm[#] ,#]&;

rowadd[scalar_,r1_,r2_,mat_]:=
	Module[{result,mat1=mat},
  	mat1[[r2]]=(scalar mat1[[r1]])+mat1[[r2]];
	mat1
	]

rowmultiply[scalar_,r1_,mat_]:=
	Module[{mat1=mat},
	mat1[[r1]]=scalar mat1[[r1]];
	mat1
	]

rowexchange[r1_,r2_,mat_]:=
	Module[{mat1=mat,temp},
	temp=mat1[[r1]];
	mat1[[r1]]=mat[[r2]];
	mat1[[r2]]=temp;
	mat1
	]

GaussJordan[A0_, 
        n_]:=
Module[ {A = A0, i, 
          p},  
Print[ 
          MatrixForm[A] ];  
For[ p = 1,  
          p ≤ 
            n,  p++,  
(Subscript[A, [[p]]])  =  ((Subscript[A, [[p]]])/(Subscript[A, [[p, p]]]));  
For[ i = 1,  
            i ≤ 
              n,  i++,  
If[ 
                i ≠ 
                  p,  
(Subscript[A, [[i]]])  =  
                  (Subscript[A, [[i]]])  -  (Subscript[A, [[i, p]]]) 
                      (Subscript[A, [[p]]])  ]; ];  
Print[
            MatrixForm[A]];  ];  ]

\(\(\(TriDiagonal[a0_, d0_, c0_, 
          b0_]\  := 
Module[{\ a = a0, b = b0, c = c0, 
            d = d0, k, m, n = Length[b0], x, 
            showmat\ }, 
showmat = 
            DiagonalMatrix[d]; 
For[i = 1, 
            i ≤ n - 1, \(i++\), 
            showmat[\([i, i + 1]\)] = c[\([i]\)]; 
            showmat[\([i + 1, i]\)] = a[\([i]\)];]; 
          Print[MatrixForm[showmat]]; 
Print[
            MatrixForm[b]]; 
For[\ k = 2, \ 
            k ≤ 
              n, \ \(k++\), 
d\_\(\([[\)\(k\)\(]]\)\)\  = \ d\_\(\([[\)\(k\)\(]]\)\)\ \  - \ \(\(\ \)\(a\_\(\([[\)\(k - 1\)\(]]\)\)\)\)\/\(\(\ \)\(d\_\(\([[\)\(k - 1\)\(]]\)\)\)\)\ \ c\_\(\([[\)\(k - 1\)\(]]\)\); \ 
            Print["k=", 
              k, \*"" (Subscript[d, [[k]]])="", 
              d\_\(\([[\)\(k\)\(]]\)\)\ ]; 
b\_\(\([[\)\(k\)\(]]\)\)\  = \ 
              b\_\(\([[\)\(k\)\(]]\)\)\  - \ \(\(\ \)\(a\_\(\([[\)\(k - 1\)\(]]\)\)\)\)\/\(\(\ \)\(d\_\(\([[\)\(k - 1\)\(]]\)\)\)\)\ b\_\(\([[\)\(k - 1\)\(]]\)\); \ 
            Print[\*""(Subscript[b, [[k]]]) ="", 
              b\_\(\([[\)\(k\)\(]]\)\)\ ]; 
showmat[\([k, k]\)] = d[\([k]\)]; 
            showmat[\([k, k - 1]\)] = 0; Print[MatrixForm[showmat]]; 
            Print[MatrixForm[b]]]; \ 
x = 
            Table[0, {n}]; \ 
x\_\(\([[\)\(n\)\(]]\)\)\  = \(\(\ \)\(b\_\(\([[\)\(n\)\(]]\)\)\)\)\/d\_\(\([[\)\(n\)\(]]\)\); \ 
For[\ k = n - 1, \ 
            1 ≤ 
              k, \ \(k--\), 
\(x\_\(\([[\)\(k\)\(]]\)\)\  = \ \(b\_\(\([[\)\(k\)\(]]\)\)\  - \ c\_\(\([[\)\(k\)\(]]\)\)\ x\_\(\([[\)\(k + 1\)\(]]\)\)\)\/d\_\(\([[\)\(k\)\(]]\)\);\)\ ]; \ 
Return[x];\ ];\)\(\ \)\)\)

\(\(\(BackSubDetails[\ a0_, b0_, 
          n_\ ] := 
Module[\ {\ a = a0, b = b0, i, j, x, 
            X\ }, 
x\  = \ 
            Table["x"\_i, {i, 1, n}]; \ 
X\  = \ 
            Table["x"\_i, {i, 1, n}]; \ 
For[\ i = n, \ 
            1 ≤ i, \ \(i--\), 
Print[
              Flatten[Chop[
                      a\_\(\([[\)\(i\)\(]]\)\)] . X] == 
                b\_\(\([[\)\(i, 1\)\(]]\)\)]; 
If[i < n, 
              Print[Flatten[
                      Chop[a\_\(\([[\)\(i\)\(]]\)\)]] . Flatten[x] == 
                  b\_\(\([[\)\(i, 1\)\(]]\)\)]; 
Print[\(a\_\(\([[\)\(i, i\)\(]]\)\)\) 
                    x\_\(\([[\)\(i\)\(]]\)\) == \((b\_\(\([[\)\(i\)\(]]\)\) - ∑\+\(j = \ i + 1\)\%n\( a\_\(\([[\)\(i, j\)\(]]\)\)\) x\_\(\([[\)\(j\)\(]]\)\))\)\_\(\([[\)\(1\)\(]]\)\)];]; 
x\_\(\([[\)\(i\)\(]]\)\)\  = \ \(b\_\(\([[\)\(i\)\(]]\)\) - ∑\+\(j = \ i + 1\)\%n\( a\_\(\([[\)\(i, j\)\(]]\)\)\) x\_\(\([[\)\(j\)\(]]\)\)\)\/a\_\(\([[\)\(i, i\)\(]]\)\); \ 
Print["x"\_i, " = ", 
              x\_\(\([[\)\(i, 1\)\(]]\)\), "
"]]; \ 
Return[
            x];\ ];\)\(\ \)\)\)

ForeSub[ a0_, b0_, 
          n_ ] := 
Module[ { a = a0,  b = b0,  i, j , 
            x}, 
x  =  
            Table[0, {n}];  
For[ i = 1,  
            i ≤ 
              n,  i++, 
(Subscript[x, [[i]]])  =  (((Subscript[b, [[i]]]) - (∑_(j =  1)^(i - 1))(Subscript[a, [[i, j]]]) (Subscript[x, [[j]]]))/(Subscript[a, [[i, i]]])) ];  
Return[
            x]; ];

BackSub[ a0_, b0_, 
          n_ ] := 
Module[ { a = a0,  b = b0,  i, j, 
            x }, 
x  =  
            Table[0, {n}];  
For[ i = n,  
            1 ≤ 
              i,  i--, 
(Subscript[x, [[i]]])  =  (((Subscript[b, [[i]]]) - (∑_(j =  i + 1)^n) (Subscript[a, [[i, j]]]) (Subscript[x, [[j]]]))/(Subscript[a, [[i, i]]])); ];  
Return[
            x]; ];

LandUmod[A0_, 
        n_] := 
Module[ {A = A0, B = B0, i, 
          p},  
U = A0;  
L = 
          IdentityMatrix[n];  
For[ p = 1,  
          p ≤ 
            n - 1,  p++,  
For[ i = p + 1,  
              i ≤ n,  i++,  
m = 
                ((Subscript[A, [[i, p]]])/(Subscript[A, [[p, p]]])); 
(Subscript[L, [[i, p]]]) = 
                m;  
(Subscript[A, [[i]]])  =  
                (Subscript[A, [[i]]])  -  m (Subscript[A, [[p]]]); 
U  =  A;  
Print[
                MatrixForm[U]];  ]; ]; 
        Print[N[ 
            BackSub[U, Transpose[U][[n + 1]], n]]] ]

\(\(\(LandU[A0_, 
        n_]\)\(:=\)\(
\)\(Module[\ {A = A0, i, 
          p}, \ 
U = A0; \ 
L = 
          IdentityMatrix[n]; \ 
Print[MatrixForm[L], 
          MatrixForm[U], " = ", 
          MatrixForm[A0]]; \ 
For[\ p = 1, \ 
          p ≤ 
            n - 1, \ \(p++\), \ 
\(For[\ i = p + 1, \ 
              i ≤ n, \ \(i++\), \ 
m = 
                A\_\(\([[\)\(i, p\)\(]]\)\)\/A\_\(\([[\)\(p, p\)\(]]\)\); 
L\_\(\([[\)\(i, p\)\(]]\)\) = 
                m; \ 
A\_\(\([[\)\(i\)\(]]\)\)\  = \ 
                A\_\(\([[\)\(i\)\(]]\)\)\  - \ m\ A\_\(\([[\)\(p\)\(]]\)\); 
U\  = \ A; \ 
Print[MatrixForm[L], 
                MatrixForm[U], " = ", 
                MatrixForm[A0]];\ \ ];\)\ \ ];\ \ ]\)\(\ \)\)\)