Tuesday, October 21, 2014

Gauss-Seidel method

#include<iostream.h>
#include<math.h>
void main()
{
            float x[10],y[10],a[10][10];
            int i,j,n,k,iteration;
            cout<<"\nEnter the order of the matrix.......=";
            cin>>n;

            cout<<"\nEnter the co-efficents and RHS of the matrix=\n";
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n+1;j++)
                {
                    cin>>a[i][j];
                }
            }
            cout<<"\nYOUR MATRIX IS\n";
         for(i=1;i<=n;i++)
            {
                for(j=1;j<=n+1;j++)
                {
                    cout<<"\t"<<a[i][j];
                }
            cout<<"\n";
            }
         cout<<"\nTHIS IS THE FOLLOWING ITERATION\n";
            for(i=1;i<=n;i++)
            {
                x[i]=0.0;
                y[i]=0.0;
            }
            iteration=0;
    step1:     iteration++;
            for(i=1;i<=n;i++)
            {
                x[i]=a[i][n+1];
                for(j=1;j<=n;j++)
                {
                    if(i==j)
                    continue;
                    x[i]=x[i]-a[i][j]*x[j];
                }
                x[i]=x[i]/a[i][i];
            }
            for(k=1;k<=n;k++)
            {
                if( fabs(x[k]-y[k])> 0.00005)
                {
                    cout<<"\n\nIteration="<<iteration;
                    for(i=1;i<=n;i++)
                    {
                        y[i]=x[i];
                        cout<<"\nx["<<i<<"]="<<x[i];
                    }
                    goto step1;
                }
            }
            cout<<"\nThis is the final value";
}

No comments:

Post a Comment