programming-examples/c/Numerical/C Program to Implement Gauss Seidel Method.c
2019-11-15 12:59:38 +01:00

33 lines
778 B
C

#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;
}