'Cannot get jacobi converted from gauss seidel in C++

I have coded up a gauss seidel method that works just fine but i cannot seem to figure out how to convert that to jacobi... I know it should be easy so i must be missing something simple. For the assignment i had to make my own vector and matrix classes so that is why Vector is capital and called differently. Here is my working gauss seidel code:

        else if (mode == 3) {
                Vector temp;
                temp.allocateData(b.numElems());
                Vector old = temp;
                Vector sum;
                double f = 50;
                int y = 4;
                double tol = 1e-12;
                double error = 10;
                int max = 999999;
                int count = 0;
                while ( error > tol && max > count) {
                        for (int i = 0; i < row_; i++) {
                                temp.setVal(i, b.getVal(i) / M[i][i]);
                                for (int j = 0; j < col_; j++) {
                                        if (j == i) {
                                                continue;
                                        }
                                        temp.setVal(i, temp.getVal(i) - ((M[i][j] / M[i][i]) * temp.getVal(j)));
                                        old.setVal(j, temp.getVal(i));
                                }
                        cout<<"x"<< i + 1 << "="<< temp.getVal(i) <<" \n";
                        error = abs(temp.getVal(i)-old.getVal(i))/abs(temp.getVal(i));
                        old = temp;
                        }
                cout << "\n";
                count++;
                }

        }

and here is my attempt at jacobi:

        else if (mode == 2) {
                Vector temp;
                temp.allocateData(b.numElems());
                Vector old = temp;
                Vector sum;
                double f = 50;
                int y = 4;
                double tol = 1e-12;
                double error = 10;
                int max = 999999;
                int count = 0;
                while ( error > tol && max > count) {
                        old.allocateData(b.numElems());
                        for (int i = 0; i < row_; i++) {
                                old.setVal(i, temp.getVal(i));
                                temp.setVal(i, b.getVal(i) / M[i][i]);
                                for (int j = 0; j < col_; j++) {
                                        if (j == i) {
                                                continue;
                                        }
                                        temp.setVal(i, temp.getVal(i) - ((M[i][j] / M[i][i]) * old.getVal(j)));

                                }
                        cout<<"x"<< i + 1 << "="<< temp.getVal(i) <<" \n";
                        error = abs(temp.getVal(i)-old.getVal(i))/abs(temp.getVal(i));
                        }
                cout << "\n";
                count++;
                }

        }

thanks everyone ahead of time for the help!!!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source