# Using Two Dimensional Arrays: System of Simultaneous Equations

• Introduction

• Examples of systems of simultaneous equations:

 ``` 3 x1 + 7 x2 = 78 8 x1 + 5 x2 = 45 ```

And:

 ``` 5 x1 + 6 x2 - 3 x3 = 23 3 x1 - 4 x2 + 8 x3 = 25 -6 x1 + x2 - 2 x3 = 19 ```

• This webpage shows you:

• a technique called the "Gaussian elmination" to solve systems of simultaneous equations.

• a computer program using a two-dimensioanl array that implements the "Gaussian elmination" method.

You can use this program to solve any systems of simultaneous equations...

• Two invariance rules for manipulating equations

• Consider the following system of equations:

 ``` 2 x1 + 4 x2 = 16 3 x1 + 2 x2 = 12 ```

• The equality (=) means that the values in the left hand side and right hand side of "=" are in "balance".

• The systems of equations above can be represented as follows:

 ``` 2 x1 + 4 x2 = 16 3 x1 + 2 x2 = 12 ```

• Invariance Rule 1: if we multiply or divide both sides by the same factor, the equality will still hold.

Example: multiply first equation by 2 and second equation by 3

 ``` 0.5 × (2 x1 + 4 x2) = 0.5 × 16 ==> x1 + 2 x2 = 8 3 x1 + 2 x2 = 12 ```

• Invariance Rule 2: you can add or subtract a multiple of one equation to a second equation (the left hand side to left hand side and the right hand side to the right hand side) then the result is equal to each other.

Example: subtract three time the first equation to the second equation

 ``` x1 + 2 x2 = 8 (1) 3 x1 + 2 x2 = 12 (2) (2) ===> 3 x1 + 2 x2 = 12 3 x (1) ===> 3 x1 + 6 x2 = 24 - --------------------------- 0 x1 - 4 x2 = -12 ```

• The Gaussian Elimination method

• The Gaussian Elimination method use these two invariance rules to reduce a system of simultaneous equations to a "trivial" system.

The "trivial" system of simultaneous equations is the following system:

 ``` x1 + 0 × x2 = ... 0 × x1 + x2 = ... ```

It's called "trivial" because solving this system requires no effort at all.

• Example:

 ``` 2 x1 + 4 x2 = 16 3 x1 + 2 x2 = 12 ```

(a) Multiply first equation by 0.5 (or divide by 2) - intermediate result:

 ``` 1 x1 + 2 x2 = 8 3 x1 + 2 x2 = 12 ```

(b) Subtract three time equation 1 from equation 2 - intermediate result:

 ``` 1 x1 + 2 x2 = 8 0 x1 - 4 x2 = -12 ```

(a) Multiply second equation by -4 - intermediate result:

 ``` 1 x1 + 2 x2 = 8 0 x1 + 1 x2 = 3 ```

(b) Subtract 2 times equation 2 from equation 1 - intermediate result:

 ``` 1 x1 + 0 x2 = 2 0 x1 + 1 x2 = 3 ```

Trivial system obtained. Solution:

 ``` x1 = 2 x2 = 3 ```

• Understanding the Gaussian Elimination method

• Here is a larger example to show you the steps that the Gaussian Elimination method makes:

 ``` 2 x1 + 4 x2 + 6 x3 = 14 x1 + 4 x2 + 3 x3 = 3 3 x1 + 5 x2 + 2 x3 = 2 ```

• Row 1:

• Determine pivot: pivot = coef[1][1] = 2

 ``` 2 x1 + 4 x2 + 6 x3 = 14 x1 + 4 x2 + 3 x3 = 3 3 x1 + 5 x2 + 2 x3 = 2 ```

• Divide row 1 by pivot coefficient [1][1] (= 2) ("normalize"):

 ``` 1 x1 + 2 x2 + 3 x3 = 7 x1 + 4 x2 + 3 x3 = 3 3 x1 + 5 x2 + 2 x3 = 2 ```

• Make the other coefficients in column 1 zero by ("sweeping"):

• subtract 1 times row 1 from row 2 (because coefficient [1][2] is 1)
• subtract 3 times row 1 from row 3 (because coefficient [1][3] is 3)

 ``` 1 x1 + 2 x2 + 3 x3 = 7 --+ -----+ | -1 | -3 x1 + 4 x2 + 3 x3 = 3 <-+ | | 3 x1 + 5 x2 + 2 x3 = 2 <------------+ ```

Result:

 ``` 1 x1 + 2 x2 + 3 x3 = 7 + 2 x2 + = -4 - x2 - 7 x3 = -19 ```

• Row 2:

• Determine pivot: pivot = coef[2][2] = 2

 ``` x1 + 2 x2 + 3 x3 = 7 + 2 x2 + = -4 - x2 - 7 x3 = -19 ```

• Divide row 2 by pivot coefficient [2][2] (= 2) ("normalize"):

 ``` x1 + 2 x2 + 3 x3 = 7 + 1 x2 + = -2 - x2 - 7 x3 = -19 ```

• Make the other coefficients in column 2 zero by "sweeping":

• subtract 2 times row 2 from row 1 (because coefficient [2][1] is 2)
• subtract -1 times row 2 from row 3 (because coefficient [2][3] is -1)

 ``` x1 + + 3 x3 = 11 + 1 x2 + = -2 - 7 x3 = -21 ```

• Row 3:

• Determine pivot: pivot = coef[3][3] = -7

 ``` x1 + + 3 x3 = 11 + x2 + = -2 - 7 x3 = -21 ```

• Divide row 2 by pivot coefficient [3][3] (= -7) ("normalize"):

 ``` x1 + + 3 x3 = 11 + x2 + = -2 + 1 x3 = 3 ```

• Make the other coefficients in column 3 zero by "sweeping":

• subtract 3 times row 3 from row 1 (because coefficient [3][1] is 3)
• subtract 0 times row 3 from row 2 (because coefficient [3][2] is 0)

 ``` x1 + + = 2 + x2 + = -2 + 1 x3 = 3 ```

• Solution:

 ``` x1 = 2 x2 = -2 x3 = 3 ```

• Information needed in the Gaussian Elmination Algorithm

• Representing (describing) the system of equations:

 ``` Initial system of equations: 2 x1 + 4 x2 + 6 x3 = 14 x1 + 4 x2 + 3 x3 = 3 3 x1 + 5 x2 + 2 x3 = 2 Will be eventually reduced to the trivial system: 1 x1 + 0 x2 + 0 x3 = 2 0 x1 + 1 x2 + 0 x3 = -2 1 x1 + 0 x2 + 1 x3 = 3 ```

Clearly, when you obtained the trivial system, the solution for x1, x2 and x3 can be "read" off the right hand side !

• We can conclude that the information needed to compute the solution are:

1. The coefficients of each unknown in all equations.
2. The right hand side

• You don't need to store the unknowns x1, x2 and x3

• Storing the information for the Gaussian algorithm:

 ``` double[][] A; // The Coefficient "matrix" double[] b; // The right hand side "vector" int n; // number of unknowns (and size of A and b) ```

• Example:

 ``` System of equations: 2 x1 + 4 x2 + 6 x3 = 14 x1 + 4 x2 + 3 x3 = 3 3 x1 + 5 x2 + 2 x3 = 2 ``` ``` n = 3; A = new double[n][n]; b = new double[n]; A[0][0] = 2; A[0][1] = 4; A[0][2] = 6; b[0] = 14; A[1][0] = 1; A[1][1] = 4; A[1][2] = 3; b[0] = 3; A[2][0] = 3; A[2][1] = 5; A[2][2] = 2; b[0] = 2; ```

• Programming the Gaussian Elmination Algorithm in Java

• From the above discussion, we have the following very high level description of the Gaussian Elimination algorithm:

 ``` Read in n, A and b; for row i from 0 to n-1 { (1) Normalize row i: pivot = A[i][i]; divide coefficients in row i by pivot; (2) Sweep other rows using row i: for row k from 0 to n-1, except row i { factor = A[k][i]; Subtract "factor" times row i from row k } } ```

• This translates into the following Java statements:

 ``` Read in n, A and b; int i, j, k; double pivot, factor; for ( i = 0; i < n; i++ ) { // (1) Normalize row i: pivot = A[i][i]; for (j = 0; j < n; j++) A[i][j] = A[i][j] / pivot; // divide coeff. in row i by pivot; b[i] = b[i] / pivot; // (2) Sweep other rows using row i: (do NOT sweep row i !) for ( k = 0; k < n; k++ ) { if ( k != i ) { factor = A[k][i]; for j = 0; j < n; j++) A[k][j] = A[k][j] - factor*A[i][j]; // Subtract "factor" times row i from row k b[k] = b[k] - factor*b[i]; } } } ```

• Example Program: (Demo above code)