/*GAUSS-JORDAN-ELIMINATION-METHOD*/
#include<iostream.h>
void main()
{
float a[10][10],b[10][10],r;
int i,j,k,n;
cout<<"\nEnter the order of the matrix=";
cin>>n;
cout<<"\nEnter A matrix row-wise \n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<endl;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
b[i][j]=0.0;
b[i][i]=1.0;
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
{
if(i==k)
continue;
r=a[i][k]/a[k][k];
for(j=1;j<=n;j++)
{
a[i][j]-=r*a[k][j];
b[i][j]-=r*b[k][j];
}
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
b[i][j]=b[i][j]/a[i][i];
cout<<"\nThe Inverse Matrix is=\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<endl;
}
float x[10],y[10],s;
y[0]=0;
cout<<"\n Enter RHS value=";
for(i=1;i<=3;i++)
{
cin>>x[i];
}
int l=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
y[i]+=b[i][j]*x[j];
}
}
for(i=1;i<=3;i++)
{
cout<<endl<<"x["<<i<<"]="<<y[i];
}
}
#include<iostream.h>
void main()
{
float a[10][10],b[10][10],r;
int i,j,k,n;
cout<<"\nEnter the order of the matrix=";
cin>>n;
cout<<"\nEnter A matrix row-wise \n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<endl;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
b[i][j]=0.0;
b[i][i]=1.0;
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
{
if(i==k)
continue;
r=a[i][k]/a[k][k];
for(j=1;j<=n;j++)
{
a[i][j]-=r*a[k][j];
b[i][j]-=r*b[k][j];
}
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
b[i][j]=b[i][j]/a[i][i];
cout<<"\nThe Inverse Matrix is=\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<"\t"<<b[i][j];
}
cout<<endl;
}
float x[10],y[10],s;
y[0]=0;
cout<<"\n Enter RHS value=";
for(i=1;i<=3;i++)
{
cin>>x[i];
}
int l=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
y[i]+=b[i][j]*x[j];
}
}
for(i=1;i<=3;i++)
{
cout<<endl<<"x["<<i<<"]="<<y[i];
}
}
No comments:
Post a Comment