Thursday, March 23, 2023

LU decomposition method for solving system of equations: Application of MATLAB

 LU decomposition is a widely used technique for solving systems of linear equations, particularly those that involve large matrices. The basic idea behind LU decomposition is to factorize a given matrix into the product of two triangular matrices: a lower triangular matrix (L) and an upper triangular matrix (U). Once this factorization is done, we can solve the system of equations more efficiently using forward and backward substitution.

Here are the steps involved in LU decomposition:

  1. Start with a square matrix A of size n x n.

  2. Factorize A into the product of two matrices: A = LU, where L is a lower triangular matrix and U is an upper triangular matrix.

  3. To obtain the L and U matrices, use Gaussian elimination to transform A into an upper triangular matrix, U, while keeping track of the multipliers used in the elimination process.

  4. Store the multipliers used in the elimination process in the lower triangular part of A to obtain the L matrix.

  5. Once the L and U matrices are obtained, we can solve the system of equations Ax = b using forward and backward substitution:

  • First, solve the system LD = b using forward substitution to obtain the values of D.

  • Next, solve the system Ux =D using backward substitution to obtain the values of x.

The advantage of LU decomposition over other methods is that it can be used to solve multiple systems of equations with the same coefficient matrix A but different right-hand sides b more efficiently. This is because the factorization of A into L and U need only be done once, and then the forward and backward substitution steps can be done for each new right-hand side b. Thus it can be summarized in a mathematical way as follows.


Now let us use a Matlab solver. The Matlab package has the methodology, thus simply calling the lu can compute the operation. Consider the following example, we can write a simple Matlab code to display the solution using LU decomposition for the system of equations.




% Now let us apply LU method on Matlab: Basically, it has a built-in package
% Input the matrix A and the vector b
A = input('Enter the matrix A: ');
b = input('Enter the vector b: ');
% Calculate the LU decomposition of A using the built-in lu() function.
[L, U] = lu(A)
% Solve Ly = b using forward substitution
y = zeros(size(b)) % initialization any matrix D call it y
y(1) = b(1)/L(1,1)
for i = 2:length(b)
y(i) = (b(i) - L(i,1:i-1)*y(1:i-1))/L(i,i)
end
% Solve Ux = y using backward substitution
x = zeros(size(b))
x(end) = y(end)/U(end,end)
for i = length(b)-1:-1:1
x(i) = (y(i) - U(i,i+1:end)*x(i+1:end))/U(i,i);
end
% Output the solution vector x
disp('The solution to Ax=b is:');
disp(x);

Here is the result and some of the lines of the code also have been shown:
========================================

LUdecom
Enter the matrix A:
[-8 1 -2;2 -6 -1; -3 -1 7] % Pivoting is necessary
Enter the vector b:
[-20;-38;-34]

L =

1.0000 0 0
-0.2500 1.0000 0
0.3750 0.2391 1.0000
U =
-8.0000 1.0000 -2.0000
0 -5.7500 -1.5000
0 0 8.1087
y =
0
0
0
y =
-20
0
0
y =
-20
-43
0
y =
-20.0000
-43.0000
-16.2174
x =
0
0
0
x =
0
0
-2
The solution to Ax=b is:
4
8
-2

 

No comments:

Post a Comment