Home » Numerical Techniques Using C++ » LU Decomposition Method C++ Program | Rule Example

# LU Decomposition Method C++ Program | Rule Example ## Why LU Decomposition Method.

As you know Gauss elimination is designed to solve systems of linear algebraic equations, [A]{X} = {B}

Although it certainly represents a sound way to solve such systems, it becomes inefficient when solving equations with the same coefficients [A], but with different constants ( b’s).

The Gauss elimination involves two steps: forward elimination and back substitution. Of these, the forward-elimination step comprises the bulk of the computational effort.

This is particularly true for large systems of equations. That is the reason why LU Decomposition method c++ has been designed.

## Rule | LU Decomposition Method.

[A] {X} = {B}.
LU decomposition methods separate the time-consuming elimination of the matrix [A] from the manipulations of the right-hand side {B}.

Thus, once [A] has been “decomposed,” multiple right-hand-side vectors can be evaluated in an efficient manner.

## LU Decomposition Method C++ Program

```.hljs{display:block;overflow-x:auto;padding:.5em;color:#abb2bf;background:#282c34}.hljs-keyword,.hljs-operator{color:#f92672}.hljs-pattern-match{color:#f92672}.hljs-pattern-match .hljs-constructor{color:#61aeee}.hljs-function{color:#61aeee}.hljs-function .hljs-params{color:#a6e22e}.hljs-function .hljs-params .hljs-typing{color:#fd971f}.hljs-module-access .hljs-module{color:#7e57c2}.hljs-constructor{color:#e2b93d}.hljs-constructor .hljs-string{color:#9ccc65}.hljs-comment,.hljs-quote{color:#b18eb1;font-style:italic}.hljs-doctag,.hljs-formula{color:#c678dd}.hljs-section,.hljs-name,.hljs-selector-tag,.hljs-deletion,.hljs-subst{color:#e06c75}.hljs-literal{color:#56b6c2}.hljs-string,.hljs-regexp,.hljs-addition,.hljs-attribute,.hljs-meta-string{color:#98c379}.hljs-built_in,.hljs-class .hljs-title{color:#e6c07b}.hljs-attr,.hljs-variable,.hljs-template-variable,.hljs-type,.hljs-selector-class,.hljs-selector-attr,.hljs-selector-pseudo,.hljs-number{color:#d19a66}.hljs-symbol,.hljs-bullet,.hljs-link,.hljs-meta,.hljs-selector-id,.hljs-title{color:#61aeee}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}.hljs-link{text-decoration:underline}.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines) {white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```  //wikkihut.com
//lu decomposition C++

#include<iostream>
#include<iomanip>

//size of matrix
# define N 3
typedef float matrix[N][N];
matrix l,u,a;

using namespace std;

float b[N],x[N],v[N];

//upper triangular matrix

void urow(int i)
{
float s;
int j,k;
for(j=i;j<N;j++)
{
s=0;
for(k=0;k<i;k++)
s+=u[k][j]*l[i][k];
u[i][j]=a[i][j]-s;
}
}

//lower triangular matrix

void lcol(int j)
{
float s;
int i,k;
for(i=j+1;i<N;i++)
{
s=0;
for(k=0;k<=i-1;k++)
s+=u[k][j]*l[i][k];
l[i][j]=(a[i][j]-s)/u[j][j];
}
}
//function to display the matrix

void printmat(matrix x)
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
cout<<setw(8)<<setprecision(4)<<x[i][j];
cout<<endl;
}
}
int main()
{
int i,j,m;
float s;
cout<<"Enter the elements of augmented matrix row wise"<<endl;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
cin>>a[i][j];
cin>>b[i];
}
for(i=0;i<N;i++)
l[i][i]=1.0;
for(m=0;m<N;m++)
{
urow(m);
if(m<(N-1))
lcol(m);
}
cout<<setw(14)<<"U \n"<<endl;
printmat(u);
cout<<endl;
cout<<setw(14)<<"L \n"<<endl;
printmat(l);

for(i=0;i<N;i++)
{
s=0;
for(j=0;j<=i-1;j++)
s+=l[i][j]*v[j];
v[j]=b[i]-s;
}
for(i=N-1;i>=0;i--)
{
s=0;
for(j=i+1;j<N;j++)
s+=u[i][j]*x[j];
x[i]=(v[i]-s)/u[i][i];
}
cout<<"The solution is: "<<endl;
for(i=0;i<N;i++)
cout<<"x["<<setw(3)<<i+1<<"] ="<<setw(6)<<setprecision(4)<<x[i]<<endl;
return 0;
}``````

## Mathematical Overview of LU Decomposition.

Just as was the case with Gauss elimination, LU decomposition requires pivoting to avoid division by zero. Let’s explain it using three simultaneous equations, then the result can be extended to n-dimensional system.
Let the equation be:
[A]{X} = {B}
The equation can be rearranged as:
[A]{X} − {B} = 0      (1)

The above equation can be expressed in upper triangular matrix as: The above matrix can be expressed in equation form as below:
[U]{X} = {D}    (2-1)
After rearranging the equation becomes
[U]{X} − {D} = 0      (2-2)

Now let’s assume that there is a lower triangular matrix with 1’s on diagonal.
i.e. [L]{X} = {D}    (3)

=>[U]{X} – {D}=0    (4)
Now when we multiply the lower and upper triangular matrix equations we get the first equation back:
[L]{[U]{X} − {D}} = [A]{X} − {B}.

If this equation holds, it follows from the rules for matrix multiplication that,
[L][U] = [A]            5.
and
[L]{D} = {B}            6.

## Two step strategy:

A two-step strategy for obtaining solutions can be based on Eqs.( 2-2),
(5), and (6):

1. LU decomposition step. [A] is factored or “decomposed” into lower [L] and upper [U] triangular matrices.
2. Substitution step. [L] and [U] are used to determine a solution {X} for a right-hand side {B}. This step itself consists of two steps.
3. First, Eq. (6) is used to generate an intermediate vector {D} by forward substitution. Then, the result is substituted into Eq. (2-2), which can be solved by back substitution for {X}.