0% found this document useful (0 votes)
28 views35 pages

Note 3

EIE STUDEIS

Uploaded by

xjfdnbzw9s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views35 pages

Note 3

EIE STUDEIS

Uploaded by

xjfdnbzw9s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

LECTURE NOTE ON EIE 419

(NUMERICAL METHODS II)

BY

Engr. Grace Olaleru


Direct Methods for Solving
Linear Equations

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

• Gaussian elimination is a systematic method for solving


systems of linear equations by transforming the augmented
matrix into row echelon form through elementary row
operations.

• The equation is of the form 𝐴𝑥 = 𝑏


Gaussian Elimination procedures
• Arrange the system of equations in matrix form.
• Use row operations to get the matrix into upper triangular (or
row-echelon) form.
• Apply back-substitution to solve for each variable starting from
the last row or solve until the diagonal equals 1 while others
should be 0 (identity matrix).
Example 1

Use the Gaussian elimination method to solve the systems of linear


equations below:
𝑥1 + 𝑥2 − 𝑥3 = −2
2𝑥1 − 𝑥2 + 𝑥3 = 5
−𝑥1 + 2𝑥2 + 2𝑥3 = 1
Solution
• Convert the systems of equations into an augmented matrix:
1 1 −1 −2
2 −1 1 ቮ 5
−1 2 2 1
−2𝑅1 + 𝑅2 = 𝑅2 , 𝑅1 + 𝑅3 = 𝑅3
1 1 −1 −2
0 −3 3 ቮ 9
0 3 1 −1
𝑅2 + 𝑅3 = 𝑅3
1 1 −1 −2
0 −3 3 ቮ 9
0 0 4 8
Solution
¼ 𝑅3 = 𝑅3
1 1 −1 −2
0 −3 3 ቮ 9
0 0 1 2
We can do back substitution from here or continue until matrix A become an identity matrix.
-1/3 𝑅2 + 𝑅3 = 𝑅2
1 1 −1 −2
0 1 0 ቮ−1
0 0 1 2
𝑅1 − 𝑅2 = 𝑅1
1 0 −1 −1
0 1 0 ቮ−1
0 0 1 2
𝑅1 + 𝑅3 = 𝑅1
1 0 0 1
0 1 0 ቮ−1
0 0 1 2
𝑥1 = 1, 𝑥2 = −1, 𝑥3 = 2.
Example 2

Use the Gaussian elimination method to solve the systems of linear


equations below:
2𝑥1 + 𝑥2 − 𝑥3 = 8
−3𝑥1 − 𝑥2 + 2𝑥3 = −11
−2𝑥1 + 𝑥2 + 2𝑥3 = −3
Solution

• Convert the systems of equations into an augmented matrix:


2 1 −1 8
−3 −1 2 ቮ−11
−2 1 2 −3
3
𝑅 + 𝑅2 = 𝑅2 , 𝑅1 + 𝑅3 = 𝑅3
2 1
2 1 −1 8
0 1/2 1/2 ቮ1
0 2 1 5
4𝑅2 − 𝑅3 = 𝑅3
2 1 −1 8
0 1/2 1/2 ቮ 1
0 0 1 −1
Solution
2𝑅2 = 𝑅2
2 1 −1 8
0 1 1 ቮ2
0 0 1 −1
We can do back substitution from here or continue until matrix A become an identity matrix.
-
𝑥3 = −1
x2 = 2 − x3 = 2 + 1 = 3
x1 = (8 − x2 + x3 )/2 = (8 − 3 − 1)/2 = 2

𝑥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

Use the LU decomposition method to solve the systems of linear


equations below:
𝑥1 + 𝑥2 − 𝑥3 = 4
−𝑥1 − 2𝑥2 + 3𝑥3 = −6
2𝑥1 + 3𝑥2 + 𝑥3 = 7
Solution
𝐴𝑥 = 𝑏
1 1 −1 𝑥1 4
1 −2 3 𝑥2 = −6
2 3 1 𝑥3 7

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

Use forward substation to solve 𝐿𝑦 = 𝑏


1 0 0 𝑦1 4
1 1 0 𝑦2 = −6
2 −1/3 1 𝑦3 7
𝑦1 = 4,
𝑦2 = −6 − 𝑦1 = −6 − 4 = −10,
1 10 13
𝑦3 = 7 − 2𝑦1 + 𝑦2 = 7 − 8 − =−
3 3 3
𝑦1 4
𝑦2 = −10
13
𝑦3 −
3
Solution
Use back substation to solve 𝑈𝑥 = 𝑦

𝑥 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

Use the LU decomposition method to solve the systems of linear


equations below:
𝑥1 + 𝑥2 − 𝑥3 = −2
2𝑥1 − 𝑥2 + 𝑥3 = 5
−𝑥1 + 2𝑥2 + 2𝑥3 = 1
Solution
𝐴𝑥 = 𝑏
1 1 −1 𝑥1 −2
2 −1 1 𝑥2 = 5
−1 2 2 𝑥3 1

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

• Efficiency: Particularly useful when solving multiple systems with


the same matrix but different right-hand sides (e.g., time-step
simulations, financial models, and iterative physics-based
calculations).
• Flexibility: Often more computationally efficient in complex
engineering problems compared to repeated Gaussian elimination.
HOMEWORK
1) Find the current flowing through each resistor using the Gaussian
elimination and LU Decomposition method.

2) Given that 𝑉1 = 9𝑉, determine the new current values.


26
Matlab code for the Lu decomposition
and Gaussian elimination methods

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy