Home » Numerical Techniques Using C++ » The Gauss-Seidel Iterative Method C++ Example.

# The Gauss-Seidel Iterative Method C++ Example.

## 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.

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

## Gauss Seidel-iterative method C++ Program

```/* info: https://wikkihut.com/wp-content/plugins/syntax-highlighting-code-block/vendor/scrivo/highlight-php/styles/atom-one-dark-reasonable.css */
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```//Gauss-seidel Iterative method
//wikkihut.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;
}``````

## Example | Rule 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)

3x1-10x2= 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=(3x1-3)/10=(6-3)/10=.3 . Use this in Equation (1) to get another value of x1. The progress of the iterations is shown in Table (1)

TABLE 4.1   Illustrating Iterative Method
Iteration Number x1 x2
1
2
.3
2
1.7
.21
3
1.79
.237
4
1.763
.229
5
1.771
.231
6
1.769
.231
7
1.769
.231

initial guess x2=0

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 successive approximations method and the technique is illustrated graphically in figure (1).

## 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.

## 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.
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++