Processing math: 100%
m
 
Line 131: Line 131:
 
\end{array}\right] \to \left[\begin{array}{ccc|c}
 
\end{array}\right] \to \left[\begin{array}{ccc|c}
 
1 & 2 & 1 & 2\\  
 
1 & 2 & 1 & 2\\  
0 & 2 & -6 & 6\\  
+
0 & 2 & -2 & 6\\  
 
0 & 4 & 1 & 2
 
0 & 4 & 1 & 2
 
\end{array}\right] \to \left[\begin{array}{ccc|c}
 
\end{array}\right] \to \left[\begin{array}{ccc|c}
 
1 & 2 & 1 & 2\\  
 
1 & 2 & 1 & 2\\  
0 & 2 & -6 & 6\\  
+
0 & 2 & -2 & 6\\  
 
0 & 0 & 5 & -10
 
0 & 0 & 5 & -10
 
\end{array}\right]</math>
 
\end{array}\right]</math>

Latest revision as of 13:51, 21 April 2018

Gaussian Elimination

Gaussian Elimination or Row Reduction is a method for solving a System of Linear Equations

  • it corresponds to elimination of variables in the system
  • if a matrix A that we reduce is non-singular and invertible, then we always have a solution
  • a by-product of Gaussian Elimination is LU Factorization


Row Elimination

  • First, do the forward elimination to reduce the matrix to triangular
  • Then we do the back-substitution to find the solution
  • When we multiply a row on some constant c and subtract from any other row, we get an equivalent system


Elimination by Example

We have the following system with 3 equations and 3 unknowns:

{x+2y+z=23x+8y+z=124y+z=2

Matrix form:

  • [121381041]A[xyz]x=[2122]b


Forward elimination

So, first let's see how elimination works

  • i.e. concentrate only on the matrix A
  • A=[121381041]


Let's proceed

  • at each step
    • multiply the ith equation by the right number and subtract it from the equations below
    • s.t. there's 0 for the first column in all other rows
    • so we want to knock out the xi part of the equation, or "eliminate" xi
  • [121381041]. 1 is the 1st pivot
  • step 2.1: clean cell a21:
    • [121381041][121022041]
  • multiplying row 1 by 3 will knock out x from the 2nd equation
  • step 3.1: clean cell a21
    • already 0, continuing
  • [121022041], 2 is the 2nd pivot
  • step 3.2
    • [121022041][121022005]
  • [121022005], 5 - it's 3rd pivot


The matrix is now in the upper-triangular form U


When does it fail?

Not always we are able to do the forward elimination

0 at a Pivot position:

  • suppose the first pivot is 0:
  • [021381041]
  • But here it doesn't mean that we can't solve the system: we can switch the rows and continue:
  • [381021041]
  • so, if there's a 0 at the pivot position, try to exchange rows
  • if there's no rows with non-zero values at the pivot position - the elimination fails
    • there's no solution to the system


Augmented Matrix

But we shouldn't forget about b!

  • A augmented is [|a1||a2|...|an| | |b|]: matrix A with column b stacked to the right
  • A augmented: [1212381120412]
  • the right part b also changes as we go through elimination
  • [1212381120412][121202260412][1212022600510]
  • let c be the result of applying elimination to b


Back Substitution

After we forward-eliminated variables of augmented A, we can do back substitution:

  • Our matrix is

[1212026600510]

  • It means that our system is


{x+2y+z=22y2z=65z=10

Now we just go backwards and solve: first for z, then for y, and finally for x

  • we get z=2,y=1,x=2
  • it's easy to solve because the matrix is triangular


Elimination: Matrix Form

We can write these elimination steps in matrix form

  • [121381041]
  • the first step of the elimination was to take first and last rows unchanged, and subtract 3 times 1st row from the second.
  • Can we write it with Matrix Multiplication?
    • [100???001]×[121381041]
    • The first and last rows remain unchanged - hence we have [1?0] and [0?1]
    • what should we put instead of [? ? ?] so multiplication takes us from one matrix to another?
    • [100310001]
    • we want to subtract 3 times row 1 from row 2:
    • [100310001]×[121381041]=[121022041]
  • let E21 be the matrix that's used for step 2,1 of the elimination
    • E21A=[121022041]
  • we continue this way until we transform A to U
    • so we have E21(E21A)=U
    • or (E21E21)EA=EA=U
    • This a part of LU Factorization


What if we need to exchange rows?

Implementations

Python

Simple elimination with no permutation:


A = np.array([[60, 91, 26], [60, 3, 75], [45, 90, 31]], dtype='float')
b = np.array([1, 0, 0])

Ab = np.hstack([A, b.reshape(-1, 1)])

n = len(b)

for i in range(n):
    a = Ab[i]

    for j in range(i + 1, n):
        b = Ab[j]
        m = a[i] / b[i]
        Ab[j] = a - m * b

for i in range(n - 1, -1, -1):
    Ab[i] = Ab[i] / Ab[i, i]
    a = Ab[i]

    for j in range(i - 1, -1, -1):
        b = Ab[j]
        m = a[i] / b[i]
        Ab[j] = a - m * b

x = Ab[:, 3]


See Also

Sources