You are on page 1of 3

PROBLEM 6

GAUSS JACOBI METHOD


C PROGRAM

#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Defining function */
#define f1(x,y,z) (4-y+z)
#define f2(x,y,z) (7-2*x-z)/3
#define f3(x,y,z) (-6-x+2*y)/3
/* Main function */
int main()
{
float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, e3, e;
int count=1;
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("\nCount\tx\ty\tz\n");
do
{
/* Calculation */
x1 = f1(x0,y0,z0);
y1 = f2(x0,y0,z0);
z1 = f3(x0,y0,z0);
printf("%d\t%0.4f\t%0.4f\t%0.4f\n",count, x1,y1,z1);
/* Error */
e1 = fabs(x0-x1);
e2 = fabs(y0-y1);
e3 = fabs(z0-z1);
count++;
/* Set value for next iteration */
x0 = x1;
y0 = y1;
z0 = z1;
}
while(e1>e && e2>e && e3>e);
printf("\nSolution: x=%0.3f, y=%0.3f and z = %0.3f\n",x1,y1,z1);
getch();
return 0;
}

Output:

Enter tolerable error:


0.001

Count x y z
1 4.0000 2.3333 -2.0000
2 -0.3333 0.3333 -1.7778
3 1.8889 3.1481 -1.6667
4 -0.8148 1.6296 -0.5309
5 1.8395 3.0535 -0.6420
6 0.3045 1.3210 -0.5775
7 2.1015 2.3228 -1.2209
8 0.4563 1.3393 -1.1520
9 1.5088 2.4131 -1.2593
10 0.3276 1.7472 -0.8942
11 1.3586 2.4130 -0.9444
12 0.6426 1.7424 -0.8442
13 1.4134 2.1863 -1.0526
14 0.7611 1.7420 -1.0136
15 1.2445 2.1638 -1.0924
16 0.7438 1.8678 -0.9723
17 1.1599 2.1616 -1.0027
18 0.8357 1.8943 -0.9456
19 1.1601 2.0914 -1.0157
20 0.8929 1.8985 -0.9924
21 1.1091 2.0689 -1.0320
22 0.8992 1.9380 -0.9904
23 1.0716 2.0640 -1.0077
24 0.9282 1.9548 -0.9812
25 1.0640 2.0416 -1.0062
26 0.9522 1.9594 -0.9936
27 1.0470 2.0297 -1.0111
28 0.9591 1.9724 -0.9959
29 1.0318 2.0259 -1.0048
30 0.9694 1.9804 -0.9933
31 1.0262 2.0182 -1.0028
32 0.9790 1.9835 -0.9966
33 1.0199 2.0129 -1.0040
34 0.9831 1.9880 -0.9980
35 1.0139 2.0106 -1.0023
36 0.9870 1.9915 -0.9976
37 1.0109 2.0078 -1.0013
38 0.9908 1.9932 -0.9984
39 1.0084 2.0056 -1.0015
40 0.9929 1.9949 -0.9991
41 1.0060 2.0044 -1.0010
42 0.9945 1.9963 -0.9991
43 1.0046 2.0033 -1.0006
44 0.9960 1.9971 -0.9993
45 1.0035 2.0024 -1.0006
46 0.9970 1.9978 -0.9996
47 1.0026 2.0019 -1.0004

Solution: x=1.003, y=2.002 and z = -1.000

COMPARITION:
ANALYTICAL C PROGRAMMING DIFFERENCE
SOLUTION SOLUTION

X 1.005 1.003 0.002

Y 2.002 2.002 0

Z -1.000 -1.000 0

You might also like