Skip to content
PostNumerical Computation / Lecture

Numerical Computation Systems of Equations

2025-11-09
Back to Blog

Numerical Computation Systems of Equations ​

Gaussian Elimination ​

We want to solve a system of linear equations:

Ax=b

where

A=[a11a12⋯a1na21a22⋯a2n⋮⋮⋱⋮an1an2⋯ann],x=[x1x2⋮xn],b=[b1b2⋮bn].

Forward Elimination ​

Goal: eliminate all entries below the pivot element ajj in each column.

For each step j=1,2,…,n−1:

mij=aijajj,for i=j+1,…,n

Then update:

aik=aik−mijajk,k=j+1,…,nbi=bi−mijbj

After this process, A becomes an upper triangular matrix.


Back Substitution ​

Once A is upper triangular, solve for xn,xn−1,…,x1 by:

xi=bi−∑j=i+1naijxjaii,i=n,n−1,…,1

Result:

The final solution vector is:

x=[x1,x2,…,xn]T

obtained by forward elimination followed by back substitution.

python
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 x

Sources of Error ​

Vector norm ​

Formulas:

  • Manhattan 1−norm:
∥x∥1=∑i=1n|xi|
  • Euclidean 2−norm:
∥x∥2=∑i=1n|xi|2
  • Chebyshev ∞−norm:
∥x∥∞=maxi|xi|

Matrix norm ​

Given a vector norm ∥x∥,

the matrix norm induced by it is defined as:

∥A∥=maxx≠0∥Ax∥∥x∥=max∥x∥=1∥Ax∥

Let A=(aij)∈Rn×n, then:

  • Manhattan 1−norm:
∥A∥1=max1≤j≤n∑i=1n|aij|
  • Euclidean 2−norm:
∥A∥2=λmax(ATA)
  • Chebyshev ∞−norm:
∥A∥∞=max1≤i≤n∑j=1n|aij|

Matrix Norm Properties ​

For matrices A,B, vector x, and scalar c:

  1. |A|>0 if A≠0
  2. |cA|=|c|⋅|A|
  3. |A+B|≤|A|+|B|
  4. |AB|≤|A|⋅|B|
  5. |Ax|≤|A|⋅|x|

Condition Number ​

κ(A)=Mm

where

M=maxx≠0∥Ax∥∥x∥=∥A∥andm=minx≠0∥Ax∥∥x∥=1∥A−1∥κ(A)=∥A∥⋅∥A−1∥

Meaning:

  • κ(A) measures how sensitive the solution of Ax=b is to errors.
  • If κ(A) is large → the matrix is ill-conditioned (unstable).
  • If κ(A)≈1 → 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 xa be an approximate solution of the linear system Ax=b. The residual is the vector r=b−Axa.
The backward error is the norm of the residual ∥b−Axa∥∞ The forward error is ∥x−xa∥∞.

The relative backward error is:

∥r∥∞∥b∥∞,

and the relative forward error is:

∥x−xa∥∞∥x∥∞.

The error magnification factor is:

error magnification factor=relative forward errorrelative backward error=∥x−xa∥∞∥x∥∞∥r∥∞∥b∥∞.

Partial pivoting ​

In Gaussian elimination, if a pivot element akk(k) is small compared to an element ajk(k) below, the multiplier

mjk=ajk(k)akk(k)

will be large, resulting in round-off error.

Partial pivoting finds the smallest p≥k such that

|apk(k)|=maxk≤i≤n|aik(k)|

and interchanges the row (Rk)↔(Rp).

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 si for each row:

si=max1≤j≤n|aij|

At step i, find p such that

|api|sp=maxi≤k≤n|akj|sk

and interchange the row (Ri)↔(Rp).

The LU Factorization ​

DEFINITION: An m×n matrix L is lower triangular if its entries satisfy lij=0 for i<j. An m×n matrix U is upper triangular if its entries satisfy uij=0 for i>j.

EXAMPLE:

[113−4]→R2−3R1→[110−7]=U

Define L to be the 2×2 lower triangular matrix with 1's on the main diagonal and the multiplier 3 in the (2,1) location:

[1031].

Check that

LU=[1031][110−7]=[113−4]=A

Derivation of L U Matrix:

Ax=bM1M2⋯MnA=UA=Mn−1⋯M2−1M1−1UL=Mn−1⋯M2−1M1−1

EXAMPLE:

Find the LU factorization of

[12−121−2−311]

SOLUTION:

[12−121−2−311]→R2−2R1→[12−10−30−311]→R3−(−3)R1→[12−10−3007−2]→R3−(−73)R2→[12−10−3000−2]=UL=[100210−3−731],LU=[100210−3−731][12−10−3000−2]=[12−121−2−311]=A
  • FACT 1:
A=[a11a12a13a21a22a23a31a32a33]→[100−c10001][a11a12a13a21a22a23a31a32a33]=[a11a12a13a21−ca11a22−ca12a23−ca13a31a32a33].
  • FACT 2:
[100−c10001]−1=[100c10001].
  • FACT 3:
[100c110001][100010c201][1000100c31]=[100c110c2c31]

Forward and Back substitution ​

Once L and U are known, the problem Ax=b can be written as LUx=b. Define a new “auxiliary” vector c=Ux. Then back substitution is a two-step procedure: a) Solve Lc=b for c. b) Solve Ux=c for x.

EXAMPLE:

Solve [113−4]x=[32], using LU factorization.

[113−4]=LU=[1031][110−7]

(a) Solve [1031][c1c2]=[32] by forward substitution: c1=3,3c1+c2=2⇒c2=−7. (b) Solve [110−7][x1x2]=[3−7] by back substitution: x2=1,x1+x2=3⇒x1=2.

EXAMPLE:

Solve [12−121−2−311]x=[33−6], using LU factorization.

[12−121−2−311]=LU=[100210−3−731][12−10−3000−2]

(a) Solve [100210−3−731][c1c2c3]=[33−6] by forward substitution: c1=3,2c1+c2=3⇒c2=−3,−3c1−73c2+c3=−6⇒c3=−4. (b) Solve [12−10−3000−2][x1x2x3]=[3−3−4] by back substitution: −2x3=−4⇒x3=2,−3x2=−3⇒x2=1,x1+2x2−x3=−4⇒x1=3.

PA=LU factorization ​

EXAMPLE: Prove that A=[0111] does not have an LU factorization.

SOLUTION:

The factorization must have the form

[0111]=[10a1][bc0d]=[bcabac+d].

Equating coefficients yields b=0 and ab=1, a contradiction.

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 n×n matrix consisting of all zeros, except for a single 1 in every row and column.

EXAMPLE:

[100001010],[001100010],……

THEOREM:

Fundamental Theorem of Permutation Matrices. Let P be the n×n permutation matrix formed by a particular set of row exchanges applied to the identity matrix. Then, for any n×n matrix A, PA is the matrix obtained by applying exactly the same set of row exchanges to A.

EXAMPLE:

[100001010][abcdefghi]=[abcghidef]

EXAMPLE: Find the PA=LU factorization of the matrix

A=[21544−4131][21544−4131]→P1=[010100001]→[44−4215131].R1↔R2[44−4215131]→R2−12R1→[44−412−17131]→R3−14R1→[44−412−171422][44−412−171422]→P2=[100001010]→[44−4142212−17].R2↔R3[44−4142212−17]→R3−(−12)R2→[44−4142212−128]P2P1=P[010001100][21544−4131]=[100141012−121][44−4022008]PA=LU

Forward and back substitution for PA=LU factorization:

Ax=b⇒PAx=Pb⇒LUx=Pb

Solve

  1. Lc=Pb for c.
  2. Ux=c for x.

EXAMPLE: Use the PA=LU factorization to solve the system Ax=b, where

A=[21544−4131],b=[506]

SOLUTION:

  1. Lc=Pb:
[100141012−121][c1c2c3]=[010001100][506]=[065].c1=014c1+c2=6⇒c2=612c1−12c2+c3=5⇒c3=8
  1. Ux=c:
[44−4022008][x1x2x3]=[068].8x3=8⇒x3=12x2+2x3=6⇒x2=24x1+4x2−4x3=0⇒x1=−1.

Therefore, the solution is x=[−1,2,1].

Operation Count ​

Operation include: +,−,×,÷

LEMMA: ​

For any positive integer n,

(a) 1+2+3+4+⋯+n=n(n+1)2
(b) 12+22+32+42+⋯+n2=n(n+1)(2n+1)6.

Operations count for Gaussian Elimination: ​

  • For j=1,

    • mult=a(i,j)a(j,j) First a multiplier is computed for each row below the first row. This requires (n−1) multiplies.
    • a(i,k)=a(i,k)−mult×a(j,k); b(i)=b(i)−mult×b(j) Then in each row below row 1 the algorithm performs n multiplies and n additions . Thus, there is a total of (n−1)+(n−1)×2×n=2n2−n−1 operations for this step of Gaussian Elimination.
  • For j=2, we zero out the column below a22.

    • There are n−2 rows below this pivot, so this takes 2(n−1)2−(n−1)−1 operations.
  • For j=3, we would have 2(n−2)2−(n−2)−1 operations.

  • ...

To complete Gaussian Elimination, it will take In flops, where

In=2∑j=1nj2−∑j=1nj−∑j=1n1

Based on Lemma above:

In=2∑j=1nj2−∑j=1nj−∑j=1n1=2n(n+1)(2n+1)6−n(n+1)2−n=23n3+12n2−23n.

Counting operation yields

1+3+5+⋯+(2n−1)=∑i=1n(2i−1)=2∑i=1ni−∑i=1n1=2n(n+1)2−n=n2
  • On large n, where lower powers of n 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 23n3 operations and that back substitution takes on the order of n2.
  • We will often use the shorthand terminology of "big-O" to mean "on the order of," saying that elimination is an O(n3) algorithm and that back substitution is O(n2). Overall, Gaussian elimination takes 23n3+n2≈23n3 operations.
  • In other words, for large n, 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

50022(500)3/3=t1⇒t=32(500)=0.003 sec.

EXAMPLE On a particular computer, back substitution of a 5000×5000 triangular matrix takes 0.1 seconds. Estimate the time needed to solve a general system of 3000 equations in 3000 unknowns by Gaussian elimination.

SOLUTION50002 operations takes 0.1 seconds. A general system of 3000 equations in 3000 unknowns by Gaussian elimination needs 23(3000)3 operations.

2(3000)3/350002=t0.1⇒t≈72 sec.

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

Ax=b1Ax=b2⋮Ax=bk

with various right-hand side vectors bi. Classical Gaussian elimination will require approximately 2kn3/3 operations. With the LU approach, the right-hand-side b doesn't enter the calculations until the elimination (the A=LU factorization) is finished. We can solve the previous set of equations with only one elimination, followed by two back substitutions (Lc=b,Ux=c) for each new b. Lc=b,Ux=c back substitutions 2n2 The approximate number of operations with the LU approach is, therefore, 2n33+2kn2.

EXAMPLE Assume that it takes one second to factorize the 3000×3000 matrix A into A=LU. How many problems Ax=b1,…,Ax=bk can be solved in the next second?

SOLUTION

2(3000)33=2k(3000)2⇒k=1000