Friday, June 24, 2016

GAUSS SEIDAL METHOD


             Gauss Seidel method is one of the iterative methods to solve the simultaneous algebraic equations. It is the most easy and fastest method among all other iterative method to solve the simultaneous algebraic equations.


Program code for Gauss Seidel method

Below is the Program code for Gauss Seidel method written in C programming using code blocks libraries.


#include<stdio.h>
#include<math.h>
#define e 0.000001
int main()
{
    float a[20][20];
    int i,j,n;
    float sum=0;
    float x[20],y[20],error[20],errormax;
    printf("enter no of equation:");
    scanf("%d",&n);
    printf("Enter the augumented matrix:");
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n+1;j++)
        {
            printf("a[%d][%d]=",i,j);
            scanf("%f",&a[i][j]);
        }
    }
    for(i=1;i<=n;i++)
    {
        x[i]=a[i][n+1]/a[i][i];
    }
    do
    {
     for(i=1;i<=n;i++)
     {
         sum=0;
         for(j=1;j<=n;j++)
         {
             if(i!=j)
                 sum=sum+a[i][j]*x[j];
         }
         y[i]=(a[i][n+1]-sum)/a[i][i];
         error[i]=fabs(y[i]-x[i]);
         x[i]=y[i];
     }
     errormax=error[1]; // maxm error among error vector
     for(i=2;i<=n;i++)
     {
         if(errormax<error[i])
            errormax=error[i];
     }
    }while(fabs(errormax)>e);
    for(i=1;i<=n;i++)
    {
       printf("result is %f",x[i]);
    }
return 0;

}

No comments:

Post a Comment