Gauss Jacobi Method
Gauss Jacobi Method
Answer:
1 /*
2 Solve by Gauss Jacobi Method.
3 Name- Ajit Kumar Podder
4 Roll No- 2212413
5 Registration Number- 0532105030370
6 */
7
8 #include<stdio.h>
9 #include<math.h>
10 #include <conio.h>
11 int main()
12 {
13 int i,j,k,n,max_itr;
14 float a[20][20],x[10],y[10],s=0.0,t=0.0;
15 printf("\nEnter the number of equations given:\n ");
16 scanf("%d",&n);
17 printf("\nEnter the elements of augmented matrix row-wise:\n\n");
18 for(i=1; i<=n; i++)
19 {
20 for(j=1; j<=(n+1); j++)
21 {
22 scanf("%f",&a[i][j]);
23 }
24 }
25 printf("\nEnter the initial approximations for the unknowns respectively:\n\n");
26 for(i=1;i<=n;i++)
27 {
28 scanf("%f",&x[i]);
29 }
30 printf("\nEnter the maximum number of iterations to perform:\n ");
31 scanf("%d",&max_itr);
32 printf("\nThe computation table is:\n");
33 for(k=1;k<=max_itr;k++)
34 {
35 printf("\nIteration %d: ",k);
36 for(i=1;i<=n;i++)
37 {
38 s=0;
39 for(j=1;j<=n;j++)
40 {
41 if(j!=i)
42 s=s+a[i][j]*x[j];
43 }
44 t=(a[i][n+1]-s)/a[i][i];
45 y[i]=t;
46 printf("x[%d]=%f ",i,y[i]);
47 }
48 for(i=1;i<=n;i++)
49 {
50 x[i]=y[i];
51 }
52 }
53 printf("\n\nThe required solution is:\n");
54 for(i=1;i<=n;i++)
55 printf("x[%d]=%f\n", i,x[i]);
56 getch();
57 return(0);
58 }
Output:
10 8 -3 1 16
2 10 1 -4 9
3 -4 10 1 10
2 2 -3 10 11
0
0
0
0
__________________________________________________________________________