Demo #8
Gaussian elimination using Mathematica

Objectives:
	- define  matrices in Mathematica
	- format the output of matrices
	- apply linear algebra to solve a real problem
	- Use Mathematica to perform Gauss-Jordan elimination on a matrix
	
	Brian Knowlton
	ChE 50B, Chemical Engineering Department, UC Santa Barbara
	created: 1/7/98
	revised: 1/7/98
	revised by G. Fredrickson for Eng 5A: 10/27/98

1.  Matrices in Mathematica

In Mathematica, matrices are called Lists.  A List is just a collection of numbers, separated by commas, and surrounded by curly braces { }.  For example, the following defines a 1x3 matrix.

u  =  { 1,  2,  3 }

You can also use variables in matrices.

v  =  {x,  y,  z}

The elements of a List don't have to be numbers or variables; they can also be other lists.  For example, a 2x5 matrix can be thought of as a 2x1 List, where both elements are 1x5 Lists.  Therefore, to specify a 2x5 matrix in Mathematica, specify a two element List, where each element is a five element List.  This type of List is also referred to as a nested List, since the five element Lists reside or are 'nested' in another List.

A = {   {a11, a12, a13, a14,  b1}, {a21, a22, a23, a24,  
          b2}  }

The matrix A might be an augmented matrix derived from a pair of linear equations in 4 unknowns,

(Subscript[a, 11]) (Subscript[x, 1]) + (Subscript[a, 12]) (Subscript[x, 2]) + (Subscript[a, 13]) (Subscript[x, 3]) + (Subscript[a, 14]) (Subscript[x, 4]) = (Subscript[b, 1])
(Subscript[a, 21]) (Subscript[x, 1]) + (Subscript[a, 22]) (Subscript[x, 2]) + (Subscript[a, 23]) (Subscript[x, 3]) + (Subscript[a, 24]) (Subscript[x, 4]) = (Subscript[b, 2])

where (Subscript[a, ij]) are coefficients multiplying the unknowns (Subscript[x, i]).

The matrices u and v  are called one-dimensional matrices, where A is a two-dimensional matrix.  You can also specify three or higher dimensional matrices in Mathematica by further nesting of Lists, and although we will not use these in Eng 5A, they are useful mathematical structures in various branches of physics and engineering.

2.  Easy-To-Read Matrix  Output

The format that Mathematica uses for matrix output is a little difficult to read.  Mathematica provides a function, MatrixForm, that produces more readable output.

MatrixForm[A]

3.  Applying Linear Algebra to a Real Problem

In Figure 1 is a circuit diagram, representing a collection of typical household appliances, connected (perhaps unsafely) to a single wall outlet.  The power that each appliance uses is listed in the figure, along with the voltage provided by the outlet.  The problem is to predict the amount of current that is required to run all the appliances at once.  This information can be important, since if too much current is drawn from the outlet, a fuse will fail and you will lose to power to all appliances.

Figure 1:  Circuit Diagram of Common Household Appliances

In order to find the current passing through the outlet, we need to find the current running through each appliance and the voltage at various places in the circuit.

The first step in analyzing this circuit is to find the resistance of each appliance.  Using the information supplied by the manufacturer, the resistance for each appliance can be determined.  Figure 2 shows the resistance of each appliance, and also the remaining unknown currents and voltages.

Figure 2: Appliance Resistances and Labeling Notation of Unknowns

To find the unknowns, we use two physical laws to write down linear equations describing the behavior of this circuit.  First, at each point where the circuit branches or joins (points A or C), we employ a "balancing equation" of currents.  Because current is just the movement of electrons, the total current entering any given point must equal the total current flowing out.  Using the notation in Figure 2, we can write the following two (redundant) equations.

(Subscript[I, 1])=  (Subscript[I, 2])  +  (Subscript[I, 3])
(Subscript[I, 2])+  (Subscript[I, 3])  =  (Subscript[I, 1])

Equations can be written using Ohm's Law, which describes how the electric potential drops when current flows through a resistor.  Ohm's Law says that the voltage drop across a resistor is proportional to the current flowing through the resistor, where the constant of proportionality is the resistance.  As an equation, Ohm's Law can be written as,

ΔV = IR

Applying this law to each of the appliances in Figure 2, we can write five more equations,

120  -   (Subscript[V, 1])    =       12 (Subscript[I, 1])
(Subscript[V, 1])   -   (Subscript[V, 2])    =         9 (Subscript[I, 3])
(Subscript[V, 2])   -   (Subscript[V, 3])    =     360 (Subscript[I, 3])
(Subscript[V, 1])   -   (Subscript[V, 3])    = (32/3) (Subscript[I, 2])
(Subscript[V, 3])   -     0     = (32/3) (Subscript[I, 1])

In total, we have seven equations in six unknowns.  Written in a more standard form, the equations look like the following.

(Subscript[I, 1])                   -  (Subscript[I, 2])             -  (Subscript[I, 3])                                                =  0
    - (Subscript[I, 1])                    +  (Subscript[I, 2])             +  (Subscript[I, 3])                                                =  0
   12 (Subscript[I, 1])                                                      +  (Subscript[V, 1])                                =  120
                                                  - 9 (Subscript[I, 3])      +  (Subscript[V, 1])    -   (Subscript[V, 2])                 =  0
                                               -360 (Subscript[I, 3])                    +   (Subscript[V, 2])  -   (Subscript[V, 3])    =  0
                     - (32/3) (Subscript[I, 2])                           +  (Subscript[V, 1])                 -   (Subscript[V, 3])    =  0     
 - (32/3) (Subscript[I, 1])                                                                         +    (Subscript[V, 3])    =  0

NOTE:  We have chosen here to include the redundant equation.   In many applications, the fact that two equations are redundant will not be so obvious.  Also, you will see that including a redundant equation will not affect the solution to the problem (see below).

4.  Using Mathematica To Perform Gauss-Jordan Elimination

Using Mathematica, we can write the equations into an augmented matrix:

\(\(A = {\ \ {\ \ 1, \ \(-1\), \(-1\), 0, 0, 0, 
            0}, 
		\ \ \ \ \ \ \ {\(-1\), 1\ \ , 1, 0, 0, 0, 
            0}, 
		\ \ \ \ \ \ \ {12, 0, 0, 1, 0, 0, 
            120}, 
		\ \ \ \ \ \ \ {\ \ 0, 0, \(-9\), 1, \(-1\), 0, 
            0}, 
		\ \ \ \ \ \ \ \ {\ 0, 0, \(-360\), 0, 1, \(-1\), 
            0}, 
\ \ \ \ \ \ \ \ \ \ \ \ {\ \ 0\ , \(-32\)/3, 0, 1, 
            0, \(-1\), 0}, 
		\ \ \ \ \ \ \ \ {\(-32\)/3, 0, 0, 0, 0, 1, 
            0}\ \ \ \ \ \ \ \ \ };\)			\)
MatrixForm[A]

To perform Gauss-Jordan Elimination on this augmented matrix, we can call the already prepared Mathematica function RowReduce, which performs Gauss-Jordan elimination, producing a matrix in reduced row-echelon form.

B = RowReduce[A];
MatrixForm[B]

N[ MatrixForm[B]]

The final answer is:

(Subscript[I, 1])  =   3.633;
(Subscript[I, 2])  =   3.531;
(Subscript[I, 3])  =   0.102;
(Subscript[V, 1])  = 76.408;
(Subscript[V, 2])  = 75.490;
(Subscript[V, 3])  = 38.748;

The total current required to run all the appliances at once is (Subscript[I, 1])or 3.6 Amperes.

NOTE:  The matrix B, the reduced matrix, has a row of zeros as the last row.   This row is the result of the redundant equation that we included in our system of equations.  Any equation that is not independant of the others will show up as a row of zeros, and can be neglected.

Appendix 1: An Even Better Way of Formatting Matrices

The use of the MatrixForm function is a rather cumbersome way to format matrix output, since you must call the function every time you output a matrix.  However, there is a way you can tell Mathematica to always print Lists in matrix form.  The following command, when evaluated, will cause Mathematica, by default, to use the MatrixForm function for output of Lists.

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

Typically, this command would come at the beginning of the notebook.  In order to restore the original formatting, simply clear the variable $Post.  See the following example for a demonstration.

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