-
Notifications
You must be signed in to change notification settings - Fork 1
/
LU_factorization.c
71 lines (70 loc) · 1.46 KB
/
LU_factorization.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
#include<stdio.h>
void main()
{
float a[10][10],l[10][10],u[10][10],z[10],x[10],b[10];
int i,j,k,n;
printf("\nEnter the size of the coefficient matrix: ");
scanf("%d",&n);
printf("Enter the elements row-wise: ");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
printf("Enter the right hand vector: ");
for(i=0;i<n;i++)
scanf("%f",&b[i]);
for(i=0;i<n;i++)
l[i][1]=a[i][1];
for(j=2;j<=n;j++)
u[1][j]=a[1][j]/l[1][1];
for(i=0;i<n;i++)
u[i][i]=1;
for(i=2;i<=n;i++)
for(j=2;j<=n;j++)
if(i>=j)
{
l[i][j]=a[i][j];
for(k=1;k<=j-1;k++)
l[i][j]-=l[i][k]*u[k][j];
}
else
{
u[i][j]=a[i][j];
for(k=1;k<=j-1;k++)
u[i][j]=-l[i][k]*u[k][j];
u[i][j]/=l[i][i];
}
printf("\nThe lower triangular matrix L: \n");
for(i=0;i<=n;i++)
{
for(j=0;j<n;j++)
printf("%f ",l[i][j]);
printf("\n");
}
printf("\nThe upper triangular matrix U: \n");
for(i=0;i<=n;i++)
{
for(j=0;j<i;j++)
printf(" ");
for(j=i;j<n;j++)
printf("%f ",l[i][j]);
printf("\n");
}
z[1]=b[1]/l[1][1];
for(j=1;i<n;i++)
{
z[i]=b[i];
for(j=0;j<i-1;j++)
z[i]-=l[i][j]*z[j];
z[i]/=l[i][i];
}
x[n]=z[n];
for(i=n-1;i>=0;i--)
{
x[i]=z[i];
for(j=i+1;j<n;j++)
x[i]-=u[i][j]*x[j];
}
printf("The solution is: ");
for(i=0;i<n;i++)
printf("%f ",x[i]);
}