Open In App

Program for Gauss Seidel Method (Computational Mathematics)

Last Updated : 01 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Gauss Seidel method is an iterative process to solve a square system of (multiple) linear equations. It is also prominently known as ‘Liebmann’ method. In any iterative method in numerical analysis, every solution attempt is started with an approximate solution of an equation and iteration is performed until the desired accuracy is obtained. In Gauss-Seidel method, the most recent values are used in successive iterations. The Gauss-Seidel Method allows the user to control round-off error.
The Gauss Seidel method is very similar to Jacobi method and is called as the method of successive displacement. (Since recently obtained values are used in the subsequent equations). The Gauss Seidel convergence criteria depend upon the following two properties: (must be satisfied). 
 

  • The matrix is diagonally dominant.
  • The matrix is symmetrical and positive.

Steps involved: 
 

  • Step 1: Compute value for all the linear equations for Xi. (Initial array must be available)
  • Step 2: Compute each Xi and repeat the above steps.
  • Step 3: Make use of the absolute relative approximate error after every step to check if the error occurs within a pre-specified tolerance.

Code for Gauss Seidel method: 
 

C




#include <stdio.h>
 
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);
    // maximum error limit till which errors are considered,
    // or desired accuracy is obtained)
     
    printf("Enter Allowed Error:\t");
    scanf("%f", & allowed_error);
    printf("\nEnter the Co-Efficients\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 a)
            {
                a = error;
            }
            y[count] = temp;
            printf("\nY[%d]=\t%f", count, y[count]);
        }
        printf("\n");
    }
    while(a > = allowed_error);
     
    printf("\n\nSolution\n\n");
     
    for(count = 1; count < = limit; count++)
    {
        printf(" \nY[%d]:\t%f" , count, y[count]);
    }
    return 0;
}


Python3




import sys
 
limit = int(input("Enter the Total Number of Equations:\t"))
# maximum error limit till which errors are considered,
# or desired accuracy is obtained)
allowed_error = float(input("Enter Allowed Error:\t"))
print("\nEnter the Co-Efficients\n")
 
matrix = [[0 for j in range(limit+1)] for i in range(limit)]
y = [0 for i in range(limit)]
 
# Read in matrix of coefficients and constants
for count in range(limit):
    for t in range(limit+1):
        print(f" Matrix[{count+1}][{t+1}] = ", end='')
        matrix[count][t] = float(input())
 
# Initialize the solution vector
for count in range(limit):
    y[count] = 0
 
# Perform Gauss-Jordan elimination
while True:
    error = 0
    for count in range(limit):
        temp = matrix[count][limit]
        for t in range(limit):
            if t != count:
                temp -= matrix[count][t] * y[t]
        temp /= matrix[count][count]
        if abs(temp - y[count]) > error:
            error = abs(temp - y[count])
        y[count] = temp
        print(f"\nY[{count+1}]:\t{y[count]}")
    if error < allowed_error:
        break
 
# Print the solution vector
print("\n\nSolution\n\n")
for count in range(limit):
    print(f" \nY[{count+1}]:\t{y[count]}")


Output: 
 

Enter the Total Number of Equations:   1
Enter Allowed Error: 0.5

Enter the Co-Efficients
Matrix[1][1] = 1
Matrix[1][2] = 4

Y[1]= 4.000000

Y[1]= 4.000000

Solution

Y[1]: 4.000000

Advantages: 
 

  • Faster iteration process. (than other methods)
  • Simple and easy to implement.
  • Low on memory requirements.

Disadvantages: 
 

  • Slower rate of convergence. (than other methods)
  • Requires a large number of iterations to reach the convergence point.

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads