# C Program for Gauss-Seidel Method

4
15355

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/a1 ( d1-b1y-c1z)
y=1/b2 ( d2-a2x-c2z)
z=1/c3 ( d3-a3x-b3y)

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

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

### Input/Output:

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.

1. vankam

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

2. Atul

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,x,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)
{
for(i=1;i<=n;i++)
printf("x[%d]=%f\n", i,x[i]);
getch();
return 0;
}
}
}

3. Atul

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.