For this **C program for LU factorization**, consider a general linear system AX = b, such that the given matrix [A] is factorized into the product of two upper and lower triangular matrices. The solution of linear simultaneous equations sought this way is called LU factorization method. This method is also known as the Triangular method or the LU Decomposition method.

So, the basic principle here is – “A square matrix [A] can be written as the product of a lower triangular matrix [L] and an upper triangular matrix [U], one of them being unit triangular, if all the principal minors of [A] are non-singular. Now, see how the matrix undergoes decomposition in the program for **LU Factorization in C** language.

[A] = [L][U]

where, L is the lower triangular matrix and U is the upper triangular matrix. The elements of these three matrices row-wise are:

[A] = {a11, a12, a13, a21, a22, a23, a31, a32, a33}

[L] = {1, 0, 0, l21, 1, 0, l31, l32, 1}

[U] = {u11, u12, u13, 0, u22, u23, 0, 0, u33}

The representation obtained upon putting these elements in the 1st expression aforementioned is termed as LU factorization. It is simply a product of a lower and an upper triangular matrix decomposed from the parent matrix A. The diagonal non-zero entry of [L] is the ith pivot during factorization. The elements of matrix A can be obtained by multiplying [L] and [U].

This factorization method is preferred over the Gauss Elimination method in computers and programming languages like C. It facilitates obtaining products in double length. This C program below illustrates the application of LU Factorization method based on the things mentioned above.

## Source Code for LU Factorization in C:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#include<stdio.h> #include<conio.h> void main() { float A[20][20]= {0},L[20][20]= {0}, U[20][20]; float B[20]= {0}, X[20]= {0},Y[20]= {0}; int i,j,k,n; printf("Enter the order of square matrix: "); scanf("%d",&n); printf("\nEnter matrix element:\n"); for(i=0; i<n; i++) { for(j=0; j<n; j++) { printf("Enter A[%d][%d] element: ", i,j); scanf("%f",&A[i][j]); } } printf("\nEnter the constant terms: \n"); for(i=0; i<n; i++) { printf("B[%d]",i); scanf("%f",&B[i]); } for(j=0; j<n; j++) { for(i=0; i<n; i++) { if(i<=j) { U[i][j]=A[i][j]; for(k=0; k<i-1; k++) U[i][j]-=L[i][k]*U[k][j]; if(i==j) L[i][j]=1; else L[i][j]=0; } else { L[i][j]=A[i][j]; for(k=0; k<=j-1; k++) L[i][j]-=L[i][k]*U[k][j]; L[i][j]/=U[j][j]; U[i][j]=0; } } } printf("[L]: \n"); for(i=0; i<n; i++) { for(j=0; j<n; j++) printf("%9.3f",L[i][j]); printf("\n"); } printf("\n\n[U]: \n"); for(i=0; i<n; i++) { for(j=0; j<n; j++) printf("%9.3f",U[i][j]); printf("\n"); } for(i=0; i<n; i++) { Y[i]=B[i]; for(j=0; j<i; j++) { Y[i]-=L[i][j]*Y[j]; } } printf("\n\n[Y]: \n"); for(i=0; i<n; i++) { printf("%9.3f",Y[i]); } for(i=n-1; i>=0; i--) { X[i]= Y[i]; for(j=i+1; j<n; j++) { X[i]-=U[i][j]*X[j]; } X[i]/=U[i][i]; } printf("\n\n[X]: \n"); for(i=0; i<n; i++) { printf("%9.3f",X[i]); } getch(); } |

### Input/Output:

Also see,

LU Decomposition Algorithm/Flowchart

Numerical Methods Tutorial Compilation

If you have any questions related to this post – the LU Factorization (LU Decomposition) method or its C source code presented above, do mention them in the comments section.

Answer should be X = [3;2;4;9]

that is good

is this LU factorization by doolittle method ?

Yes.

then replace line 32 with this

for(k=0; k<=i-1; k++)

Hi, I donĀ“t understand why you put 9.3f in the [U] matrix. Thank you

Here, 9 specifies maximum field width, whereas the 3 known as the “Precision Specifier” allocates how many digits after the decimal point.

Printing 3.141592 using %f displays 3.141592

Printing 3.141592 using %1.1f displays 3.1

Printing 3.141592 using %1.2f displays 3.14

Printing 3.141592 using %3.3f displays 3.142

Printing 3.141592 using %4.4f displays 3.1416

Printing 3.141592 using %4.5f displays 3.14159

Printing 3.141592 using %09.3f displays 00003.142

Printing 3.141592 using %9.3f displays _ _ _ _ 3.142.

printf(“9.3f”,U[i][j]); lacks a percent sign before the 9.

It seems printf(“%9.3f”,U[i][j]); was intended instead.

You are right. Thanks for noticing that.