Chapter 2 - Solving Linear Equations
Chapter 2 - Solving Linear Equations
SoICT
Hanoi University of Science and Technology
1
Introduction
1 What is a system of linear equations?
2 3-dimensional example
3 Permutation matrices and triangular matrices
4 LU analysis
5 Role of the pivot element
6 Impact of rounding error
7 Bad determinism matrix and matrix conditions
Matrix norm
Number of matrix conditions
Evaluate error when the number of conditions of the matrix is known
8 Solving a system of linear equations using matrix analysis
9 Summary
2
System of Linear Equations
Definition
a11 x1 + a12 x2 + · · · + a1n xn = b1
a21 x1 + a22 x2 + · · · + a2n xn = b2
···
am1 x1 + am2 x2 + · · · + amn xn = bm
Denote
A = (aij ) where i = 1, · · · , m and j = 1, · · · , n is the coefficient matrix A.
b = (b1 , b2 , · · · , bm )T is the right-hand side vector.
x = (x1 , x2 , · · · , xn )T is the variable vector.
We can rewrite the system of linear equations in matrix form
Ax = b
3
System of Linear Equations
Example 1 :
Consider a system of linear equations with
!
3 2
Coefficient Matrix A =
1 −1
!
−first
The right-hand side vector is b =
first
!
0.2
then the system has a unique solution x =
−0.8
4
System of Linear Equations
Example 2 :
Consider a system of linear equations with
!
1 0 3
Coefficient Matrix A =
0 1 −5
!
1
The right-hand side vector is b =
2
1 − 3t
then the system has infinitely many solutions x = 2 + 5t for every t ∈ R.
5
System of Linear Equations
Example 3 :
Consider a system of linear equations with
1 0
Coefficient Matrix A = 0 1
3 4
1
The right-hand side vector is b = 2
3
then the system has no solution.
6
System of Linear Equations
7
Solve a system of linear equations
x = A−1 b
Matlab
» x=inv(A)*b
8
Solve a system of linear equations
Example 4: :
Solve the system of equations A = (7) and b = (21) or equation
7x = 21
9
Solve a system of linear equations
Comment
The use of the inverse matrix gives a less precise solution.
When solving a system of linear equations, we often find the solution directly and only use the
inverse matrix A−1 in a few situations.
Some methods:
LU Factorization (LU Factorization)
Cholesky Analysis (Cholesky Factorization)
QR Decomposition
10
Solve a system of linear equations
XA = B
11
Solve a system of linear equations
12
Solve a system of linear equations
Matlab
» D=det(A)
» T=trace(A)
» R=rank(A)
13
Solve a system of linear equations
Kronecker-Capelli theorem
The system of linear equations Ax = b has a solution if and only if
rank(A) = rank(Ab)
14
Solve a system of square linear equations
15
Solve a system of square linear equations
Example 8 :
» A1=[-1 1; -2 2];b1=[1 ; 0];
% system has no solution
» x1 = A1\b1, D1 = det(A1)
Warning : Matrix is singular to working precision.
x1 = Inf
Inf
D1=0
16
Solve a system of square linear equations
Example 9 :
» A2=[-1 1; -2 2];b2=[1 ; 2];
% system has infinitely many solutions
» x2 = A2\b2
Warning : Matrix is singular to working precision.
x2 = -1
0
17
Solve a system of square linear equations
18
1 What is a system of linear equations?
2 3-dimensional example
3 Permutation matrices and triangular matrices
4 LU analysis
5 Role of the pivot element
6 Impact of rounding error
7 Bad determinism matrix and matrix conditions
Matrix norm
Number of matrix conditions
Evaluate error when the number of conditions of the matrix is known
8 Solving a system of linear equations using matrix analysis
9 Summary
19
Solve a system of square linear equations
3-dimensional example
Given the following system of 3rd degree equations:
10 −7 0 x1 7
−3 2 6 x2 = 4
5 −1 5 x3 6
10x1 − 7x2 =7
−3x1 + 2x2 + 6x3 = 4
5x1 − x2 + 5x3 = 6
20
Solve a system of square linear equations
we proceed to solve
Remove x1 ⇒ (2) − (1) × (−0.3) and (3) − (1) × 0.5
The factor of 10 of the x1 in (1) is called pivot element, the coefficients -0.3 and 0.5 are
called factor.
21
Solve a system of square linear equations
we continue to solve
Remove x2 ⇒ because the pivot element of x2 in (5) is -0.1 has a small absolute value,
we proceed to swap the two equations (5) and (6) and then proceed to eliminate x2 .
This is called rotation
22
Solve a system of square linear equations
we continue to solve
Remove x2 ⇒ (9) − (8) × (−0.04)
23
Solve a system of square linear equations
we continue to solve
From equation (12) ⇒ x3 = 1.
replace x3 in (11) then 2.5x2 + 5 × (1) = 2.5 ⇒ x2 = −1.
replace x2 in (10) then 10x1 − 7 × (−1) = 7 ⇒ x1 = 0.
24
Solve a system of square linear equations
Matrixes L,U,P
The entire solution just presented can be encapsulated in the following matrices
1 0 0 10 −7 0 1 0 0
L = 0.5 1 0 , U = 0 2.5 5 , P = 0 0 1
With
L is the matrix containing the factors
U is the final coefficient matrix
P is the permutation matrix describing the rotation
then we have
LU = PA
25
Solve a system of square linear equations
26
1 What is a system of linear equations?
2 3-dimensional example
3 Permutation matrices and triangular matrices
4 LU analysis
5 Role of the pivot element
6 Impact of rounding error
7 Bad determinism matrix and matrix conditions
Matrix norm
Number of matrix conditions
Evaluate error when the number of conditions of the matrix is known
8 Solving a system of linear equations using matrix analysis
9 Summary
27
Solve a system of square linear equations
Permutation matrix
The permutation matrix is obtained from the unit matrix I by permuting its rows.
With permutation matrix : P −1 = P T
Multiplication PX to permute rows of matrix X
Multiplication XP to permute matrix columns X
Example 10
0 1 0 0
0 0 0 1
P=
0
0 1 0
1 0 0 0
28
Solve a system of square linear equations
Example 10 (continued)
0 1 0 0
0 0 0 1
P=
0
0 1 0
1 0 0 0
Short form in Matlab
p = [2 4 3 1]
29
Solve a system of square linear equations
Matrix triangle
The matrix X ∈ Rn×n is upper triangular matrix if xij = 0 ∈ X ∀i > j means of the form
x11 x12 · · · x1n
0 x22 · · · x2n
X = .
.. . . ..
.. . . .
0 0 · · · xnn
When this matrix has major diagonal elements xii = 1 ∀i = 1, · · · , n is called unit upper
triangular matrix.
The determinant of an upper triangular matrix is non-zero if and only if all the elements
lie on the main diagonal are different from zero.
⇒ Similar definition we have lower triangle matrix and unit lower triangle matrix.
30
Solve a system of square linear equations
Matrix triangle
The system of equations with the matrix of triangular coefficients can be solved easily. Start
solving the last row equation to find the last unknown; then alternately substitute the above
equations to find the remaining unknowns.
Example 11 :
Solve a system of triangle equations on Ux = b
»x = zeros(n,1);
for k = n:-1:1
x(k) = b(k)/U(k,k);
i=(1:k-1)’;
b(i) = b(i) - x(k) * U(i,k);
end
31
Solve a system of square linear equations
Example 12 :
Solving a system of linear equations
% Matlab Programs
» U=[3,4,5;0.2,-3;0,0,5]; b = [7;8;11];n=3;x=zeros(n,1);
» for k=n:-1:1
» x(k) = b(k)/U(k,k);
» i=(1:k-1)’;
» b(i) = b(i) - x(k) * U(i,k);
» end;
32
1 What is a system of linear equations?
2 3-dimensional example
3 Permutation matrices and triangular matrices
4 LU analysis
5 Role of the pivot element
6 Impact of rounding error
7 Bad determinism matrix and matrix conditions
Matrix norm
Number of matrix conditions
Evaluate error when the number of conditions of the matrix is known
8 Solving a system of linear equations using matrix analysis
9 Summary
33
Solve a system of square linear equations
LU Analyze
The common algorithm used to solve a system of square linear equations has two stages
Forward elimination is the transformation of the square matrix into the upper triangular
form used to eliminate each unknown, with compatible factors and pivot elements
combined with rotation.
- consists of n − 1 steps
- at step k = 1, · · · , n − 1 multiply the equation k by the factor and then subtract the
remaining equations to de-anonymize the number xk .
- If the coefficients of xk are small then we should swap the equations.
Backward subtitution solves the last row equation to find the last solution, and then
reverses the upper rows to find the remaining unknowns. (see example 12)
34
Solve a system of square linear equations
LU analysis (continued)
Let Pk be the permutation matrices at steps k = 1, · · · , n − 1
Let Mk be the unit lower triangular matrices obtained by inserting the "addition" factors
used in the k step below the diagonal position of the k column of the unit matrix .
Let U be the final upper triangular matrix obtained at the end of the forward elimination
phase.
The elimination process is rewritten in matrix form as follows
U = Mn−1 Pn−1 · · · M1 P1 A
35
Solve a system of square linear equations
LU analysis (continued)
The equation can be rewritten as:
L1 L2 · · · Ln−1 U = Pn−1 · · · P1 A
where Lk is obtained from Mk by permutation and change sign of the factors below the
diagonal. So if we put
L = L1 L2 · · · Ln−1
P = Pn−1 · · · P2 P1
36
Solve a system of square linear equations
Example 12 :
10 −7 0
Going back to the first 3-dimensional example, we have A = −3 2 6 then the
5 −1 5
matrices determined in the forward elimination process are
1 0 0 1 0 0
P1 = 0 1 0 , M1 = 0.3 1 0
0 0 1 −0.5 0 1
1 0 0 1 0 0
P2 = 0 0 1 , M2 = 0 1 0
0 1 0 0 0.04 1
37
Solve a system of square linear equations
Example 12 (continued):
The matrices L1 ,L2 are respectively
1 0 0 1 0 0
L1 = 0.5 1 0 , L2 = 0 1 0
−0.3 0 1 0 −0.04 1
Note :
When calculating the elimination phase, we will calculate directly on the rows of the matrix,
not perform the matrix multiplication as above.
Analyze LU
The relation LU = PA is called LU analysis or triangular decomposition of the matrix A.
38
Solve a system of square linear equations
LU analysis
With the system of equations
Ax = b
where the matrix A is non-degenerate and PA = LU is the LU analysis of A, the system of
equations can be solved in two steps.
Forward elimination Solve the system
Ly = Pb
to find y , since L is a lower unit matrix, y can be found with a forward elimination (from
top to bottom).
Backward substitution Solve the system
Ux = y
39
1 What is a system of linear equations?
2 3-dimensional example
3 Permutation matrices and triangular matrices
4 LU analysis
5 Role of the pivot element
6 Impact of rounding error
7 Bad determinism matrix and matrix conditions
Matrix norm
Number of matrix conditions
Evaluate error when the number of conditions of the matrix is known
8 Solving a system of linear equations using matrix analysis
9 Summary
40
Role of the pivot element
pivot element
The elements that lie on the main diagonal of the matrix U.
The k-th pivot element is the coefficient of the solution xk in the k-th equation at the k
step of the elimination phase.
Both the forward elimination and backward substitution steps need to be divided by the
pivot element, so they cannot be zero.
Intuition :
The system of equations solves badly if the pivot element is close to zero.
41
Role of the pivot element
Example 13 :
Slight change in the second row of the above examples
10 −7 0 x1 7
−3 2.099 6 x2 = 3, 901
5 −1 5 x3 6
Thus, suppose all calculations are performed on a hypothetical computer equipped with
arithmetic operations with 5-digit floating point real numbers.
Coefficient x2 in row two changed from 2,000 to 2,099
Also the corresponding right side changed from 4,000 to 3,901
the goal is to keep the solution (0, −1, 1)T of the system of equations.
42
Role of the pivot element
Example 13 (continued) :
The first step of the elimination phase
10 −7 0 x1 7
0 −0.001 6 x2 = 6.001
0 2.5 5 x3 2.5
continue to perform elimination even though the pivot element −0.001 is small compared to
other coefficients of the matrix without performing the rotation. So we
Multiply the second row equation by 2.5 × 103 and then add the third row.
On the right hand side of this equation, multiplying 6, 001 by 2.5 × 103 results in
1, 50025 × 104 rounded to 1, 5002 × 104
43
Role of the pivot element
Example 13 (continued) :
10 −7 0 x1 7
0 −0.001 6 x2 = 6.001
0 0 1, 5005 × 10 4 x3 1, 5004 × 104
continue ...
The result on the right hand side of the second equation is rounded 1, 5002 × 104 is
added to the 2.5 which is the right hand side of the third equation and is rounded again.
So equation three becomes 1, 5005 × 104 x3 = 1, 5004 × 104 solving we have
1.5004 × 104
x3 = = 0.99993
1.5005 × 104
Obviously, with the exact value of the unknown x3 = 1, the value solved by this equation is
not worrisome.
44
Role of the pivot element
Example 13 (continued) :
10 −7 0 x1 7
0 −0.001 6 x2 = 6.001
0 0 1, 5005 × 10 4 x3 1, 5004 × 104
continue ...
for unknown x2
−0.001x2 + 6 × (0.99993) = 6.001
1.5×10−3
Candlestick x2 = −1.0×10−3
= −1.5
Finally substitute the first equation to find the solution x1
10x1 − 7 × (−1.5) = 7
deduce x1 = −3.5
45
Role of the pivot element
Example 13 (continued) :
Thus, when not performing rotation, select the pivot element
10 −7 0 x1 7
−3 −0.001 6 x2 = 6.001
5 2.5 5 x3 2.5
instead of the solution (0, −1, 1)T we get the solution (−0.35, −1.5, 0.99993)T .
46
1 What is a system of linear equations?
2 3-dimensional example
3 Permutation matrices and triangular matrices
4 LU analysis
5 Role of the pivot element
6 Impact of rounding error
7 Bad determinism matrix and matrix conditions
Matrix norm
Number of matrix conditions
Evaluate error when the number of conditions of the matrix is known
8 Solving a system of linear equations using matrix analysis
9 Summary
47
Impact of rounding error
48
Impact of rounding error
Example 14 :
Consider the system of equations
Gaussian elimination like the previous example, apply the rule of choosing the largest pivot
element, but all calculations are accurate to 3 digits after the decimal point.
49
Impact of rounding error
Example 14 (continued):
continue ....
Perform the rotation, so that 0.913 becomes the pivot element.
50
Impact of rounding error
Example 14 (continued):
continue ....
Hide x2 = 0.001/0.001 =1,000 (exactly)
Substitute for the above pt, x1 = (0.254 − 0.659x2 )/0.913 = −0.443
Finally we get the solution x ∗ = (−0.443, 1, 000)T
51
Impact of rounding error
Example 14 (continued):
Measuring the difference, it is clear that the true solution of the system x = (1, −1)T
Error : e = x − x ∗ = (1, 433, −2)T
Deviation :
!
0.217 − (0.780(−0.443) + 0.563(1, 000))
r = b − Ax ∗ =
0.254 − (0.913(−0.443) + 0.659(1, 000))
!
−0.000460
=
−0.000541
Obviously, while the deviation is acceptable when we round to three decimal places, the error
is even larger than the solution.
52
Impact of rounding error
53
Impact of rounding error
Example 14 (continued):
Replacing the assumption of rounding with 3 decimals to rounding with 6 decimals after the
decimal point, we get a system of equations after Gauss elimination
! ! !
0.913000 0.659000 x1 0.254000
=
0.000000 0.000001 x2 −0.0000001
Notice, the value of the right hand side of the second equation has changed. In fact the
approximate solution is also the exact solution of the system
−0.00001
x1 = = −1.000000
0.00001
0.254 − 0.659x2
x2 = = 1.000000
0.913
54
Impact of rounding error
Important conclusion :
When we perform Gaussian elimination with the maximum pivot element on the column make
sure the deviation r = b − Ax ∗ is small.
55
Impact of rounding error
56
Algorithm installation on Matlab
1 function [L , U ,p ]= lutx ( A )
2 [n , n ]= size ( A );
3 p =(1: n ) ’;
4 for k =1: n -1
5 [r , m ]= max ( abs ( A ( k :n , k )));
6 m = m +k -1;
7 if ( A (m , k )~=0)
8 if ( m ~= k )
9 A ([ k m ] ,:)= A ([ m k ] ,:);
0 p ([ k m ])= p ([ m k ]);
1 end
2 i = k +1: n ;
3 A (i , k )= A (i , k )/ A (k , k );
4 j = k +1: n ;
5 A (i , j )= A (i , j ) - A (i , k )* A (k , j );
6 end
7 end
8 L = tril (A , -1)+ eye (n , n );
9 U = triu ( A );
0 end
56
Exercise
Write a function bslashtx that implements MatLab’s (simplified) left division to solve a system
of linear equations.
function x=bslashtx(A,b)
n=size(A,1);
%Su use lutx(A);
...
% Downside
...
%The source
...
57
1 What is a system of linear equations?
2 3-dimensional example
3 Permutation matrices and triangular matrices
4 LU analysis
5 Role of the pivot element
6 Impact of rounding error
7 Bad determinism matrix and matrix conditions
Matrix norm
Number of matrix conditions
Evaluate error when the number of conditions of the matrix is known
8 Solving a system of linear equations using matrix analysis
9 Summary
58
Bad determinism matrix and matrix conditions
59
Bad determinism and matrix conditions
60
Bad determinism and matrix conditions
Vector norms
Definition: The function v : Rn 7→ R is said to be a vector norm over Rn if and only if
1 v (x) ≥ 0 ∀x ∈ Rn and v (x) = 0 if and only if x = 0
2 v (αx) = |α|v (x) ∀x ∈ Rn , ∀α ∈ R
3 v (x + y ) ≤ v (x) + v (y ) ∀x, y ∈ Rn this is the triangle inequality.
Normally v (x) is denoted by ||x||
61
Bad determinism and matrix conditions
Matlab
norm(x,p) for lp
and for p = 2 the function is simpler than norm(x)
62
Bad determinism and matrix conditions
Matrix norm
Definition: Function ||.|| : Rn×n 7→ R is said to be matrix norm if
||Ax||
||A|| = max ||Ax|| = max
||x||=1,x∈Rn ||x||̸=0,x∈Rn ||x||
where ||Ax|| is the norm of the vector Ax. Of course, we have the inequality ||Ax|| ≤ ||A|| ||x||
63
Bad determinism and matrix conditions
64
Bad determinism and matrix conditions
65
Bad determinism and matrix conditions
so the number of conditions that measure the ratio of the maximum expansion to the
maximum contraction that the matrix can act on for a vector is non-zero.
66
Bad determinism and matrix conditions
Note :
The matrix determinant is not a good characterization for approximation. Although when
det(A) = 0, the matrix is degenerate, but the magnitude or small of the determinant is not
contains information about whether the matrix is near degenerate or not.
For example, for the matrix det(αIn ) = αn may be a very small number when |α| < 1 but the
matrix αIn has good conditions with cond(αIn ) = 1. Where In is the n dimensional unit
matrix.
67
Bad determinism and matrix conditions
68
Bad determinism and matrix conditions
69
Bad determinism and matrix conditions
Evaluate the error when knowing the number of conditions of the matrix
Let x be the exact solution of Ax = b, and x ∗ be the solution of the system Ax ∗ = b + ∆b
(note we only consider b to be additive noise. ). Put ∆x = x ∗ − x, we have
b + ∆b = Ax ∗ = A(x + ∆x) = Ax + A∆x since Ax = b substitute in, then ∆x = A−1 ∆b.
Multiply the two inequalities (13) (14) and use the definition cond(A) = ||A||||A−1 || we have
evaluation
||∆x|| ||∆b||
≤ cond(A)
||x|| ||b||
70
Bad determinism and matrix conditions
Evaluate error when knowing the number of conditions of the matrix (continued)
continue...
||∆x|| ||∆b||
≤ cond(A)
||x|| ||b||
So the number of conditions allows us to determine the relative error variation in the solution
||∆x|| ||∆b||
||x|| given the relative change in the right-hand side. ||b||
When cond(A) is large or the system is near degenerate, the relative transformation of
the right side will ’force’ the corresponding error change in the solution.
Conversely, when cond(A) approaches 1 or the system is well-conditioned, the equivalent
transformation of the right hand side and the solution are the same.
71
Bad determinism and matrix conditions
40 5
20 0
0 −5
−20 −10
−0.5 0 0.5 1 1.5 2 2.5 3 3.5 −200 −100 0 100 200 300 400
72
Bad determinism and matrix conditions
Evaluate the error when knowing the number of conditions of the matrix (Conclusion)
If the input data is represented approximately to computer accuracy, then the relative error
estimate of the calculated solution is given by the formula:
||x ∗ − x||
≈ cond(A)ϵM
||x||
The calculated solution will lose an interval log10 (cond(A)) in decimal places in the relative
error of the data precision.
Conclusion
The system of linear equations Ax = b is ill-conditioned if cond(A) is large, then a small
change in the data can lead to a large change in the solution.
73
Bad determinism and matrix conditions
Example 15 :
Consider the system of equations
74
Bad determinism and matrix conditions
Example 16 :
Consider the system of equations
! ! !
4.1 2.8 x1 4.1
=
9.7 6.6 x2 9.7
This is an ill-conditioned system because cond(A, 1) = 2494.4 and the exact solution of the
system is x = (1, 0)T . If we substitute the right hand side b + ∆b = (4.11, 9.70)T then the
solution of the system will be x ∗ = (0.34, 0.97)T .
In Matlab we have
» A = [4.1 2.8; 9.7 6.6]; b = [4.1 ; 9.7]; b1=[4.11 ; 9.7];
» x = (A \ b)’, x1 = (A \ b1)’
x=10
x1 = 0.3400 0.9700
75
Bad determinism and matrix conditions
76
1 What is a system of linear equations?
2 3-dimensional example
3 Permutation matrices and triangular matrices
4 LU analysis
5 Role of the pivot element
6 Impact of rounding error
7 Bad determinism matrix and matrix conditions
Matrix norm
Number of matrix conditions
Evaluate error when the number of conditions of the matrix is known
8 Solving a system of linear equations using matrix analysis
9 Summary
77
Solve a system of linear equations using matrix analysis
R1 i1 R3 i2 R5 i3
V1 R2 R4 V2
78
Solving a system of linear equations by matrix analysis (continued)
−V1 + R1 i1 + R2 (i1 − i2 ) = 0
R2 (i2 − i1 ) + R3 i2 + R4 (i2 − i3 ) = 0
R4 (i3 − i2 ) + R5 i3 + V2 = 0
converted into
R1 + R2 −R2 0 i1 V1
−R2 R2 + R3 + R4 −R4 i2 = 0
0 −R4 R4 + R5 i3 −V2
79
System of Linear Equations
Summary
Features of a system of linear equations (linear algebra)
Recalling permutation matrices and triangular matrices
LU analysis is used to solve system of linear equations
The role of cylinders in LU analysis can cause errors in the results
Rounding error effect
Determines the condition of a matrix in a linear equation that causes the resulting error
An example of applying a system of linear equations
80
Solve a system of linear equations using matrix analysis
More homework
We can use many methods besides LU analysis to solve the system of equations
Cholesky Analysis
▶ The concept of a positive semi-deterministic matrix
▶ If A is a positive definite matrix then there exists a positive lower triangular matrix L such
that A = LLT
▶ Eliminate forward Ly = b, backward substitution LT x = y
Decay QR
▶ Orthogonal matrix concept
▶ Decay QR : if A ∈ Rm×n with m ≥ n and has rank n then there exists an orthogonal matrix
Q ∈ Rm×n and a triangular matrix on R ∈ Rn×n with positive elements on the diagonal such
that A = QR
▶ Solve least squares problem
min{||Ax − b||2 |x ∈ R}
so the solution x is the stopping point when minimizing the above problem.
81