Lecture - Solving Linear Systems of Equations
Lecture - Solving Linear Systems of Equations
• We know:
Numerical Methods
• initial concentrations of all
components A0 , B0 , C0 , D0 , E0 , F0
• We want to compute
• steady-state concentrations of all
components A, B, C, D, E, F
Numerical Methods
dC/dt = k31A + k32B + k34D - k13C- k23C- k43C
dA/dt+dB/dt+dC/dt+dD/dt+dE/dt+dF/dt = 0
A + B + C + D + E + F = const
• The data
A0 =1 ; B0 =0 ; C0 =0 ; D0 =0 ; E0 =1 ; F0 =0 ;
3
k21 =0.2 ; k31 =0.1 ; k32 =0.1 ; k34 =0.1 ; k54 =0.05 ;
k64 =0.2 ; k65 =0.1 ; k12 =0.1 ; k13 =0.05 ; k23 =0.05 ;
k43 =0.2 ; k45 =0.1 ; k46 =0.2 ; k56 =0.1 ; ;
Chemical reaction network: steady state
Numerical Methods
Constantinides & Mostoufi, Numerical methods for
Chemical Engineers with MATLAB applications, p 139
Numerical Methods
• The radiosity of a pixel i, Bi, can be computed as a
solution of the following linear system of n
equations for a given 3D model with n pixels:
Bi = Ei + ρi Bj Fi,j
j
5
Linear systems of equations (LSE)
Numerical Methods
• Interpolation (e.g., construction of the cubic spline
interpolant)
• Boundary value problems (BVP) of ordinary differential
equations (ODE)
• Partial differential equations (PDE)
• …
https://vimeo.com/160322285
Linear systems of equations (LSE)
• Find:
Numerical Methods
• vector x ∈ Rn such that it satisfies Ax = b
where:
• System matrix A ∈ Rn,n with coefficients aij, i,j=1..n
• Right hand side vector (RHS) b ∈ Rn with coefficients bj,
j=1..n
• Solution methods:
Numerical Methods
• Direct methods
• provide the exact solution in a finite number of
operations (Gauss elimination, etc.)
• Iterative methods
• start with an approximate (guess) solution and
iterate to approach to the exact solution
• In all methods, we assume that matrix A is :
• regular/invertible <-> full column rank <-> full row
rank <-> non-zero determinant 8
Gauss elimination method
• LSE of n equations and n unknowns
Numerical Methods
• Phase 1: transform to a upper triangular LSE
Numerical Methods
2x1 - x2 + x3 + 2x4 = 1 × -0.5
× -1.5
× -0.5
x1 + 4x2 + 2x3 - 4x4 = -2 +
3x1 + x2 - x3 - 10x4 = 5 +
x1 + x2 - x3 - 6x4 = 3 +
10
Phase 1: forward elimination
pivotal
• LSE element
Numerical Methods
2x1 - x2 + x3 + 2x4 = 1
11
Eliminated
elements
Phase 1: forward elimination
• LSE
Numerical Methods
2x1 - x2 + x3 + 2x4 = 1
pivotal
element
0 0 - 2x3 - 5⅓ x4 = 3⅓ +
Eliminated 12
elements
Phase 1: forward elimination
• LSE
Numerical Methods
2x1 - x2 + x3 + 2x4 = 1
0 0 - 3⅓ x3 - 102⁄9 x4 = 4 8⁄9
0 0 0 0.8 x4 = 0.4
Eliminated 13
elements
Forward elimination: algorithm
pivotal
element
Numerical Methods
for i in range(n - 1): # Pivoting rows
for j in range(i + 1, n):
elFact = -a[j, i] / a[i, i]
for k in range(i, n):
a[j, k] += elFact * a[i, k]
b[j] += elFact * b[i]
return a, b
Eliminated in Element
previous i-1 steps Elements to compute
to eliminate
14
Phase 2: back substitution
• LSE
Numerical Methods
2x1 - x2 + x3 + 2x4 = 1
0 0 - 3⅓ x3 - 102⁄9 x4 = 4 8⁄9
0 0 0 0.8 x4 = 0.4
x4 = 0.5 17
Phase 2: back substitution
• LSE
Numerical Methods
2x1 - x2 + x3 = 0
+ 4.5x2 + 1.5x3 = 0
- 3⅓ x3 = 10
0.8 x4 = 0.4
x3 = -3 18
Phase 2: back substitution
• LSE
Numerical Methods
2x1 - x2 = 3
+ 4.5x2 = 4.5
- 3⅓ x3 = 10
0.8 x4 = 0.4
x2 = 1 19
Phase 2: back substitution
• LSE
Numerical Methods
2x1 = 4
+ 4.5x2 = 4.5
- 3⅓ x3 = 10
0.8 x4 = 0.4
x1 = 2 20
Back substitution: algorithm
Numerical Methods
bn(n)
xn = (n)
ann
1 é (n-1) (n-1) ù
xn-1 = b - an-1n xn û
(n-1) ë n-1
an-1n-1
1 é (i ) n (i ) ù
xi = (i ) êbi - å aik xk ú i = n -1,n - 2,…,1 21
aii ë k=i+1 û
Gauss elimination algorithm: cost
Numerical Methods
n(n -1)(2n + 5)
• Additions + subtractions: 6
n(n 2 + 3n -1)
• Multiplications + divisions
3
• A simple example:
Numerical Methods
é 0.001 1 ù é x1 ù é 1 ù × -1000
ê úê ú=ê ú
ë 1 1 ûëê x 2 úû ë 2 û +
é 0.001 1 ù é x1 ù é 1 ù
ê úê ú=ê ú
ë 0 -999 û êë 2 úû ë -998 û
x
é x1 ù é 1000(1- 0.998) ù é 2 ù
ê ú=ê ú=ê ú
x
êë 2 úû êë 0.998 úû ë 0.998 û
23
Pivoting
Numerical Methods
é 1 1 ù é x1 ù é 2 ù ×-0.001
ê úê ú=ê ú
ë 0.001 1 x
û êë 2 úû ë 1 û +
é 1 1 ù é x1 ù é 2 ù
ê úê ú=ê ú
ë 0 0.999 ê x
û ë 2 úû ë 0.998 û
Numerical Methods
Swap
these rows
Largest in
magnitude 25
Numerical Methods
Swap
these rows
Largest in
magnitude
• When swapping columns, remember to swap the solution vector, i.e., xp becomes xi and
vice versa!
Pivoting recapitulation
• Errors due to finite-precision arithmetic are introduced in each
Numerical Methods
arithmetic operation
Numerical Methods
A L U
Ax = b
thus, we are Ly = b - forward substitution
LUx = b solving instead: Ux = y - back substitution
y
28
LU decomposition: example
é a11 a12 a13 ù é l11 0 0 ù é 1 u12 u13 ù
ê ú ê úê ú
ê a21 a22 a23 ú Þ ê l21 l22 0 úê 0 1 u23 ú
Numerical Methods
ê a a32 a33 ú ê l31 l32 l33 ú êë 0 0 1 úû
ë 31
û ë û
Numerical Methods
1 Compute 1st column of L as: li1 = ai1
a1 j
2 Compute 1st row of U as: u1 j =
l 11
Numerical Methods
• factorization of A as LU
2 3
• forward substitution n + n2 + n2 total operations
3
• backward substitution
• For any number of RHS vectors (vectors b) we need to perform
LU decomposition only once!
31
LU decomposition: remarks
• Solving a LSE using LU decomposition requires
Numerical Methods
• factorization of A as LU
2 3
• forward substitution n + n2 + n2 total operations
3
• backward substitution
• For any number of RHS vectors (vectors b) we need to perform
LU decomposition only once!
• Each element of A matrix is used only once to compute the
corresponding element of L or U matrix –> so L and U can be
stored in A
32
• Partial pivoting is sufficient, and widely implemented
Cholesky decomposition
Numerical Methods
a11 a12 a13 l11 0 0 l11 l21 l31
a12 a22 a23 l21 l22 0 0 l22 l32
a a33 l31 l33 0 l33
13 a23 l32 0
• A must be symmetric, positive definite, i.e., xTAx > 0, for all x≠0
Numerical Methods
a12 a22 l22 0 0 l22 l32
a a33 l31 l33 0 l33
13 a23 l32 0
Numerical Methods
a12 a22 l22 0 0 l22 l32
a a33 l31 l33 0 l33
13 a23 l32 0
Numerical Methods
• Easy to solve LSE: diagonal, upper or lower triangular
• Finitely many elementary operations – number
depending on n
• Gauss elimination methods, LU decomposition,
Cholesky decomposition
• Some well-known methods such as the Cramer’s rule or
the explicit computation of the inverse of A are
36
computationally prohibitive.
Sparse systems
• For sparse systems
a11 0 a13 a14 0
0 a 0 a24 0
22
Numerical Methods
a41 a42 0 a44 0
0 0 a53 0 a55
• During the process of forward elimination there is a fill-in effect
a11 0 a13 a14 0 non-zero, fill-in, terms
0 a
a23 a24 0
22
0 0
a33
a34
a35
0 0 0
a44
a45
0 0 0 0
a55
Numerical Methods
a21 x1 + a22 x2 + a23 x3 = b2
a31 x1 + a32 x2 + a33 x3 = b3
• Convert to
b1 a12 a13
x1 = - x2 - x3
a11 a11 a11
b2 a21 a
x2 = - x1 - 23 x3 Cx + d
a22 a22 a22
b3 a31 a32
x3 = - x1 - x2
a33 a33 a33
• Solve iteratively 38
x ( k ) = Cx ( k −1) + d
Iterative methods for solving LSE
• If LSE are of a very large size, the computational efforts required for
Numerical Methods
applying direct methods are prohibitively expensive
• Gauss-Seidel method
x1(k ) = −
a12 (k −1) a13 (k −1) b1
x2 − x3 +
a11 a11 a11
Numerical Methods
x 2 (k ) = −
a21 (k −1) a23 (k −1) b2
x1 − x3 +
a22 a22 a22
x3 (k ) = −
a31 (k −1) a32 (k −1) b3
x1 − x2 +
a33 a33 a33
• Compute new values of solution, x(k), using the values from the
previous iteration, x(k-1)
40
Jacobi method: example
• Apply the Jacobi method to solve the system:
5x1 - x2 = -7
Numerical Methods
x1 + 4x2 = 7
• we got
a12 (k-1) b1 1 (k-1) 7
x 1
(k )
=- x2 + = x2 -
a11 a11 5 5
a21 (k-1) b2 1 (k-1) 7
x2 (k )
=- x1 + = - x1 +
a22 a22 4 4
• if we take the starting point (x1, x2)=(0, 0) and perform iterations
1 7 7 1 7 7 21
x1(1) = ×0 - = - x1(2) = × - =- x1(6) = -1.0001
5 5 5 5 4 5 20
... 41
1 7 7
x2 (1) = - × 0 + =
1 7 7 42
x2 (2) = - × - + = x2 (6)
= 2.0002
4 4 4 4 5 4 20
Gauss-Seidel method
Numerical Methods
from x is updated,
(k) a21 ( k ) a23 ( k-1) b2
it is used subsequently
x2 = - x1 - x3 +
a22 a22 a22
(k) a31 ( k ) a32 ( k ) b3
x3 =- x1 - x2 +
a33 a33 a33
42
Gauss-Seidel method: example
• System: 5x1 - x2 = -7
x1 + 4x2 = 7
Numerical Methods
• Gauss-Seidel iteration: a12 (k-1) b1 1 (k-1) 7
x1(k ) = - x2 + = x2 -
a11 a11 5 5
a21 (k ) b2 1 7
x2(k ) = - x1 + = - x1(k ) +
a22 a22 4 4
• if we take the starting point (x1, x2)=(0, 0) and perform iterations:
1 7 7 1 21 7 49 1 399 7
x1(1) = × 0 - = - x1(2) = × - = - x1(3) = × - = -1.001
5 5 5 5 10 5 50 5 200 5
1 7 7 21 1 49 7 399 1 7
x2(1) = - × - + = x2(2) = - × - + = x2(3) = - × -1.001+ = 2.0001
4 5 4 10 4 50 4 200 4 4
1 8001 7
x1(4) = × - = 0.99995 x1(6) = -1.0001 43
5 4000 5 Compared to Jacobi:
1 7 x2 (6) = 2.0002
x2(4) = - × -0.99995 + = 1.99999
4 4
Successive Over Relaxation (SOR)
w
x1( k ) = (1- w )x1( k-1) + (b1 - a12 x2 ( k-1) - a13 x3( k-1) )
a11
Numerical Methods
w
x2 ( k ) = (1- w )x2( k-1) + (b2 - a21 x1( k ) - a23 x3( k-1) )
a22
w
x3( k ) = (1- w )x3( k-1) + (b3 - a31 x1( k ) - a32 x2( k ) )
a33
Numerical Methods
the magnitude of the largest eigenvalue of C should be smaller than 1
• If A is diagonally dominant matrix (i.e., the size of the diagonal element
is larger than the sum of the moduli of the other elements in the row -
|aii|>|ai1|+|ai2|+…+|aii-1|+|aii+1|+…+|ain|) then Jacobi and Gauss-
Seidel converge (sufficient condition)
Numerical Methods
iteratively computed: xk+1 = xk + w(b - Axk), w≠0
• Number of iterations (and therefore elementary operations) is
apriori unknown
• May not converge
• Jacobi method, Gauss-Seidel method, Succesive Over Relaxation
(SOR)
• There exist other methods that solve linear systems by
minimizing the residual of the equation: rk = (b - Axk) – see
46
Krylov subspace methods
Sensitivity of linear systems
Numerical Methods
• In many real world applications A and b are known only
approximately -> which might prevent us to find x*
• Assume that we know the exact A but not b (we know 𝑏)
• Q: how sensitive is our solution with respect to uncertainty in b?
47
Motivational example: deblurring images
Numerical Methods
optical system, turbulence…
• Frequent problem in astronomy (e.g., Hubble)
• Linear system Ax=b:
• A – blurring matrix
• x – sharp image
• b – blurred image
48
Numerical Methods
49
Blurred image (b) Blurred image + noise
(jitter in b)
taken from L. Vandenberghe
Deblurring images
Numerical Methods
What happened here???
50
𝐴−1 𝑏 𝐴−1 𝑏
taken from L. Vandenberghe
Sensitivity of linear systems
Numerical Methods
• In many real world applications A and b are known only
approximately -> which might prevent us to find x*
• Assume that we know the exact A but not b (we know 𝑏)
• Q: how sensitive is our solution with respect to uncertainty in b?
we can define
• If we denote 𝑥ො = 𝐴−1 𝑏,
• Deviation: e = 𝑥 ∗ − 𝑥ො
• Residual: r = 𝑏 − 𝐴𝑥ො = 𝐴𝑒
Numerical Methods
• 𝑥 > 0, 𝑥 = 0 iff 𝑥 = 0 – positivity
• 𝑎𝑥 = 𝑎 𝑥 - homogeneity
• 𝑥+𝑦 ≤ 𝑥 + 𝑦 - triangle inequality
• P-norm
𝑝 𝑝 𝑝 1/𝑝
• 𝑥 𝑝 = 𝑥1 + 𝑥2 + ⋯ + 𝑥𝑛
• Manhattan norm (1 norm) : 𝑝 = 1
• Euclidian norm (2 norm): 𝑝 = 2
52
• Maximum norm (∞ norm): 𝑥 ∞ = 𝑚𝑎𝑥1≤i≤n 𝑥𝑖
Matrix norms
Numerical Methods
• It is a mapping Rmxn -> R satisfying the three properties presented
for the vector norms. A matrix p-norm can additionally be
• 𝐴𝑥 ≤ 𝐴 𝑥 - consistent
• 𝐴𝐵 ≤ 𝐴 𝐵 - submultiplicative
• 2-norm
• 𝐴 2 = 𝜆𝑚𝑎𝑥 (𝐴𝑇 𝐴)
53
Condition number
• Relates relative errors in the inputs (b) to relative errors in
solutions:
Numerical Methods
𝑥 ∗ −𝑥ො 𝑏∗ −𝑏
𝛿𝑥 = 𝑥∗
∝ 𝛿𝑏 = 𝑏∗
𝑥 ∗ − 𝑥ො
𝐴−1 (𝑏 ∗ −𝑏)
𝐴−1 (𝑏 ∗ −𝑏) 𝑏∗
𝛿𝑥 = ∗
= ∗
≤
𝑥 𝑥 𝑥∗ 𝑏∗
𝐴−1 𝐴−1
𝛿𝑥 ≤ 𝑏 ∗ 𝛿𝑏 = 𝐴𝑥 ∗ 𝛿𝑏 ≤ 𝐴−1 𝐴 𝛿𝑏
𝑥∗ 𝑥∗
• Some properties
• Scale-invariant cond(aA)=cond(A), for all a
Numerical Methods
• It is norm dependent
• In general, cond(A) >> 1 system is highly sensitive:
• a small jitter in RHS (𝛿𝑏) results in big errors in solutions
(𝛿𝑥)
• ill-conditioned systems (otherwise well-conditioned
systems)
Numerical Methods
56
𝐴−1 𝑏 𝐴−1 𝑏
taken from L. Vandenberghe