Note 3
Note 3
BY
2
Learning Objectives
By the end of this lecture, students will be able to:
1.Understand and apply direct methods for solving
systems of linear equations.
2.Implement Gaussian elimination and LU decomposition
for linear systems.
3.Identify appropriate methods for different types of linear
systems.
Direct methods
• A direct method for solving linear equations is an approach
that attempts to solve a system of linear equations in a finite
number of steps instead of iterative methods, which gradually
approximate the solution. Direct methods yield an exact
solution, assuming no rounding errors from computation.
• In the context of the learning objectives, direct methods refer
specifically to techniques like Gaussian elimination and LU
decomposition, which transform or decompose matrices in
ways that make solving for the variables straightforward.
Gaussian elimination method
5
Gaussian Elimination Method
𝑥1 2
𝑥2 = 3
𝑥3 −1
Application of the Gaussian Elimination
Gaussian Elimination is preferable in cases where the
system of equations changes entirely with each solution, or in
educational contexts where simplicity and understanding the
process are more important than computational efficiency.
Lu decomposition method
15
LU Decomposition method for solving
system of equations
Given a system of equations
• Decomposes matrix A into L and U matrices: 𝐴 = 𝐿𝑈
𝑤ℎ𝑒𝑟𝑒, 𝐿 = 𝐿𝑜𝑤𝑒𝑟 𝑡𝑟𝑖𝑎𝑛𝑔𝑢𝑙𝑎𝑟 𝑚𝑎𝑡𝑟𝑖𝑥
𝑈 = 𝑈𝑝𝑝𝑒𝑟 𝑡𝑟𝑖𝑎𝑛𝑔𝑢𝑙𝑎𝑟 𝑚𝑎𝑡𝑟𝑖𝑥
𝑆𝑢𝑏𝑠𝑡𝑖𝑡𝑢𝑡𝑒 𝐴 = 𝐿𝑈 𝑖𝑛𝑡𝑜 𝐴𝑥 = 𝑏,
𝐿𝑈𝑥 = 𝑏
Let 𝑈𝑥 = 𝑦
• Performs forward substitution to solve 𝐿𝑦 = 𝑏.
• Performs back substitution to solve 𝑈𝑥 = 𝑦
Example 3
1 1 −1
𝐴= 1 −2 3
2 3 1
Perform forward elimination on A.
−𝑅1 + 𝑅2 = 𝑅2 , −2𝑅1 + 𝑅3 = 𝑅3 the –ve of the multiplier used to obtain 0 in 𝐴2,1 = 𝐿2,1 and 𝐴3,1 = 𝐿3,1
respectively.
1 1 −1
0 −3 4
0 1 3
1/3𝑅2 + 𝑅3 = 𝑅3 , the –ve of the multiplier used to obtain 0 in 𝐴3,2 = 𝐿3,2
1 1 −1
0 −3 4
0 0 13/3
Solution
1 1 −1
𝑈 = 0 −3 4 ,
0 0 13/3
1 0 0
𝐿= 1 1 0 ,
2 −1/3 1
𝑥 4
1 1 −1 1
0 −3 4 𝑥2 = −10
13
0 0 13/3 𝑥3 −
3
𝑥3 = −1,
𝑥2 = (−10 − 4𝑥3 )/−3 = (−10 + 4)/−3 = 2,
𝑥1 = 4 − 𝑥2 +𝑥3 = 4 − 2 − 1 = 1
𝑥1 −1
𝑥2 = 2
𝑥3 1
Example 4
1 1 −1
𝐴 = 2 −1 1
−1 2 2
Perform forward elimination on A.
−2𝑅1 + 𝑅2 = 𝑅2 , 𝑅1 + 𝑅3 = 𝑅3 the –ve of the multiplier used to obtain 0 in 𝐴2,1 = 𝐿2,1 and 𝐴3,1 = 𝐿3,1
respectively.
1 1 −1
0 −3 3
0 3 1
𝑅2 + 𝑅3 = 𝑅3 , the –ve of the multiplier used to obtain 0 in 𝐴3,2 = 𝐿3,2
1 1 −1
0 −3 3
0 0 4
Solution
1 1 −1
𝑈= 0 −3 3 ,
0 0 4
1 0 0
𝐿= 2 1 0 ,
−1 −1 1
Use forward substation to solve 𝐿𝑦 = 𝑏
1 0 0 𝑦1 −2
2 1 0 𝑦2 = 5
−1 −1 1 𝑦3 1
𝑦1 = −2,
𝑦2 = 5 − 2𝑦1 = 5 + 4 = 9,
𝑦3 = 1 + 𝑦1 +𝑦2 = 1 − 2 + 9 = 8
𝑦1 −2
𝑦2 = 9
𝑦3 8
Solution
Use back substation to solve 𝑈𝑥 = 𝑦
1 1 −1 𝑥1 −2
0 −3 3 𝑥2 = 9
0 0 4 𝑥3 8
8
𝑥3 = = 2,
4
9 − 3𝑥3 9 − 6
𝑥2 = = = −1,
−3 −3
𝑥1 = −2 − 𝑥2 +𝑥3 = −2 + 1 + 2 = 1
𝑥1 1
𝑥2 = −1
𝑥3 2
Advantages of Gaussian Elimination
27
MATLAB for LU Decomposition
% LU Decomposition Function
function [L, U, x] = lu_decomposition(A, b)
% Input:
% A - coefficient matrix
% b - right hand side vector
% Output:
% L - lower triangular matrix
% U - upper triangular matrix
% x - solution vector
[n, m] = size(A);
if n ~= m
error('Matrix A must be square');
end
28
MATLAB Code for LU Decomposition
% Initialize L and U matrices
L = eye(n);
U = A;
% Perform LU decomposition
for k = 1:n-1
for i = k+1:n
factor = U(i,k)/U(k,k);
L(i,k) = factor;
U(i,k:n) = U(i,k:n) - factor*U(k,k:n);
end
end
29
MATLAB Code for LU Decomposition
% Forward substitution (Ly = b)
y = zeros(n,1);
y(1) = b(1);
for i = 2:n
y(i) = b(i) - L(i,1:i-1)*y(1:i-1);
end
% Back substitution (Ux = y)
x = zeros(n,1);
x(n) = y(n)/U(n,n);
for i = n-1:-1:1
x(i) = (y(i) - U(i,i+1:n)*x(i+1:n))/U(i,i);
end
end 30
MATLAB Code for Gaussian Elimination
function x = gaussian_elimination(A, b)
% Input:
% A - coefficient matrix
% b - right hand side vector
% Output:
% x - solution vector
[n, m] = size(A);
if n ~= m
error('Matrix A must be square');
end
% Create augmented matrix [A|b]
Aug = [A b];
31
MATLAB Code for Gaussian Elimination
% Forward elimination
for k = 1:n-1
% Partial pivoting
[~, pivot_row] = max(abs(Aug(k:n,k)));
pivot_row = pivot_row + k - 1;
if pivot_row ~= k
Aug([k,pivot_row],:) = Aug([pivot_row,k],:);
end
for i = k+1:n
factor = Aug(i,k)/Aug(k,k);
Aug(i,k:n+1) = Aug(i,k:n+1) - factor*Aug(k,k:n+1);
end
end
32
MATLAB Code for Gaussian Elimination
% Back substitution
x = zeros(n,1);
x(n) = Aug(n,n+1)/Aug(n,n);
for i = n-1:-1:1
x(i) = (Aug(i,n+1) - Aug(i,i+1:n)*x(i+1:n))/Aug(i,i);
end
end
33
Example
function example_usage()
% Create a sample system
A = [1 1 -1;
2 -1 1;
-1 2 2];
b = [-2; 5; 1];
% Solve using LU decomposition
[L, U, x_lu] = lu_decomposition(A, b);
disp('LU Decomposition Solution:');
disp(x_lu);
% Solve using Gaussian elimination
x_gauss = gaussian_elimination(A, b);
disp('Gaussian Elimination Solution:');
disp(x_gauss);
% Verify solutions
disp('Verification (should be close to zero):');
disp(norm(A*x_lu - b));
disp(norm(A*x_gauss - b));
end
34
NEXT CLASS
35