Numerical Methods

Gauss-Siedel Iteration Method


Program in C
================================================================================================== Program to find solutions of a system of linear equations by Gauss-Siedel Iteration Method (The coefficient matrix must be Diagonally Dominant) LANGUAGE :: C (Compiler: GNU GCC Compiler) ==================================================================================================
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <stdbool.h> int main() { int n; //Number of variables in the system int maxItr; //Maximum number of iterations to be calculated printf("Enter the number of variables:"); scanf("%d",&n); printf("Enter the maximum number of iterations:"); scanf("%d",&maxItr); double a[n][n]; //The Coefficient matrix double b[n],x[n]; double sumRow, sumCol; int iterations,i,j; bool diagonallyDominant = true; printf("\nEnter the coefficient matrix:\n"); for(i=0; i < n; i++){ for(j=0; j < n; j++){ scanf("%lf",&a[i][j]); } } //------ Checking whether the matrix 'a' is diagonally dominant ------ for(i=0; i < n; i++){ sumRow = 0;sumCol =0; for(j=0; j < n; j++){ if(i!=j){ sumRow += fabs(a[i][j]); //sum of non-diagonal elements in a row sumCol += fabs(a[j][i]); //sum of non-diagonal elements in a column } } if (fabs(a[i][i])< sumRow || fabs(a[i][i])< sumCol) diagonallyDominant = false; } if(diagonallyDominant == false){ printf("\nError! The matrix is not diagonally dominant.\n"); return 0; } printf("\nEnter the vector b:\n"); // For the matrix equation Ax = b for(j=0; j < n; j++){ scanf("%lf",&b[j]); x[j] = 0; //initializing x = 0 } iterations = 1; printf("\nThe Iterations:\n"); while (iterations < = maxItr){ for(i=0; i < n; i++){ x[i] = b[i]; for(j=0; j < n; j++){ if(i!=j){ x[i] = x[i]-a[i][j]*x[j]; } } x[i] = x[i]/a[i][i]; } printf("\nIteration %d",iterations); for(j =0; j < n; j++ ){ printf("\tx%d= %lf",j+1,x[j]); } iterations++; } printf("\n\nThe Solutions are:"); for(j =0; j < n; j++ ) printf("\n\tx%d= %lf",j+1,x[j]); return 0; }
========================= OUTPUT ============================
Enter the number of variables:3
Enter the maximum number of iterations:10

Enter the coefficient matrix:
28 4 -1
2 17  4
1  3 10

Enter the vector b:
32
35
24

The solutions are:

Iteration 1     x1= 1.142857    x2= 1.924370    x3= 1.708403
Iteration 2     x1= 0.928962    x2= 1.547557    x3= 1.842837
Iteration 3     x1= 0.987593    x2= 1.509027    x3= 1.848532
Iteration 4     x1= 0.993301    x2= 1.507016    x3= 1.848565
Iteration 5     x1= 0.993589    x2= 1.506974    x3= 1.848549
Iteration 6     x1= 0.993595    x2= 1.506977    x3= 1.848547
Iteration 7     x1= 0.993594    x2= 1.506978    x3= 1.848547
Iteration 8     x1= 0.993594    x2= 1.506978    x3= 1.848547
Iteration 9     x1= 0.993594    x2= 1.506978    x3= 1.848547
Iteration 10    x1= 0.993594    x2= 1.506978    x3= 1.848547

The Solutions are:
        x1= 0.993594
        x2= 1.506978
        x3= 1.848547
Program in Fortran 95
===================================================================================
Program to find solutions of a system of linear equations by
GAUSS-SIEDEL Iteration Method 
(The coefficient matrix must be Diagonally Dominant)
LANGUAGE :: FORTRAN 95  
Compiler :: GNU Fortran
===================================================================================

PROGRAM Gauss_Siedel
  IMPLICIT NONE
  INTEGER :: n,i,j                  !n=Number of variables in the system
  INTEGER :: iterations,maxItr      !Maximum number of iterations to be calculated
  REAL,ALLOCATABLE :: a(:,:)
  REAL,ALLOCATABLE :: b(:),x(:)
  REAL :: sumRow,sumCol
  LOGICAL diagonallyDominant
  diagonallyDominant = .TRUE.
  WRITE(*,*)'Enter the number of variables'
  READ(*,*) n
  WRITE(*,*)'Enter the maximum number of iterations'
  READ(*,*) maxItr
  ALLOCATE (a(n,n))
  ALLOCATE (b(n))
  ALLOCATE (x(n))
  x = 0                     !initializing x = 0
  WRITE(*,*)'Enter the coefficient matrix'
  DO i=1,n
    READ(*,*)(a(i,j),j=1,n)          !Reading the coefficient matrix
  END DO
!------ Checking whether the matrix 'a' is diagonally dominant ------
  DO i=1,n
      sumRow = 0
      sumCol = 0
      DO j=1,n
          IF(i/=j)THEN
              sumRow = sumRow+abs(a(i,j))        !sum of non-diagonal elements in a row
              sumCol = sumCol+abs(a(j,I))
          END IF
      END DO
      IF (abs(a(i,i))< sumRow .OR. abs(a(i,i))< sumCol) diagonallyDominant = .FALSE.
  END DO

  IF(.NOT.diagonallyDominant)THEN
      WRITE(*,*)'Error! The matrix is not diagonally dominant'
      STOP
  END IF
  WRITE(*,*)'Enter the vector b:'                !For the matrix equation Ax = b
  READ(*,*)(b(j),j=1,n)

  iterations = 1
  WRITE(*,*)'The iterations are:'
  DO WHILE (iterations <= maxItr)
      DO i=1,n
          x(i) = b(i)
          DO j=1,n
              IF (i/=j)  x(i) = x(i)-a(i,j)*x(j)
          END DO
          x(i) = x(i)/ a(i,i)
      END DO
      10 FORMAT ('Iteration-',I2,': ',*(2X,A1,I1,'=',F12.8))
      WRITE(*,10)iterations,('x',j,x(j),j=1,n)
      iterations = iterations+1
  END DO
  WRITE(*,*)'The solutions are:'
  11 FORMAT ((2X,A1,I1,1X,A1,1X,F12.8))
  WRITE(*,11)('x',j,'=',x(j),j=1,n)
END PROGRAM


============================OUTPUT===============================
 Enter the number of variables
 3
 Enter the maximum number of iterations
 10
 Enter the coefficient matrix
 28 4 -1
 2 17 4
 1 3 10
 Enter the vector b:
 32
 35
 24
 The iterations are:
Iteration- 1:   x1=  1.14285719  x2=  1.92436981  x3=  1.70840335
Iteration- 2:   x1=  0.92896158  x2=  1.54755676  x3=  1.84283698
Iteration- 3:   x1=  0.98759317  x2=  1.50902748  x3=  1.84853244
Iteration- 4:   x1=  0.99330080  x2=  1.50701571  x3=  1.84856510
Iteration- 5:   x1=  0.99358940  x2=  1.50697422  x3=  1.84854889
Iteration- 6:   x1=  0.99359471  x2=  1.50697732  x3=  1.84854734
Iteration- 7:   x1=  0.99359423  x2=  1.50697768  x3=  1.84854722
Iteration- 8:   x1=  0.99359417  x2=  1.50697780  x3=  1.84854722
Iteration- 9:   x1=  0.99359411  x2=  1.50697803  x3=  1.84854734
Iteration-10:   x1=  0.99359411  x2=  1.50697792  x3=  1.84854734
 The solutions are:
  x1 =   0.99359411
  x2 =   1.50697792
  x3 =   1.84854734