#include<iostream>
#include<math.h>
#include<iomanip>
#define err 0.0001
using namespace std;
int main()
{
float a[20][20],x[20],e;
int i,j,k,n,max;
cout<<"Enter Order of equation: ";
cin>>n;
for(i=0;i<n;i++)
x[i]=0;
cout<<"Enter Coefficients of equation row-wise:\n";
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
cin>>a[i][j];
}
cout<<"Enter Maximum No of iterations: ";
cin>>max;
cout<<setprecision(4);
for(k=0;k<max;k++)
{
for(i=0;i<n;i++)
{
float sum=0.0;
for(j=0;j<n;j++)
{
if(j!=i)
sum+=a[i][j]*x[j];
}
float temp=(a[i][n]-sum)/a[i][i];
e=fabs(x[i]-temp);
x[i]=temp;
}
cout<<"Iteration "<<k<<"\t";
for(i=0;i<n;i++)
cout<<x[i]<<"\t";
cout<<"\n";
if(e<err)
{
cout<<"\nSolution Converges in "<<k<<" iteration\n";
for(i=0;i<n;i++)
cout<<"X"<<i<
return 1;
}
}
return 0;
}
<"=\t"<<x[i]<<endl;
#include<math.h>
#include<iomanip>
#define err 0.0001
using namespace std;
int main()
{
float a[20][20],x[20],e;
int i,j,k,n,max;
cout<<"Enter Order of equation: ";
cin>>n;
for(i=0;i<n;i++)
x[i]=0;
cout<<"Enter Coefficients of equation row-wise:\n";
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
cin>>a[i][j];
}
cout<<"Enter Maximum No of iterations: ";
cin>>max;
cout<<setprecision(4);
for(k=0;k<max;k++)
{
for(i=0;i<n;i++)
{
float sum=0.0;
for(j=0;j<n;j++)
{
if(j!=i)
sum+=a[i][j]*x[j];
}
float temp=(a[i][n]-sum)/a[i][i];
e=fabs(x[i]-temp);
x[i]=temp;
}
cout<<"Iteration "<<k<<"\t";
for(i=0;i<n;i++)
cout<<x[i]<<"\t";
cout<<"\n";
if(e<err)
{
cout<<"\nSolution Converges in "<<k<<" iteration\n";
for(i=0;i<n;i++)
cout<<"X"<<i<
return 1;
}
}
return 0;
}
<"=\t"<<x[i]<<endl;
No comments:
Post a Comment