Jacobi Iterative Method

Good evening guys.

For the past few days I have been working on what is basically, a Jacobi Iterative Method. For some reason, my results keep coming back differently from the wanted solution. They are good results, but clearly not true.
Today luckily I found a book with the code on how to make it (http://books.google.pt/books?id=weYj75E_t6MC&pg=PA236&lpg=PA236&dq=jacobi+iterative+method+c%2B%2B&source=bl&ots=LQ04T5v9qz&sig=h-3Im5vRfVK3mijKh4-7iZu2lFA&hl=pt-PT&sa=X&ei=bnRUUdu2IMWR7AazgoHgDA&ved=0CGoQ6AEwBg#v=onepage&q=jacobi&f=false page 237)
and yet, as far as I can see, their code is exactly the same as mine (small differences in declared vars and names) and yet, using the same matrix and b vector as them I get different results.
Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for(k=0; k<nmax ; k++)
        {
            vector<double> prevx = x; //x = x(k+1); prevx=x(k)
            
            for(int i=0; i<n; i++)
            {
                double sum = .0;
                
                for(int j=0 ; j<n; j++)
                    if(i != j)
                        sum +=  matrix[i][j] * prevx[j];
                x[i] = (b[i] - sum)/ matrix[i][i];
            }
            double norm = 0.0;
            for(int i=0;i<x.size();i++)
                norm += (pow(x[i]-prevx[i],2));
            norm = sqrt(norm);            
            if(norm < epsilon)
            {
                success = true;
                break;  
            }
        }


Am I blind or they are basically the same?
If so, how come mine isn't working properly?

PS- I know this sounds stupid, but I would rather prefer hints instead of a solution. Figuring out the problem by myself (even if with some assistance) is a great way to learn.
Last edited on
Just a ^bump since its now on page...4?!
> my results keep coming back differently from the wanted solution.
> They are good results, but clearly not true.
Show an example.

Their stop condition is different
@ne555- Thanks for your time mate. You are indeed right, their condition is an infinitenorm but that isn't what really concerns me. You see, each x (solution) vector per iteration is different from theirs, even the mean and variance, mine are much much smaller.
It may be a precision issue, ¿what and how are you testing?
I'm testing if my xi vectors (possible solution vectors) are the same as theirs. I test it by outputting the vectors onto the console.

Tbh, my program does more, e.g. it has to read the matrix and "n" from a txt, parse it, etc. So the problem may be in there...but as far as you can see, the Jacobi iteration itself is fine?

PS- I also have to calc the ||Ax -b|| (after the x solution is found), meaning, this is linear algebra. Will this do the trick?
1
2
3
4
5
6
7
8
9
10
11
12
double getResidual(vector< vector<double> > m, vector<double> x, vector<double> b)
{
    double norm= .0;
    for(int i=0;i<m.size(); i++)
        {
        double matSol = 0.0; //matriz * vector solucao
        for(int j=0; j< m[0].size(); j++)
            matSol += m[i][j] * x[j];
        norm += pow(matSol - b[i],2);        
        }
    return sqrt(norm);
}
Topic archived. No new replies allowed.