Javatpoint Logo
Javatpoint Logo

C++ Program to Perform LU Decomposition of Any Matrix

A popular method in numerical analysis for resolving linear equation systems and computing matrix inverses is LU decomposition. The process entails breaking down a matrix into its product of an upper triangular matrix (U) and a lower triangular matrix (L). Engineering, physics, and computational mathematics are just a few of the disciplines that use the LU decomposition. The following explains the theory underlying the LU decomposition approach and how it is implemented in the given C++ program:

1. LU Decomposition

The LU decomposition method takes an n×n matrix A as input and finds two matrices, L and U, such that A = LU.

With 1s on the major diagonal, L is a lower triangular matrix, and U is an upper triangular matrix.

2. Algorithmic Steps

Various strategies, like Doolittle's method or Crout's method, which entail methodically breaking down the original matrix into L and U, are frequently used to achieve the LU decomposition.

The values of the original matrix are used to determine the elements of L and U throughout the decomposition process.

3. Implementation in the C++ Program

The supplied C++ program generates empty matrix L and U and initializes a sample matrix A.

The input matrix A is divided into the upper triangular matrix U and the bottom triangular matrix L by the decomposeLU function.

The generated L and U matrices are then printed by the program.

4. Application and Usage

LU decomposition is a useful tool for efficiently computing matrix inverses, determinants, and systems of linear equations.

It is used in many different numerical techniques, such as structural analysis, electrical circuit analysis, and differential equation solutions.

Example:

Let us take an example to illustrate the use of LU decomposition of matrix in C++.

Output:

Lower Triangular Matrix (L):
1 0 0 
-2 1 0 
-2 -1 1 

Upper Triangular Matrix (U):
2 -1 -2 
0 4 -1 
0 0 3

Explanation:

1. File headers and global constants

  • In this example, the program comes with the required header files, <iostream> and <vector> or input/output operations and vector data structures.
  • It indicates the square matrix's size: const int N = 3. It's configured to 3x3 in this instance.

2. Function for LU Decomposition decomposeLU

  • The decomposeLU function was created to carry out LU decomposition. Three requirements must be met.
  • A represents the original matrix in two dimensions.
  • There are two 2D vectors called L and U that hold the bottom and upper triangular matrices.

3. LU Decomposition

  • The function iterates through the components of the input matrix A using nested loops to determine the values of matrices L and U.
  • The typical LU decomposition algorithm is followed throughout the decomposition procedure.
  • The procedure determines the value for each element in the higher triangular matrix U by deducting the sum of products of elements from both the lower triangular matrix L and the upper triangular matrix U.
  • As it should be, the diagonal members of the lower triangular matrix L are set to 1.
  • The non-diagonal elements of the bottom triangular matrix L are computed based on the elements of the upper triangular matrix U and the original matrix A.

4. Main Function

  • A sample 3x3 matrix A is defined in the main function.
  • The output of the LU decomposition is stored in empty matrices L and U, each of the same size.

5. LU Decomposition call

The input matrix A and the empty matrices L and U are passed to the decomposeLU function.

6. Printing the Results

The higher triangular matrix U and the lower triangular matrix L are printed to the console by the program.

7. Output

The output displays the upper and lower triangular matrices that were produced by decomposing the sample matrix A using the LU method.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA