Numerical Computation Systems of Equations
Gaussian Elimination
We want to solve a system of linear equations:
where
Forward Elimination
Goal: eliminate all entries below the pivot element
For each step
Then update:
After this process,
Back Substitution
Once
Result:
The final solution vector is:
obtained by forward elimination followed by back substitution.
import numpy as np
def gauss_naive(A, b):
"""
Solve A x = b using naive Gaussian elimination (no pivoting).
A: (n,n) ndarray, b: (n,) or (n,1)
Returns x shape (n,)
"""
# Ensure inputs are float type
A = np.array(A, dtype=float)
b = np.array(b, dtype=float).reshape(-1,)
n = A.shape[0]
# Augment matrix [A | b]
Ab = np.hstack([A, b.reshape(-1,1)])
# Forward elimination
for j in range(n-1):
# Check for small pivot (numerical stability check)
if abs(Ab[j, j]) < np.finfo(float).eps:
raise ValueError(f"Zero pivot encountered at column {j}")
for i in range(j+1, n):
mult = Ab[i, j] / Ab[j, j]
# Row operation: Row_i = Row_i - mult * Row_j
Ab[i, j:] = Ab[i, j:] - mult * Ab[j, j:]
# Back substitution
x = np.zeros(n)
for i in range(n-1, -1, -1):
# x[i] = (b[i] - sum(A[i, j] * x[j])) / A[i, i]
rhs = Ab[i, -1] - Ab[i, i+1:n] @ x[i+1:n]
x[i] = rhs / Ab[i, i]
return xSources of Error
Vector norm
Formulas:
- Manhattan
:
- Euclidean
:
- Chebyshev
:
Matrix norm
Given a vector norm
the matrix norm induced by it is defined as:
Let
- Manhattan
:
- Euclidean
:
- Chebyshev
:
Matrix Norm Properties
For matrices
if
Condition Number
where
Meaning:
measures how sensitive the solution of is to errors. - If
is large the matrix is ill-conditioned (unstable). - If
the matrix is well-conditioned (stable).
The condition number is a good indicator of how close is a matrix to be singular. The large the condition number the closer we are to singularity.
It is also very useful in assessing the accuracy of solutions to linear systems.
In practice we don’t really calculate the condition number, it is merely estimated, to perhaps within an order of magnitude.
Error Magnification
Let
The backward error is the norm of the residual
The relative backward error is:
and the relative forward error is:
The error magnification factor is:
Partial pivoting
In Gaussian elimination, if a pivot element
will be large, resulting in round-off error.
Partial pivoting finds the smallest
and interchanges the row
Scaled partial pivoting
If there are large variations in magnitude of the elements within a row, scaled partial pivoting can be used.
Define a scale factor
At step
and interchange the row
The LU Factorization
DEFINITION: An
EXAMPLE:
Define
Check that
Derivation of L U Matrix:
EXAMPLE:
Find the LU factorization of
SOLUTION:
- FACT 1:
- FACT 2:
- FACT 3:
Forward and Back substitution
Once L and U are known, the problem
EXAMPLE:
Solve
(a) Solve
EXAMPLE:
Solve
(a) Solve
PA=LU factorization
EXAMPLE: Prove that
SOLUTION:
The factorization must have the form
Equating coefficients yields
PA = LU factorization is the matrix formulation of elimination with partial pivoting. PA = LU factorization is simply the LU factorization of a row-exchanged version of A.
PA=LU factorization
DEFINITION:
A permutation matrix is an
EXAMPLE:
THEOREM:
Fundamental Theorem of Permutation Matrices. Let
EXAMPLE:
EXAMPLE: Find the PA=LU factorization of the matrix
Forward and back substitution for PA=LU factorization:
Solve
for . for .
EXAMPLE: Use the PA=LU factorization to solve the system
SOLUTION:
:
:
Therefore, the solution is
Operation Count
Operation include:
LEMMA:
For any positive integer
(a)
(b)
Operations count for Gaussian Elimination:
For
, First a multiplier is computed for each row below the first row. This requires multiplies. ; Then in each row below row 1 the algorithm performs multiplies and additions . Thus, there is a total of operations for this step of Gaussian Elimination.
For
, we zero out the column below . - There are
rows below this pivot, so this takes operations.
- There are
For
, we would have operations. ...
To complete Gaussian Elimination, it will take
Based on Lemma above:
Counting operation yields
- On large
, where lower powers of become negligible by comparison. In this case, if we ignore the lower order terms in the expressions for the number of multiplication/divisions, we find that elimination takes on the order of operations and that back substitution takes on the order of . - We will often use the shorthand terminology of "big-O" to mean "on the order of," saying that elimination is an
algorithm and that back substitution is . Overall, Gaussian elimination takes operations. - In other words, for large
, the lower order terms in the complexity count will not have a large effect on the estimate for running time of the algorithm and can be ignored if only an estimated time is required.
EXAMPLE Estimate the time required to carry out back substitution on a system of 500 equations in 500 unknowns, on a computer where elimination takes 1 second.
SOLUTION
EXAMPLE On a particular computer, back substitution of a
SOLUTION
Operations count for LU factorization:
Now, suppose that we need to solve a number of different problems with the same A and different b. That is, we are presented with the set of problems
with various right-hand side vectors
EXAMPLE Assume that it takes one second to factorize the
SOLUTION