Skip to content
site-logo
  • Home
  • JavaScript
  • Machine Learning
  • C++Expand
    • C++ Programing
    • Numerical Techniques C++
    • Data Structures C++
    • C++ Programs
  • Typing Guide
  • Blogs
site-logo
Home / Numerical Techniques C++ / The Gauss-Seidel Iterative Method C++ Example.

The Gauss-Seidel Iterative Method C++ Example.

ByZahid Updated onSeptember 22, 2021
Numerical Techniques C++
Ad
Table of contents
  1. Gauss seidel-iterative method c++
  2. Gauss Seidel-iterative method C++ Program
    1. C++ program Gauss-Seidel method.
  3. Example | Gauss Siedel Method
  4. Generalized form:
  5. Algorithm:
  6. Some Numerical Methods For MCA

Gauss seidel-iterative method c++

Iterative or approximate methods provide an alternative to the elimination methods described to this point.

Those approaches consisted of guessing a value and then using a systematic method to obtain a refined estimate of the root.

Ad

The Gauss-Seidel method is the most commonly used iterative method. read more

Must Read: Lu Decomposition Method C++

Gauss Seidel-iterative method C++ Program

//Gauss-seidel Iterative method
   //techindetail.com

#include<iostream>
#include<conio.h>
 
using namespace std;
 
int main(void)
{
    float a[10][10], b[10], x[10], y[10];
    int n = 0, m = 0, i = 0, j = 0;
    cout << "Enter size of 2d array(Square matrix) : ";
    cin >> n;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            cout << "Enter values no :(" << i << ", " << j << ") ";
            cin >> a[i][j];
        }
    }
    cout << "\n Enter Values to the right side of equation\n";
    for (i = 0; i < n; i++)
    {
        cout << "Enter values no :(" << i << ", " << j << ") ";

        cin >> b[i];
    }

    cout << "Enter initial values of x\n";
    for (i = 0; i < n; i++)
    {
        cout << "Enter values no. :(" << i<<"):";
        cin >> x[i];
    }

    cout << "\nEnter the no. of iteration : ";
    cin >> m;

    while (m > 0)
    {
        for (i = 0; i < n; i++)
        {
            y[i] = (b[i] / a[i][i]);
            for (j = 0; j < n; j++)
            {
                if (j == i)
                    continue;
                y[i] = y[i] - ((a[i][j] / a[i][i]) * x[j]);
                x[i] = y[i];
            }
            printf("x%d = %f    ", i + 1, y[i]);
        }
        cout << "\n";
        m--;
    }
    return 0;
}Code language: C++ (cpp)

C++ program Gauss-Seidel method.

Read More: Gauss Jordan Method C++ Example

Example | Gauss Siedel Method


We will first illustrate the Gauss-Seidel method on two simultaneous equations.

Later we will generalize it to n equations in n unknowns.


Consider the simultaneous equations given below:

x1 + x2= 2 (1)

3×1-10×2= 3 (2)

Start with an initial value of x2=0. Substitute this in Equation (1) and obtain the value of x1=2.

Use this guessed value in Equation (2) to obtain a refined guess for x2. This is given by x2 = (3×1-3)/10 = (6-3)/10= .3

Ad

Use this in Equation (1) to get another value of x1. The progress of the iterations is shown in Table (1)

Iteration no.X1X2
123
21.7.21
31.79.237
41.763.229
51.771.231
61.769.231
71.769.231

Observe that the values of x1 and x2 converge to 1.769 and .231 respectively which is the solution.

As this is an iterative method iterations are stopped when successively values of x1 as well as of x2 are “closer enough”.

This is a successive approximations method and the technique is illustrated graphically in figure (1).

fiqure (1) Illustrating the Gauss-seidel method.

Related: Regula Falsi Method Solved Example

Generalized form:

Now let’s generalize Gauss-seidel Iterative method
Assume that we are given a set of n equations:
[A]{X}={B}
Suppose that for conciseness we limit ourselves to a 3 × 3 set of equations.

If the diagonal elements are all nonzero, the first equation can be solved for x1, the second for x2, and the third for x3 to yield

x1 =(b1 − a12x2 − a13x3)⁄a11 2(a)
x2 =(b2 − a21x1 − a23x3)⁄a22 2(b)
x3 =(b3 − a31x1 − a32x2)⁄a33 3(b)

Now, we can start the solution process by choosing guesses for the x’s. A simple way to obtain initial guesses is to assume that they are all zero. These zeros can be substituted into Eq. (2a), which can be used to calculate a new value for

x1 = b1/a11

Then, we substitute this new value of x1 along with the previous guess of zero for x3 into Eq. (2b) to compute a new value for x2.

The process is repeated for Eq. (2c) to calculate a new estimate for x3. Then we return to the first equation and repeat the entire procedure until our solution converges closely enough to the true values. Convergence can be checked using the criterion

|εa,i| = |xij − xij-1|/|xij|× 100% < εs 2(c)

for all i, where j and j − 1 are the present and previous iterations.

Related: Bisection Method Solved Example

Algorithm:

  1. for i=1 to n in steps of 1 and j=1 to n+1 in steps of 1 do read aij end for.
  2. Read e, maxit
    Note: e is the allowed relative error in the result vector. maxit is maximum number of iterations allowed for the solution to converge.
  3. for i=1 to n in steps of 1 xi=0 end for
  4. for itr=1 to maxit do
  5. big=0
  6. for i=1 to n in steps of 1 do
  7. sum=0
  8. for j=1 to n in steps of 1 do
  9. for (j≠i) then sum =sum+aijxj end for
  10. temp=(ai(n+1)-sum)/aii
  11. relerror=|(xi-temp)/temp|
  12. if relerror > big then big=relerror
  13. xi=temp
    end for
  14. if (big<=e) then
    begin Write ‘converges to a solution’
    for i=1 to n in steps of 1 do Write xi endfor
    stop end

    end for
  15. Write ‘Does nott converge in maxit iteratioins’
  16. for i=1 to n in steps of 1 do Write xi end for
  17. Stop
    This is the Algorithm for Gauss-Seidel Iterative method C++

Some Numerical Methods For MCA

  • Gauss Elimination Method C++
  • Gauss Elimination Partial Pivoting
  • Gauss Jordon Method C++
  • Maclaurin Series Formula
  • Regula Falsi Method C++
  • Newton Raphson Method C++
  • Bisection Method C++
  • Linear Regression

Post navigation

Previous Previous
Gauss Elimination Method C++ Program Algorithm & Example
NextContinue
Gauss Jordan Method C++ Program Algorithm & Example
Search

  • Home
  • Privacy Policy
  • Disclaimer
  • Sitemap
  • Write for us
  • Contact Us

Copyright © 2025 TechInDetail

Scroll to top
  • Home
  • JavaScript
  • Machine Learning
  • C++
    • C++ Programing
    • Numerical Techniques C++
    • Data Structures C++
    • C++ Programs
  • Typing Guide
  • Blogs
Search