Numerical Methods

Jacobi Iteration Method


Program in C
================================================================================================== Program to find solutions of a system of linear equations by Jacobi 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],prevX[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])lt; sumRow || fabs(a[i][i])lt; 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; prevX[j] = 0; //initializing x = 0 } iterations = 1; printf("\nThe solutions are:\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]*prevX[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]); for(j=0;j<n;j++){prevX[j]=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:20 Enter the coefficient matrix: 6 1 1 1 4 -1 1 -1 5 Enter the vector b: 20 6 7 The solutions are: Iteration 1 x1= 3.333333 x2= 1.500000 x3= 1.400000 Iteration 2 x1= 2.850000 x2= 1.016667 x3= 1.033333 Iteration 3 x1= 2.991667 x2= 1.045833 x3= 1.033333 Iteration 4 x1= 2.986806 x2= 1.010417 x3= 1.010833 Iteration 5 x1= 2.996458 x2= 1.006007 x3= 1.004722 Iteration 6 x1= 2.998212 x2= 1.002066 x3= 1.001910 Iteration 7 x1= 2.999337 x2= 1.000924 x3= 1.000771 Iteration 8 x1= 2.999717 x2= 1.000358 x3= 1.000317 Iteration 9 x1= 2.999887 x2= 1.000150 x3= 1.000128 Iteration 10 x1= 2.999954 x2= 1.000060 x3= 1.000053 Iteration 11 x1= 2.999981 x2= 1.000025 x3= 1.000021 Iteration 12 x1= 2.999992 x2= 1.000010 x3= 1.000009 Iteration 13 x1= 2.999997 x2= 1.000004 x3= 1.000004 Iteration 14 x1= 2.999999 x2= 1.000002 x3= 1.000001 Iteration 15 x1= 2.999999 x2= 1.000001 x3= 1.000001 Iteration 16 x1= 3.000000 x2= 1.000000 x3= 1.000000 Iteration 17 x1= 3.000000 x2= 1.000000 x3= 1.000000 Iteration 18 x1= 3.000000 x2= 1.000000 x3= 1.000000 Iteration 19 x1= 3.000000 x2= 1.000000 x3= 1.000000 Iteration 20 x1= 3.000000 x2= 1.000000 x3= 1.000000 The Solutions are: x1= 3.000000 x2= 1.000000 x3= 1.000000
Program in Fortran 95
===================================================================================
Program to find solutions of a system of linear equations by
Jacobi Iteration Method (The coefficient matrix must be Diagonally Dominant)
LANGUAGE :: FORTRAN 95  
Compiler :: GNU Fortran
===================================================================================

PROGRAM Jacobi_Iteration
  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(:),prevX(:)
  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))
  ALLOCATE (prevX(n))
  x = 0                     !initializing x = 0
  prevX = 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)*prevX(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)
      DO j=1,n
        prevX(j)=x(j)
      END DO
      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
20
 Enter the coefficient matrix
6 1 1
1 4 -1
1 -1 5
 Enter the vector b:
20 6 7
 The iterations are:
Iteration- 1:   x1=  3.33333325  x2=  1.50000000  x3=  1.39999998
Iteration- 2:   x1=  2.85000014  x2=  1.01666665  x3=  1.03333342
Iteration- 3:   x1=  2.99166679  x2=  1.04583335  x3=  1.03333318
Iteration- 4:   x1=  2.98680568  x2=  1.01041663  x3=  1.01083338
Iteration- 5:   x1=  2.99645829  x2=  1.00600696  x3=  1.00472212
Iteration- 6:   x1=  2.99821186  x2=  1.00206590  x3=  1.00190985
Iteration- 7:   x1=  2.99933743  x2=  1.00092447  x3=  1.00077081
Iteration- 8:   x1=  2.99971747  x2=  1.00035834  x3=  1.00031745
Iteration- 9:   x1=  2.99988747  x2=  1.00014997  x3=  1.00012815
Iteration-10:   x1=  2.99995351  x2=  1.00006020  x3=  1.00005245
Iteration-11:   x1=  2.99998093  x2=  1.00002480  x3=  1.00002134
Iteration-12:   x1=  2.99999237  x2=  1.00001013  x3=  1.00000882
Iteration-13:   x1=  2.99999690  x2=  1.00000405  x3=  1.00000358
Iteration-14:   x1=  2.99999881  x2=  1.00000167  x3=  1.00000131
Iteration-15:   x1=  2.99999928  x2=  1.00000060  x3=  1.00000060
Iteration-16:   x1=  3.00000000  x2=  1.00000036  x3=  1.00000024
Iteration-17:   x1=  3.00000000  x2=  1.00000000  x3=  1.00000012
Iteration-18:   x1=  3.00000000  x2=  1.00000000  x3=  1.00000000
Iteration-19:   x1=  3.00000000  x2=  1.00000000  x3=  1.00000000
Iteration-20:   x1=  3.00000000  x2=  1.00000000  x3=  1.00000000
 The solutions are:
  x1 =   3.00000000
  x2 =   1.00000000
  x3 =   1.00000000