Date:
Assignment No. : Problem Statement Program in C to solve the given equation by Gauss Jacobi method. 8x1-3x2+2x3=20 4x1+11x2-x3=33 x1+x2+4x3=9
Algorithm Variable Description : Variables n i j sum temp a[10][10] x[3] y[3]
Datatype Integer Integer Integer Float Float Float Float Float
err
Float
Step Step Step Step Step Step
1: 2: 3: 4: 5: 6:
Page No.
Purpose Stores the number of equations Loop variable Loop variable Stores the sum Temporary storage Stores the input given by the Stores the result Calculates and temporarily stores the result Stores the error
Read the input Print the number of equation Print the values of a[i][j] for (i=0 to n) x[i]=0.0 do loop (i)for (i=0 to n )
(ii) sum=0.0 (iii)for (j=0 to n) (iv) if(j!=i) (v)sum=sum+a[i][j]*x[j] (vi)end for loop for j (vii) temp=(a[i][n+1]-sum)/a[i][i] (viii)err=fabs(x[i]-temp) (ix)y[i]=temp (x)Print x[i] (xi)end for loop for i (xii) x[i]=y[i] (xiii)end do loop Step 7: while(err>=0.01) Step 8: for (i=0 to n) Step 9: Print the result x[i] Step 10: Stop.
Program Code #include<stdio.h> #include
#include<math.h> void main() { int i,j,n; float sum,temp,err, a[10][10],x[3],y[3]; clrscr(); printf ("enter the number of equations:"); scanf ("%d",&n); Page No.
printf (" \n enter the values of a[i][j]:"); for(i=1;i<=n;i++) { for(j=1;j<=n+1;j++) { scanf("%f",&a[i][j]); } } for(i=1;i<=n;i++) x[i]=0.0; do { for(i=1;i<=n;i++) { sum=0.0; for(j=1;j<=n;j++) { if(j!=i) sum=sum+a[i][j]*x[j]; } temp=(a[i][n+1]-sum)/a[i][i]; err=fabs(x[i]-temp); y[i]=temp; printf("\n x[%d]=%f",i,x[i]); } for(i=1;i<=n;i++) x[i]=y[i]; } while(err>=0.01); for(i=1;i<=n;i++) printf("the result is %f\n",x[i]); getch(); }
Input/Output Page No.
enter the number of equations:3 enter the values of a[i][j]:8 -3 2 20 4 11 -1 33 1 1 4 9 x[1]=0.000000 x[2]=0.000000 x[3]=0.000000 x[1]=2.500000 x[2]=3.000000 x[3]=2.250000 x[1]=3.062500 x[2]=2.295455 x[3]=0.875000 x[1]=3.142045 x[2]=1.965909 x[3]=0.910511 x[1]=3.009588 x[2]=1.940212 x[3]=0.973011 x[1]=2.984327 x[2]=1.994060 x[3]=1.012550 the result is 2.994635 the result is 2.006840 the result is 1.005403
Discussion
Page No.
The above program can be used to solve a given equation by Gauss Jacobi method. For a system to converge it should be strictly diagonally dominant. The equation to be solved must be arranged in proper way otherwise it cannot be solved.
Page No.