The direct methods such as Cramer’s rule, matrix inversion method, Gauss Elimination method, etc. for the solution of simultaneous algebraic equations yield the solution after a certain amount of fixed computation. So, direct method of solution takes longer time to get the solution.

On the other hand, in case of iterative methods such as Gauss Jacobi and Gauss-Seidel iteration method, we start with an approximate solution of equation and iterate it till we don’t get the result of desired accuracy. Here is source code for Gauss-Seidel in C with working procedure and sample output.

The manual computation iterative method is quite lengthy. But, the program in high level languages run fast and effectively. This **C program for Gauss-Seidel method** has been designed for the solution of linear simultaneous algebraic equations based on the principle of iteration.

In this program, a certain approximate value of solution is assumed and further calculations are done based on the result of assumed approximate solution.

The program for** Gauss-Seidel method in C** works by following the steps listed below:

- When the program is executed, first of all it asks for the value of elements of the augmented matrix row wise.
- Then, the program asks for allowed error and maximum number of iteration to which the calculations are to be done. The number of iterations required depends upon the degree of accuracy.
- The program assumes initial or approximate solution as y=0 and z=0 and new value of x which is used to calculate new values of y and z using the following expressions:

x= 1/a_{1} ( d_{1}-b_{1}y-c_{1}z)

y=1/b_{2} ( d_{2}-a_{2}x-c_{2}z)

z=1/c_{3} ( d_{3}-a_{3}x-b_{3}y)

- The iteration process is continued until a desired degree of accuracy is not met.

## Source Code for Gauss-Seidel Method in C:

```
#include<stdio.h>
#include<math.h>
#define X 2
main()
{
float x[X][X+1],a[X], ae, max,t,s,e;
int i,j,r,mxit;
for(i=0;i<X;i++) a[i]=0;
puts(" Eneter the elemrnts of augmented matrix rowwise\n");
for(i=0;i<X;i++)
{
for(j=0;j<X+1;j++)
{
scanf("%f",&x[i][j]);
}
}
printf(" Eneter the allowed error and maximum number of iteration: ");
scanf("%f%d",&ae,&mxit);
printf("Iteration\tx[1]\tx[2]\n");
for(r=1;r<=mxit;r++)
{
max=0;
for(i=0;i<X;i++)
{
s=0;
for(j=0;j<X;j++)
if(j!=i) s+=x[i][j]*a[j];
t=(x[i][X]-s)/x[i][i];
e=fabs(a[i]-t);
a[i]=t;
}
printf(" %5d\t",r);
for(i=0;i<X;i++)
printf(" %9.4f\t",a[i]);
printf("\n");
if(max<ae)
{
printf(" Converses in %3d iteration\n", r);
for(i=0;i<X;i++)
printf("a[%3d]=%7.4f\n", i+1,a[i]);
return 0;
}
}
}
```

### Input/Output:

Also see,

Gauss Seidel Matlab Program

Gauss Seidel Algorithm/Flowchart

Numerical Methods Tutorial Compilation

In this C language code for Gauss-Seidel method, the value of order of square matrix has been defined as a macro of value 2 which can be changed to any order in the source code.

Also, the elements of augmented matrix have been defined as array so that a number of values can be stored under a single variable name. The program can be used effectively to solve linear simultaneous algebraic equation though easy, accurate and convenient way.

Hi can you c code on Jacobi iteration method by solving Helmholtz equation

Hey, can if it’s not too much trouble can you look at my program & why it’s displaying results after only one iteration. I wrote it based on yours but i don’t have to read max no. of iterations or max. error allowed. I just have to read the no. of unknowns.

#include

#include

#include

main ()

{

float a[20][20],x[10],max,t,s,e;

int i,j,n,r;

printf(“Enter the number of unknowns: “);

scanf(“%d”, &n);

for(i=1;i<=n;i++)

{

x[i]=0;

}

printf(" Enter the elemrnts of augmented matrix rowwise\n");

for(i=1;i<=n;i++)

{

for(j=1;j<=n+1;j++)

{

scanf("%f", &a[i][j]);

}

}

for(r=1;r<=20;r++)

{

max=0;

for(i=1;i<=n;i++)

{

s=0;

for(j=1;j<=n;j++)

if(j!=i)s=s+a[i][j]*x[j];

t=(a[i][n+1]-s)/a[i][i];

e=fabs(x[i]-t);

x[i]=t;

}

if(max<0.00005)

{

printf("Answer obtained is:");

for(i=1;i<=n;i++)

printf("x[%d]=%f\n", i,x[i]);

getch();

return 0;

}

}

}

ok, may be this is just me being dumb or my faulty laptop but when i write your program it does not show errors but on execution it exits and i cannot see the result. plz check.

Hey Atul, I tested this program before posting it here, and it still executes on my computer. The source code is written and compiled in Code::Blocks.

I don’t think the program would simply exit upon execution. Try writing a simple program to print “Hello World” to check if there’s any problem with your compiler.