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

*Gauss Seidel-iterative method C++ Program*

```
//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;
}
```

**C++ program Gauss-Seidel method**.

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

_{1}+ x

_{2}= 2 (1)

_{1}-10x

_{2}= 3 (2)

Start with an initial value of x_{2}=0. Substitute this in Equation (1) and obtain the value of x_{1}=2. Use this guessed value in Equation (2) to obtain a refined guess for x_{2}. This is given by x_{2}=(3x_{1}-3)/10=(6-3)/10=.3 . Use this in Equation (1) to get another value of x_{1}. The progress of the iterations is shown in Table (1)

**TABLE 4.1**Illustrating Iterative Method

Iteration Number | x_{1} |
x_{2} |
---|---|---|

initial guess x

_{2}=0

Observe that the values of x_{1} and x_{2} converge to 1.769 and .231 respectively which is the solution. As this is an iterative method iterations are stopped when successively values of x_{1} as well as of x_{2} 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

_{1}=

^{(b1 − a12x2 − a13x3)}⁄

_{a11}. 2(a)

x

_{2}=

^{(b2 − a21x1 − a23x3)}⁄

_{a22}. 2(b)

x

_{3}=

^{(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

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:

*for i=1 to n in steps of 1 and j=1 to n+1 in steps of 1 do read aij end for.**Read e, maxi*t

Note: e is the allowed relative error in the result vector. maxit is maximum number of iterations allowed for the solution to converge.*for i=1 to n in steps of 1 xi=0 end for*- for itr=1 to maxit do
- big=0
*for i=1 to n in steps of 1 do**sum=0**for j=1 to n in steps of 1 do**for (j≠i) then sum =sum+a*_{ij}x_{j}end for*temp=(a*_{i(n+1)}-sum)/a_{ii}*relerror=|(x*_{i}-temp)/temp|*if relerror > big then big=relerror**x*_{i}=temp

end for*if**(big<=e) then**begin*Write ‘converges to a solution’*for i=1 to n in steps of 1 do Write x*_{i}endfor

stop end*end for*- Write ‘Does nott converge in maxit iteratioins’
- for i=1 to n in steps of 1 do Write xi end for
- Stop

This is the Algorithm for**Gauss-Seidel Iterative method C++**