C Program to Implement Gauss Seidel Method
Posted by Superadmin on December 07 2015 02:10:40
#include <stdio.h>
int main()
{
//a sparse way of representing the equations
float eq[3][4];
int i;
float x ,y , z;
x = 1; y = 1; z = 2; //initial guess
eq[0][0] = 7/4.0;
eq[0][1] = 0;
eq[0][2] = 1/4.0;
eq[0][3]= -1/4.0;
eq[1][0] = 21/8.0;
eq[1][1] = 4/8.0;
eq[1][2] = 0;
eq[1][3]= 1/8.0;
eq[2][0] = 15/5.0;
eq[2][1] = 2/5.0;
eq[2][2] = -1/5.0;
eq[2][3]= 0;
//10 iterations of gauss-seidel
for (i = 0;i < 10; i++) {
x = eq[0][0] + eq[0][2] * y + eq[0][3] * z;
y = eq[1][0] + eq[1][1] * x + eq[1][3] * z;
z = eq[2][0] + eq[2][1] * x + eq[2][2] * y;
printf("Output: \n%f %f %f\n", x, y, z);
}
return 0;
}
$ gcc gauss_seidel.c -o gauss_seidel
$ ./gauss_seidel
1.500000 3.625000 2.875000
1.937500 3.953125 2.984375
1.992188 3.994141 2.998047
1.999023 3.999268 2.999756
1.999878 3.999908 2.999969
1.999985 3.999989 2.999996
1.999998 3.999999 3.000000
2.000000 4.000000 3.000000
2.000000 4.000000 3.000000
2.000000 4.000000 3.000000