#include<iostream>
using namespace std;
int main()
{
int i,j,k,n,p;
float a[10][10],l[10][10]={0},u[10][10]={0},b[10],z[10]={0},x[10]={0};
cout<<"Enter order of matrix: ";
cin>>n;
cout<<"Enter the coefficients of matrix A\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"Enter Elements of Matrix B\n";
for(i=1;i<=n;i++)
cin>>b[i];
float sum=0.0;
for(k=1;k<=n;k++)
{
u[k][k]=1;
for(i=k;i<=n;i++)
{
sum=0;
for(p=1;p<=k-1;p++)
sum+=l[i][p]*u[p][k];
l[i][k]=a[i][k]-sum;
}
for(j=k+1;j<=n;j++)
{
sum=0;
for(p=1;p<=k-1;p++)
sum+=l[k][p]*u[p][j];
u[k][j]=(a[k][j]-sum)/l[k][k];
}
}
cout<<"\nMatrix L & U\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cout<<l[i][j]<<" ";
cout<<endl;
}
cout<<endl;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cout<<u[i][j]<<" ";
cout<<endl;
}
//Forward Substitution LZ=b
for(i=1;i<=n;i++)
{
sum=0;
for(p=1;p<i;p++)
sum+=l[i][p]*z[p];
z[i]=(b[i]-sum)/l[i][i];
}
//Finding X ; UX=Z
for(i=n;i>0;i--)
{
sum=0;
for(p=n;p>i;p--)
sum+=u[i][p]*x[p];
x[i]=(z[i]-sum)/u[i][i];
}
//Printing solution
for(i=1;i<=n;i++)
cout<<"\nX"<<i<<"= "<<x[i];
0;
}
return
using namespace std;
int main()
{
int i,j,k,n,p;
float a[10][10],l[10][10]={0},u[10][10]={0},b[10],z[10]={0},x[10]={0};
cout<<"Enter order of matrix: ";
cin>>n;
cout<<"Enter the coefficients of matrix A\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cin>>a[i][j];
}
cout<<"Enter Elements of Matrix B\n";
for(i=1;i<=n;i++)
cin>>b[i];
float sum=0.0;
for(k=1;k<=n;k++)
{
u[k][k]=1;
for(i=k;i<=n;i++)
{
sum=0;
for(p=1;p<=k-1;p++)
sum+=l[i][p]*u[p][k];
l[i][k]=a[i][k]-sum;
}
for(j=k+1;j<=n;j++)
{
sum=0;
for(p=1;p<=k-1;p++)
sum+=l[k][p]*u[p][j];
u[k][j]=(a[k][j]-sum)/l[k][k];
}
}
cout<<"\nMatrix L & U\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cout<<l[i][j]<<" ";
cout<<endl;
}
cout<<endl;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
cout<<u[i][j]<<" ";
cout<<endl;
}
//Forward Substitution LZ=b
for(i=1;i<=n;i++)
{
sum=0;
for(p=1;p<i;p++)
sum+=l[i][p]*z[p];
z[i]=(b[i]-sum)/l[i][i];
}
//Finding X ; UX=Z
for(i=n;i>0;i--)
{
sum=0;
for(p=n;p>i;p--)
sum+=u[i][p]*x[p];
x[i]=(z[i]-sum)/u[i][i];
}
//Printing solution
for(i=1;i<=n;i++)
cout<<"\nX"<<i<<"= "<<x[i];
0;
}
return
No comments:
Post a Comment