Demo #9
A Gauss Elimination Tool

This notebook manually performs Gauss-Jordan elimination on a matrix A and prints out the matrix resulting from each intermediate step in the process.  This should help students to check the accuracy of their hand calculations.  To use the program,  replace the current definition of A (below) with the matrix you want row-reduced, and evaluate.  Note that the matrix A is an augmented matrix, i.e. it is the coefficient matrix augmented by the right-hand-side column vector.  Also, note that this routine switches rows when a zero appears on the main diagonal.

Brian Knowlton
ChE 50B
Chemical Engineering, UC Santa Barbara
created:  8/4/97
revised: 1/7/98
revised for Eng 5A by G. Fredrickson: 10/27/98
revised for Eng 5A by D. Pine: 10/25/00

$Post := If[MatrixQ[#1], MatrixForm[#1], #1] &

A = {{2, 3, 2, 1}, {3, 2, 3, 6}, {5, 7, 4, 3}}

storeA = A;
div = "----------------------------------";
{nrows, ncols} = Dimensions[A];
n = Min[nrows, ncols];
\(Do[
{
For[p = k + 1, 
          A[[k, k]] == 
            0, \(p++\), 
{
A = 
              A[[Flatten[{Range[1, k - 1], 
                    Range[k + 1, nrows], k}]]], 
            Print[div], 
            Print["matrix after placing row ", k, " at last row"], 
            Print[MatrixForm[
                A]]
}], 
A[[k]] /= 
          A[[k, 
            k]], 
Print[
          div], 
Print["matrix after introducing leading one in row ", k], Print[MatrixForm[A]], 
        Do[{mult = 
              A[[i, k]]\/A[[k, k]], 
            A[[i]] -= 
              mult\ A[[k]], Print[div], 
            Print["matrix after reducing row ", i, " using row ", k], 
            Print[MatrixForm[A]]}, {i, k + 1, n}], Null}, {k, 1, 
        n}]\)
\(Do[{mult = 
          A[[i, k]]\/A[[k, k]], 
        A[[i]] -= 
          mult\ A[[k]], Print[div], 
        Print["matrix after reducing row ", i, " using row ", k], 
        Print[MatrixForm[A, 
            TableAlignments -> Center]]
}, {k, n, 
        1, \(-1\)}, {i, 1, k - 1}]\)

Print["For comparison purposes, RowReduce gives:"]
RowReduce[storeA]