Gauss Jacobi Method

Algorithm : Gauss-Jacobi Method GaussJacobi(a,b,n) { This is an algorithm to find the roots of the equation using Gauss Jacobi method. Where a is the coefficient matrix and b is the constant matrix of size n. The limitation of this method is that the matrix has to be strictly diagonally dominant i.e a(1,i)>≠|a(i,j)| for j=1 to n and j≠i. for(i=0 to i<n) { prex[i]=0 // It initializes the previous and present values to 0 newx[i]=0 } do { for(i=0 to i<n) { sum=0 for(j=0 to j<n) { if(i!=j) sum=sum+a[i][j]*prex[j] } newx[i]=(b[i]-sum)/a[i][i] } error=0 for(i=0 to i<n) { error=error+fabs(prex[i]-newx[i]) prex[i]=newx[i] } } while(error>e) //To check the precision for(i=0 to i<n) print("%2f ",newx[i]) // Prints the root print("\n\n") } C Program For Gauss Jacobi Method Source Code: #include<stdio.h> #include<conio.h> #include<math.h> void show(float A[20][20],int n) { int i,j; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("\t%g",A[i][j]); } printf("\n"); } } void input(float A[20][20],int n) { int i,j; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("\nEnter term %d of equation %d:",j,i); scanf("%f",&A[i][j]); } } } void build(float A[20][20],float B[20],int n) { float max,sum,C[20],D[20],y; int i,j,pos,k,h; for(i=1;i<=n;i++) { max=-9999; for(j=1;j<=n;j++) { if(A[i][j]>max) { max=A[i][j]; pos=j; } } y=B[i]; B[i]=B[pos]; B[pos]=y; for(k=1;k<=n;k++) { C[k]=A[i][k]; D[k]=A[pos][k]; } for(k=1;k<=n;k++) { A[i][k]=D[k]; A[pos][k]=C[k]; } } for(h=1;h<=n;h++) { printf("\n%g",B[h]); } for(i=1;i<=n;i++) { sum=0; for(j=1;j<=n;j++) { if(i!=j) { sum=sum+fabs(A[i][j]); } } if(fabs(A[i][i])<=sum) { printf("\nNot a strictly diagonally matrix"); getch(); exit(0); } } } void gauss_jacobi(float A[20][20],float B[20],int n) { float E=0.000001,sum,PREVX[20],NEWX[20],error; int i,j; printf("\nThe co-efficient matrix(A):\n"); show(A,n); printf("\nThe const. matrix(B):\n"); for(i=1;i<=n;i++) { printf("\n%g",B[i]); } for(i=1;i<=n;i++) { sum=0; for(j=1;j<=n;j++) { sum=sum+fabs(A[i][j]); } if(2*fabs(A[i][i])<=sum) { printf("\nNot a strictly diagonally dominant matrix"); return; } } for(i=1;i<=n;i++) { PREVX[i]=0; NEWX[i]=0; } do { for(i=1;i<=n;i++) { sum=0; for(j=1;j<=n;j++) { if(i!=j) { sum=sum+A[i][j]*PREVX[j]; } } NEWX[i]=(B[i]-sum)/A[i][i]; } error=0; for(i=1;i<=n;i++) { error=error+fabs(PREVX[i]-NEWX[i]); PREVX[i]=NEWX[i]; } }while(error>E); printf("\n"); for(i=1;i<=n;i++) { printf("\nX%d=%g",i,NEWX[i]); } } void main() { float A[20][20],B[20]; int i,j,n; clrscr(); printf("\nEnter number of variables:"); scanf("%d",&n); printf("\nEnter the co-efficient matrix:\n"); input(A,n); printf("\nEnter the const. values:\n"); for(i=1;i<=n;i++) { scanf("%f",&B[i]); } build(A,B,n); gauss_jacobi(A,B,n); getch(); } OUTPUT: Enter number of variables:3 Enter the co-efficient matrix: Enter term 1 of equation 1:1 Enter term 2 of equation 1:1 Enter term 3 of equation 1:4 Enter term 1 of equation 2:8 Enter term 2 of equation 2:-3 Enter term 3 of equation 2:2 Enter term 1 of equation 3:4 Enter term 2 of equation 3:11 Enter term 3 of equation 3:-1 Enter the const. values: 9 20 33 20 33 9 The co-efficient matrix(A): 8 -3 2 4 11 -1 1 1 4 The const. matrix(B): 20 33 9 X1=3 X2=2 X3=1

Share:

0 comments