Numerical Methods

Gauss-Jordan Elimination method


Program in C
================================================================================================== Program to find solutions of a system of linear equations by Gauss-Jordan Elimination Method (The coefficient matrix must be Diagonally Dominant) LANGUAGE :: C (Compiler: GNU GCC Compiler) ==================================================================================================
#include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int n; printf("Enter the number of variables: "); scanf("%d",&n); float a[n][n+1]; //The augmented matrix int i,j,k; float solution,multiplier; printf("\nEnter the augmented matrix:\n"); for (i = 0;i < n;i++){ for (j = 0;j < n+1;j++){ scanf("%f",&a[i][j]); //reading the matrix } } printf("\n"); //------------ to get the diagonal matrix -------------------| for (k = 0;k < n;k++){ for (i=0; i < n; i++){ if(i!= k){ multiplier = a[i][k]/a[k][k]; for (j=0; j < n+1; j++){ a[i][j] = a[i][j] - a[k][j]*multiplier; } } } } //-------------- Printing the diagonal matrix -----------| printf("\nThe diagonal matrix:\n"); for (i = 0;i < n;i++){ for (j = 0;j < n+1;j++){ printf("\t%f",a[i][j]); } printf("\n"); } // -------------------Printing the Solutions---------------------| printf("\nThe Solutions are:\n"); for (i = 0;i < n;i++){ solution = a[i][n]/a[i][i]; printf("\t x%d = %f",i+1,solution); } printf("\n"); return 0; } ========================= OUTPUT ============================ Enter the number of variables: 3 Enter the augmented matrix: 1 1 1 9 7 -5 4 9 2 3 8 10 The diagonal matrix: 1.000000 0.000000 0.000000 6.130435 0.000000 -12.000000 0.000000 -60.521740 0.000000 0.000000 5.750000 -12.500000 The Solutions are: x1 = 6.130435 x2 = 5.043478 x3 = -2.173913
Program in Fortran 95
===================================================================================
Program to find solutions of a system of linear equations by
Gauss-Jordan Elimination Method 
(The coefficient matrix must be Diagonally Dominant)
LANGUAGE :: FORTRAN 95  
Compiler :: GNU Fortran
===================================================================================

PROGRAM gaussJordanElimination
  IMPLICIT NONE

  INTEGER :: n,i,j,k
  REAL,ALLOCATABLE :: a(:,:)            !The augmented matrix 'a'
  REAL multiplier, solution
  WRITE(*,*)'Enter the number of variables'
  READ(*,*)n
  ALLOCATE (a(n,n+1))
  WRITE(*,*)'Enter the augmented matrix:'
  DO i=1,n
      READ(*,*)(a(i,j),j=1,n+1)         !reading the matrix
  END DO
  !!------------ to get the diagonal matrix -------------------|
  DO k = 1,n
      DO i=1,n
        IF(i.NE.k) THEN
          multiplier = a(i,k) / a(k,k)
          DO j=1,n+1
              a(i,j) = a(i,j) - a(k,j)*multiplier
          END DO
        ENDIF
      END DO
  END DO
  !!-------------- Printing the diagonal matrix -----------|
  WRITE(*,'(/)')
  WRITE(*,*)'The diagonal matrix:'
  DO i = 1,n
      WRITE(*,'(*(2X,F10.6))')(a(i,j),j=1,n+1)
  END DO
  !! -------------------Printing the Solutions---------------------|
  WRITE(*,'(/)')
  WRITE(*,*)'The Solutions are:'
  DO j = 1,n
      solution = a(j,n+1)/a(j,j)
      WRITE(*,'(2X,A1,I1,A1,F10.6,2X)')'x',j,'=',solution
  END DO
END PROGRAM

===================---------- OUTPUT ----------===================
 Enter the number of variables
 3
 Enter the augmented matrix:
 5 -2 3 18
 1 7 -3 -22
 2 -1 6 22


 The diagonal matrix:
    5.000000   -0.000000    0.000000    5.000000
   -0.000000    7.400000    0.000000  -14.800001
   -0.000000    0.000000    4.702703   14.108109


 The Solutions are:
  x1=  1.000000
  x2= -2.000000
  x3=  3.000000