Friday, June 24, 2016

GAUSS JORDAN METHOD


            Gauss Jordan method is the modification Gauss Elimination method hence it is also one of the procedures to solve the simultaneous algebraic equation.
          In this method, elimination of unknown is performed not in the equation below but in the equation above also, reducing the system to a diagonal matrix form i.e. each equation involving only one unknown. From these equations the unknowns can be obtained readily.
          Thus in this method, the labor of back substitution for finding the unknowns is saved at the cost of addition calculation.

Source Code

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int i,n,j,k;
    float pivot,f,x[10],sum=0;
    float a[10][10];
    printf("\n\n\t\tEnter the no of equations::\n\t");
    scanf("%d",&n);
    printf("\n\t\tEnter the matrix:\n");
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n+1;j++)
        {
            printf("\t\ta[%d][%d]=",i,j);
            scanf("%f",&a[i][j]);
        }
        printf("\n\n");
    }
    for(k=1;k<=n-1;k++)
    {
        pivot=a[k][k];
        for(i=k+1;i<=n;i++)
        {
            f=a[i][k]/pivot;
            for(j=k;j<=(n+1);j++)
            {
                a[i][j]=a[i][j]-f*a[k][j];
            }
        }
    }

    for(k=n;k>=2;k--)
    {
        pivot=a[k][k];
        for(i=k-1;i>=1;i--)
        {
            f=a[i][k]/pivot;
            for(j=k;j<=(n+1);j++)
            {
                a[i][j]=a[i][j]-f*a[k][j];
            }
        }
    }
    printf("\n\n\t\t The diagnol matrix is:\n\n");
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=(n+1);j++)
        {
            printf("\t%f",a[i][j]);
        }
        printf("\n");
    }
    for(i=1;i<=n;i++)
    {
        x[i]=(a[i][n+1])/a[i][i];
    }
    printf("\n\tThe value is:\n\n");
    for(i=1;i<=n;i++)
    {
       printf("\tx%d=%f\t",i,x[i]);
    }
    getch();
}


Sample output:


In this output only two equations is taken for references. Any number of equations can be taken.

The output of Gauss Jordan method is shown as:




( Note: Above source code is written using C programming in TurboC++)

No comments:

Post a Comment