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=[a11a12a1na21a22a2nan1an2ann],x=[x1x2xn],b=[b1b2bn].

Forward Elimination

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

For each step j=1,2,,n1:

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

Then update:

aik=aikmijajk,k=j+1,,nbi=bimijbj

After this process, A becomes an upper triangular matrix.


Back Substitution

Once A is upper triangular, solve for xn,xn1,,x1 by:

xi=bij=i+1naijxjaii,i=n,n1,,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 1norm:
x1=i=1n|xi|
  • Euclidean 2norm:
x2=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=maxx0Axx=maxx=1Ax

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

  • Manhattan 1norm:
A1=max1jni=1n|aij|
  • Euclidean 2norm:
A2=λmax(ATA)
  • Chebyshev norm:
A=max1inj=1n|aij|

Matrix Norm Properties

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

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

Condition Number

κ(A)=Mm

where

M=maxx0Axx=Aandm=minx0Axx=1A1κ(A)=AA1

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=bAxa.
The backward error is the norm of the residual bAxa The forward error is xxa.

The relative backward error is:

rb,

and the relative forward error is:

xxax.

The error magnification factor is:

error magnification factor=relative forward errorrelative backward error=xxaxrb.

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 pk such that

|apk(k)|=maxkin|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=max1jn|aij|

At step i, find p such that

|api|sp=maxikn|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:

[1134]R23R1[1107]=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][1107]=[1134]=A

Derivation of L U Matrix:

Ax=bM1M2MnA=UA=Mn1M21M11UL=Mn1M21M11

EXAMPLE:

Find the LU factorization of

[121212311]

SOLUTION:

[121212311]R22R1[121030311]R3(3)R1[121030072]R3(73)R2[121030002]=UL=[1002103731],LU=[1002103731][121030002]=[121212311]=A
  • FACT 1:
A=[a11a12a13a21a22a23a31a32a33][100c10001][a11a12a13a21a22a23a31a32a33]=[a11a12a13a21ca11a22ca12a23ca13a31a32a33].
  • FACT 2:
[100c10001]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 [1134]x=[32], using LU factorization.

[1134]=LU=[1031][1107]

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

EXAMPLE:

Solve [121212311]x=[336], using LU factorization.

[121212311]=LU=[1002103731][121030002]

(a) Solve [1002103731][c1c2c3]=[336] by forward substitution: c1=3,2c1+c2=3c2=3,3c173c2+c3=6c3=4. (b) Solve [121030002][x1x2x3]=[334] by back substitution: 2x3=4x3=2,3x2=3x2=1,x1+2x2x3=4x1=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=[215444131][215444131]P1=[010100001][444215131].R1R2[444215131]R212R1[4441217131]R314R1[44412171422][44412171422]P2=[100001010][44414221217].R2R3[44414221217]R3(12)R2[444142212128]P2P1=P[010001100][215444131]=[100141012121][444022008]PA=LU

Forward and back substitution for PA=LU factorization:

Ax=bPAx=PbLUx=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=[215444131],b=[506]

SOLUTION:

  1. Lc=Pb:
[100141012121][c1c2c3]=[010001100][506]=[065].c1=014c1+c2=6c2=612c112c2+c3=5c3=8
  1. Ux=c:
[444022008][x1x2x3]=[068].8x3=8x3=12x2+2x3=6x2=24x1+4x24x3=0x1=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 (n1) 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 (n1)+(n1)×2×n=2n2n1 operations for this step of Gaussian Elimination.
  • For j=2, we zero out the column below a22.

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

  • ...

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

In=2j=1nj2j=1njj=1n1

Based on Lemma above:

In=2j=1nj2j=1njj=1n1=2n(n+1)(2n+1)6n(n+1)2n=23n3+12n223n.

Counting operation yields

1+3+5++(2n1)=i=1n(2i1)=2i=1nii=1n1=2n(n+1)2n=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+n223n3 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=t1t=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.1t72 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=b2Ax=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)2k=1000