Sunday, September 18, 2011

Numerical Methods: Solution of simultaneous algebraic equations using Gauss Jordan method in C

Source Code:

/*****************************************************
    Program: solution of linear algebraic equations
    using Gauss Jordan Method
    Language used: C
    Program written by Bibek Subedi
    July 2, 2011
    Tribhuvan University
*****************************************************/
#include<stdio.h>
int main()
{
    double matrix[10][10],a,b;
    int  i, j, k, n;
    printf("Enter the no of variables: ");
    scanf("%d", &n);
    printf("Enter the agumented matrix:\n");
    for(i = 0; i < n ; i++){
        for(j = 0; j < (n+1); j++){
            scanf("%lf", &matrix[i][j]);
        }
    }
    for(i = 0; i < n; i++){
       for(j = 0; j < n; j++){
            if(i != j){
                a = matrix[j][i];
                b = matrix[i][i];
                for(k = 0; k < n+1; k++){
                    matrix[j][k] = matrix[j][k] - (a/b) * matrix[i][k];
                }
            }
        }
    }
    for(i = 0; i < n; i++){
         a = matrix[i][i];
        for(j = 0; j < n+1; j++){
            matrix[i][j] /= a;
        }
    }
    printf("The required solution is: \n\n");
    for(i = 0; i < n ; i++){
        printf("%c => %.2f", i+97, matrix[i][n]);
        printf("\n");
    }
    return 0;
}

No comments:

Post a Comment