Gauss Seidel iterative method HELP

Hello everyone ,
I have a problem with the algorithm of Gauss Seidel iterative method .
Here's my code . It prints results but they aren't true . Here's what it supposed to print:

1 iteration:
x1= 0,5
x2 = 1,625
x3 = 0,90625

2 iteration:
x1= 0,90625
x2 = 1,953125
x3= 0,988281

3 iteration:
x1= 0,988281
x2= 1,994141
x3= 0,998535

4 iteration :
x1= 0,998535
x2 = 1,999268
x3= 0,999817

5 iteration :
x1= 0,999817
x2 = 1,999908
x3 = 0,999977

6 iteration:
x1= 0,999977
x2 = 1,999989
x3 = 0,999997

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cmath>
using namespace std;
const double eps=0.000006;
const int n=3;
const int MAX=3;

bool converge(double *xk, double *xkp)
{
    double norm = 0;
    for (int i = 0; i < n; i++)
    {
      norm += (xk[i] - xkp[i])*(xk[i] - xkp[i]);
    }
    if(sqrt(norm) >= eps)
      return false;
    return true;
}

void Gaus(double a[][MAX],double x[],double b[])
{
    double p[n];

//do
//{
    for(int k=0;k<6;k++)
    {
    for (int i = 0; i < n; i++)
        p[i] = x[i];

    for (int i = 0; i <n; i++)
    {
        double var = 0;
        for (int j = 0; j < i; j++)
            var += (a[i][j] * x[j]);
        for (int j = i; j < n; j++)
            var += (a[i][j] * p[j]);
        x[i] = (b[i] - var) / a[i][i];
        cout<<"x["<<i<<"]= "<<x[i]<<'\t';
    }
    cout<<endl;
    }
//}
//while (!converge(x, p));
}
int main()
{
    double a[n][n]={{4,-1,0},{-1,4,-1},{0,-1,4}};
    double x[n]={0,0,0};
    double b[n]={2,6,2};
    Gaus(a,x,b);
    return 0;
}
(You haven't told us what yours prints that makes it incorrect).

I would think you would have more luck going line through line with a debugger and seeing where the values change from what you expect.
the program incorrectly prints :
1 iteration:
x1= 0,5
x2 = 1,625
x3 = 0,90625

2 iteration:
x1= 0,40625
x2 = 0,203125
x3= -0,355469

3 iteration:
x1= 0,144531
x2= 1,24414
x3= 1,1665
Topic archived. No new replies allowed.