Gaussian Elimination
Gaussian Elimination
Consider the problem: Find such that
Gaussian elimination -- step 1 reduce the above system of equations so that the unknown is removed from the last two equations:
In this case the 2 coefficient for x in the first row is the pivot, and we are using this pivot value to convert the other two x coefficients in the rows below to zeros.
Gaussian elimination -- step 2 remove the unknown from the last equation:
Now the pivot is the 1 coefficient for in eq2.
This system is said to be upper triangular. It is also known as row echelon form, and the leading coefficients ([2, 1, -72] in this case) are known as the pivots.
Gaussian elimination -- step 3 We can now use back substitution to obtain .
In this case
Pivoting
Consider the following system
This firstly reduces to
The problem here is that we have zero for the pivot in eq2. This can easily be switched into upper triangular form by switching rows two and three.
Partial pivoting: In general, we should be worried about both zero and very small pivot values, as in the latter case they will lead to division by a small value, which can cause large roundoff errors. So common practice is to select a row/pivot value such that the pivot value is as large as possible
Singular matrices in Gaussian Elimination
This firstly reduces to
We cannot solve this by switching rows in this case, which means that the matrix is singular and has no inverse
Gaussian Elimination Rules
Operate on LHS and RHS (or RHSs) at the same time
Replace row with a sum/combination of rows
Work on one column at a time, choosing a pivot (leading non-zero entry in a chosen row), and eliminating all other non-zero values below that
Switch rows to avoid zeros on the diagonal (pivoting)
If (3) does not work, zeros on the diagonal (pivots) indicate a singular matrix
Computational cost: If the number of equations is large, then a number of operations for gaussian elimination is .
Pseudocode
Wikipedia has a pseudocode implementation of the gaussian elimination algorithm which is helpful to understand how it works:
h := 1 /* Initialization of the pivot row */ k := 1 /* Initialization of the pivot column */ while h ≤ m and k ≤ n /* Find the k-th pivot: */ i_max := argmax (i = h ... m, abs(A[i, k])) if A[i_max, k] = 0 /* No pivot in this column, pass to next column */ k := k+1 else swap rows(h, i_max) /* Do for all rows below pivot: */ for i = h + 1 ... m: f := A[i, k] / A[h, k] /* Fill with zeros the lower part of pivot column: */ A[i, k] := 0 /* Do for all remaining elements in current row: */ for j = k + 1 ... n: A[i, j] := A[i, j] - A[h, j] * f /* Increase pivot row and column */ h := h + 1 k := k + 1
Gaussian Elimination
Code a Python function that takes an 2D numpy array representing a matrix , and a 1D array representing a RHS , and returns the solution of the linear equation . If you wish you can assume that the matrix has an inverse. Try it out on a few test matrices and check your answer using
scipy.linalg.solve
.
Condition Number
Gaussian Elimination might still fail if is close to being singular, if a slight change to its values causes it to be singular. In this case simple round-off error in the floating point calculations can lead to zeros in the pivot positions.
Even if the pivot value is not exactly zero, a pivot value close to zero can lead to large differences in the final result. In this case the matrix would be nearly singular, or ill-conditioned. Most linear algebra packages will include a method of calculating the condition number of a matrix, which evaluates how sensitive the solution is to the input values of the matrix or rhs vector. An identity matrix has a condition number of 1, while an exactly singular matrix has a condition number of infinity.
Condition Number
Suppose an experiment leads to the following system of equations:
Solve system (1), and then solve system (2), below, in which the data on the right have been rounded to two decimal places. In each case, find the exact solution.
The entries in (2) differ from those in (1) by less than .05%. Find the percentage error when using the solution of (2) as an approximation for the solution of (1).
Use
numpy.linalg.cond
to produce the condition number of the coefficient matrix in (1).