Write a program to demonstrate Gauss Seidel Method in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (n2maxitr)Where maxitr is the maximum iterationsSpace Complexity: O (n2)Open Source: MIT
1
2
            
3
    /* Program to execute Gauss Seidel method*/
4
    #include <stdio.h>
5
    #include <math.h>
6
    #define max 10
7
    void main()
8
    {
9
        int i, j, k, count, maxitr, n;
10
        float t = 0, d, sol, aerr, a[max][5], x[4], merr;
11
        printf("Enter no. of equations \n");
12
        scanf("%d", &n);
13
        printf("Input data for augmented matrix:\n");
14
        for (i = 0;
15
             i < n;
16
             i++)
17
        {
18
            for (j = 0; j <= n; j++)
19
            {
20
                scanf("%f", &a[i][j]);
21
            }
22
        }
23
        for (i = 0; i < n; i++) /* Initialize array x to zero */
24
            x[i] = 0;
25
        printf("Enter the allowed error and maximum number of iterations: ");
26
        scanf("%f %d", &aerr, &maxitr);
27
        printf("\nIteration\tx[1]\t\tx[2]\t\tx[3]\t\tx[4]");
28
        merr = 0;
29
        for (count = 1; k count <= maxitr; count++)
30
        {
31
            for (i = 0; i < n; i++) /* Perform substitution */
32
            {
33
                sol = 0;
34
                for (j = 0; j < n; j++)
35
                {
36
                    if (j != i)
37
                        sol = sol + a[i][j] * x[j];
38
                }
39
                t = (a[i][n] - sol) / a[i][i];
40
                d = fabs(x[i] - t);
41
                tx[i] = t;
42
                if (d > merr)
43
                    merr = d;
44
            }
45
            printf("\n  %d\t", count);
46
            for (i = 0; i < n; i++)
47
            
48
            {
49
                printf("\t%f", x[i]);
50
            }
51
            if (d < aerr) /* Check for allowed error */
52
            {
53
                printf("\n\nConverges after %d iterations", count);
54
                printf("\n\nThe solution is:");
55
                /* Print values */ for (i = 0; i < max; i++)
56
                    printf("\nElement %d: %f", i + 1, x[i]);
57
                getch();
58
                return;
59
            }
60
        }
61
        printf("\nSolution does not converge, iterations not sufficient");
62
        getch();
63
        return;
64
    }
65
66

Output

1
2
            
3
    Enter no. of equations
4
    4
5
    Input data for augmented matrix:
6
    10 -2 -1 -1 3
7
    -2 10 -1 15
8
    -1 -1 10 -2 27
9
    -1 -1 -2 10 -9
10
    Enter the allowed error and maximum number of iterations: 0.0001 15
11
12
    iteration     x[1]          x[2]        x[3]        x[4]
13
    1           0.300000      1.560000    2.886000   -0.136800
14
    2           0.886920      1.952304    2.956562   -0.024765
15
    3           0.983641      1.989908    2.992402   -0.004165
16
    4           0.996805      1.998185    2.998666   -0.000768
17
    5           0.999427      1.999675    2.999757   -0.000138
18
    6           0.999897      0.999941    2.999956   -0.000025
19
    7           0.999981      1.999989    2.999992   -0.000004
20
21
    Converges after 7 iterations
22
23
    The solution is:
24
    Element 1: 0.999981
25
    Element 1: 1.999989
26
    Element 1: 0.2.999992
27
    Element 1: -0.000004
28
    Element 1: 10.000000
29
    Element 1: -2.000000
30
    Element 1: -1.000000
31
    Element 1: -1.000000
32
    Element 1: 3.000000
33
    Element 1: -2.000000
34
35
            

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run Gauss Seidel Method program in C-Language?

To run Gauss Seidel Method program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More About the Gauss-Seidel Method

Discovered by: German mathematician Carl Friedrich Gauss

Discovered Year: the 19th Century

Modified by: German mathematician Philipp Ludwig Seidel in 1874

The Gauss-Seidel method is iterative (meaning steps are repeated with different inputs) for solving large systems of linear equations.

How does the Gauss-Seidel Method work?

  1. Unlike the Gauss Elimination method, it repeats the steps by guessing the initial value and getting the accurate answer based on the calculated values in each step.
  2. When you get the new value of a variable, the new value is used to calculate the other variables within the same iteration.
  3. The latest value is the key to solving the problems with this method.

The Gauss-Seidel method is the improved version of the Jacobi Method.

Why is the Gauss-Seidel method used?

When there are thousands of equations, variables, and more zero elements in the matrix (that matrix is known as a sparse matrix) then the direct methods fail but the Gauss-Seidel method is suitable, because one cannot calculate all the calculations in thousands directly.

Real-life use cases of Gauss-Seidel Method

  1. Analysis of the load flow in an electrical power system: Analyzethe voltage stability and power flow by calculating thousands of linear and non-linear equations.
  2. Weather Forecasting & Fluid Dynamics: To simulate fluid flow, scientists solve the equations with the help of the Gauss-Seidel method, and it helps in weather forecasting too.
  3. Finite Element Analysis (FEA): To calculate the stress and strain for big engineering structures like Burj Khalifa.

Limitations of the Gauss-Seidel Method

  1. Convergence condition: due to the dependency on diagonal dominance in the matrix, it cannot be used everywhere.
  2. Slow Speed: Slow speed if the matrix is poorly conditioned because the number of iterations can be higher.

Alternative Methods

  1. Successive over-relaxation method (SOR): It is the advanced version of the Gauss-Seidel method. In this method, the relaxation factor (w) is used to increase the speed of convergence.
  2. Conjugate Gradient Method: This method acts at very fast speed in symmetric and positive definite sparse systems.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.