#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
#define N 3
typedef float matrix[N][N];
matrix l,u,a;
float b[N],x[N],v[N];
void urow(int i)
{
float s;
int j,k;
for(j=i;j<N;j++)
{
s=0;
for(k=0;k<N-1;k++)
s += u[k][j]*l[i][k];
u[i][j]=a[i][j]-s;
}
}
void lcol(int j)
{
float s;
int i,k;
for(i=j+1;i<N;i++)
{
s=0;
for(k=0;k<=j-1;k++)
s += u[k][j]*l[i][k];
l[i][j]=(a[i][j]-s)/u[j][j];
}
}
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";
cout<<" matrix rowwise "<<endl;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
cin>>a[i][j];
cin>>b[i];
}
cout<<fixed;
//calculating the elements of l and uflow
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);
}
//printing the elements of l and u
cout<<setw(14)<<"U"<<endl;
printmat(u);
cout<<setw(14)<<"L"<<endl;
printmat(l);
//solving LV=B by forward substitution
for(i=0;i<N;i++)
{
s=0;
for(j=0;j<=i-1;j++)
s += l[i][j]*v[j];
v[i]=b[i]-s;
}
//solving UX=v by backward substitution
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];
}
//Results
cout<<"Solution is:-"<<endl;
for(i=0;i<N;i++)
{cout<<"x["<<setw(3)<<i+1<<"]=";
cout<<setw(6)<<setprecision(4);
cout<<x[i]<<endl;
}
return 0;
}
Output
Enter the elements of augmented matrix rowwise
3 2 7 4
2 3 1 5
3 4 1 7
U
3.0000 2.0000 7.0000
0.0000 1.6667 -3.6667
0.0000 0.0000 -1.6000
L
1.0000 0.0000 0.0000
0.6667 1.0000 0.0000
1.0000 1.2000 1.0000
Solution is:-
x[ 1]=0.8750
x[ 2]=1.1250
x[ 3]=-0.1250
