Gauss Seidel Method In C

In comparison, employing iterative methods such as the Gauss-Jacobi and Gauss-Seidel iterations involves initiating with an initial approximation and repeatedly refining it until the desired level of accuracy is attained.

The step-by-step manual computation process can be time-consuming. In contrast, high-level programming languages enable fast and effective program execution. This C code for the Gauss-Seidel technique was developed for solving systems of linear equations through iterative methods.

The Jacobi technique and the Gauss-Seidel approach are commonly known as the iterative displacement method. It is essential that the upcoming equations incorporate the latest values. The Gauss-Seidel convergence criteria necessitate the fulfillment of the following two conditions:

  • The dominance of the matrix along the diagonal.
  • The matrix being both positive and symmetric.
  • Steps involved:

Step-1:

For the system of linear equations, select arbitrary initial values for the variables. These values will serve as the starting points for the iterative process.

Step-2:

Utilize the current values of the remaining variables, the given set of linear equations, and the fresh value of each variable to determine the updated value for every variable. This involves adjusting each variable individually.

Step-3:

Determine the absolute relative approximation error for each variable after Step 2 has completed updating all variables. The error reflects the extent of change in values across iterations.

The method converges towards a solution, allowing you to halt the iteration process once all variable inaccuracies decrease beyond a specified tolerance level (a small value determined by you).

Code for the Gauss-Seidel algorithm:

Example

#include <stdio.h>
#include <math.h> // Added math.h for fabs() function

int main()
{
    int count, t, limit;
    float temp, error, a, sum = 0;
    float matrix[10][10], y[10], allowed_error;

printf("\nEnter the Total Number of Equations:\t");
scanf("%d", &limit);

    // highest error threshold at which accuracy is attained or errors are taken into account
printf("Enter Allowed Error:\t");
scanf("%f", &allowed_error);

printf("\nEnter the Coefficients\n");
    for (count = 1; count <= limit; count++)
    {
        for (t = 1; t <= limit + 1; t++)
        {
printf("Matrix[%d][%d] = ", count, t);
scanf("%f", &matrix[count][t]);
        }
    }

    for (count = 1; count <= limit; count++)
    {
        y[count] = 0;
    }

    do
    {
        a = 0;

        for (count = 1; count <= limit; count++)
        {
            sum = 0;

            for (t = 1; t <= limit; t++) // Removed extra condition 't a'
            {
                if (t != count)
                {
                    sum += matrix[count][t] * y[t];
                }
            }

            temp = (matrix[count][limit + 1] - sum) / matrix[count][count];
            error = fabs(temp - y[count]);

            if (error > a)
            {
                a = error;
            }

            y[count] = temp;
printf("\nY[%d] = %f", count, y[count]);
        }

printf("\n");
    } while (a >= allowed_error);

printf("\n\nSolution:\n\n");

    for (count = 1; count <= limit; count++)
    {
printf("Y[%d]:\t%f\n", count, y[count]);
    }

    return 0;
}

Output:

Output

Enter the Total Number of Equations: 2
Enter Allowed Error: 0.3

Enter the Coefficients
Matrix[1][1] = 2
Matrix[1][2] = 6

Y[1] = 3.000000
Y[1] = 2.000000
Y[1] = 2.000000

Solution:
Y[1]: 2.000000

Advantages:

There are several advantages of the Gauss-Seidel method in C. Some advantages of the Gauss-Seidel method are as follows:

  • It has a quicker iteration cycle (than other techniques).
  • It is simple and straightforward to apply.
  • Low demands on memory.
  • Disadvantages:

There are numerous drawbacks associated with the Gauss-Seidel technique in C programming. Some of the disadvantages of the Gauss-Seidel method include:

  • It leads to a slower rate of convergence compared to other methods.
  • It requires a high number of iterations to reach the convergence point.

Input Required

This code uses input(). Please provide values below: