Jacobi Iterative Method help!!!

Hey all I have a project this semester where we have to write up our own Jacobi method. Below is what I have so far:

This is my actual method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Solvers::jacobiMeth(Matrix &myMatrix){
    double x = 0.0;
    double z = 1.0;
    setTimesRan(0);
    cout << "I have entered the Jacobi Method" << endl;
    while ( z > .0001)
    {
        for(int i = 0; i < myMatrix.getRow(); i++){
            for(int j = 0; j < myMatrix.getCol(); j++){
                if(i != j)
                {
                    x += (myMatrix.getGrid()[i][j] * myMatrix.getJsol()[j]);
                }
            }
            double solATi = (myMatrix.getB()[i] - x) / myMatrix.getGrid()[i][i];
            x=0;
            myMatrix.setJsol(i, solATi);
        }
        timesRan++;
        z = getJerror(myMatrix);
    }
}


This is what my Matrix class looks like:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "Matrix.h"
#include <iostream>
#include <ctime> // Needed for the true randomization
#include <cstdlib>
#include <cmath>
#include <fstream>

using namespace std;

Matrix::Matrix(){
    vector<vector<double> > grid;
    row = 0;
    col = 0;
    vector <double> ranSolution;
    vector <double> jb_solution;
    vector <double> solution;
    vector <double> b_solution;
    vector <double> J_solution;
    vector <double> GS_solution;
    vector <double> SOR_solution;
}

Matrix::Matrix(int row, int col){
    row = row;
    col = col;
    for(int i = 0; i < row; i++)
    {
        vector <double> cols;
        solution.push_back(0);
        jb_solution.push_back(0);
        J_solution.push_back(0);
        GS_solution.push_back(0);
        SOR_solution.push_back(0);
        for(int j = 0; j < col; j++)
        {
            int x = rand()%20000 + 1;
            double y = x / 100.00;
            y = y - 100.00;
            cols.push_back(y);
        }
        grid.push_back(cols);

    }
    generateSol();
    generateJB();
}

Matrix::Matrix(const Matrix& m){
    row = m.row;
    col = m.col;
    b_solution = m.b_solution;
    //cout << "testing testing testing3 " << endl;
    //ranSolution = rhs.getRanSolution();
    //cout << "testing testing testing4 " << endl;
    solution = m.solution;
    //cout << "testing testing testing5 " << endl;
    grid = m.grid;
    //cout << "testing testing testing6 " << endl;
    J_solution = m.J_solution;
    GS_solution = m.GS_solution;
    SOR_solution = m.SOR_solution;

}

Matrix::Matrix(ifstream& readFile, string& fileName){
    readFile.open(fileName.c_str());
    readFile >> row;
    readFile >> col;
    for(int i = 0; i < row; i++)
    {
        vector <double> cols;
        jb_solution.push_back(0);
        solution.push_back(0);
        J_solution.push_back(0);
        GS_solution.push_back(0);
        SOR_solution.push_back(0);
        for(int j = 0; j < col; j++)
        {
            double y = 0;
            readFile >> y;
            cols.push_back(y);
        }
        grid.push_back(cols);

    }
    for(int k = 0; k < row; k++)
    {
        double x = 0;
        readFile >> x;
        b_solution.push_back(x);
    }
    generateSol();
    generateJB();

}


int Matrix::getRow() const{
    return grid.size();
}

int Matrix::getCol() const{
    return grid[0].size();
}

vector <double> Matrix::getB() const{
    return b_solution;
}

vector <double> Matrix::getJB() const{
    return jb_solution;
}

vector <double> Matrix::getSolution() const{
    return solution;
}


vector <double> Matrix::getJsol() const{
    return J_solution;
}

void Matrix::setJsol(int i, double x)
{
    J_solution[i] = x;
}
vector <double> Matrix::getGSsol() const{
    return GS_solution;
}

void Matrix::setGSsol(int i, double x)
{
    GS_solution[i] = x;
}
vector <double> Matrix::getSORsol() const{
    return SOR_solution;
}

void Matrix::setSORsol(int i, double x)
{
    SOR_solution[i] = x;
}

vector <vector <double> > Matrix::getGrid() const{
    return grid;
}

void Matrix::generateSol(){
  cout << "What should you solution be?" << endl;
  for (int i = 0; i < getRow(); i++)
  {
      cin >> solution[i];
  }
}

void Matrix::generateB(){
  for (int i = 0; i < getRow(); i++)
  {
    for (int j = 0; j < getCol(); j++)
    {
        b_solution[i] = ((grid[i][j] * solution[j]) + b_solution[i]);
    }
  }
}

void Matrix::generateJB(){
  for (int i = 0; i < getRow(); i++)
  {
    for (int j = 0; j < getCol(); j++)
    {
        jb_solution[i] += (grid[i][j] * J_solution[j]);
    }
  }
}

void Matrix::printMatrixInfo(){
    for (int i = 0; i < grid.size(); i++)
    {
        for (int j = 0; j < grid[i].size(); j++)
        {
            cout << grid[i][j] << " ";
        }
        cout << endl;

    }

    cout << "B is followed by Solution" << endl;
    for (int i = 0; i < b_solution.size(); i++)
    {
        cout << b_solution[i] << "   ";
        cout << solution[i];
        cout << endl;

    }

    cout << "J solution is followed by J_B" << endl;
    for (int i = 0; i < J_solution.size(); i++)
    {
        cout << J_solution[i] << ", " << jb_solution[i];
        cout << endl;

    }

    cout << "GS solution" << endl;
    for (int i = 0; i < J_solution.size(); i++)
    {
        cout << GS_solution[i];
        cout << endl;

    }


}

void Matrix::diagMatrix(){
    for (int i = 0; i < getRow(); i++)
    {
         for (int j = 0; j < getCol(); j++)
        {
            if (j == i)
            {
                for (int l = 0; l < getCol(); l++)
                {
                    if (l != i)
                        grid[i][j] = fabs(grid[i][j]) + fabs(grid[i][l]);
                }
            }
        }

    }
}

void Matrix::writeFile(){
  ofstream myFile;
  myFile.open("MatrixFile.txt");
  if (myFile.is_open())
  {
    myFile << getRow() << " " << getCol() << endl;
    for (int i = 0; i < getRow(); i++)
     {
        for (int j = 0; j < getCol(); j++)
        {
            myFile << grid[i][j] << " ";
        }
     }
    myFile << endl;
    for (int i = 0; i < getRow(); i++)
    {
        myFile << b_solution[i] << " ";
    }
  }
}

Matrix Matrix::operator=(const Matrix& rhs){

        row = rhs.getRow();
        //cout << "testing testing testing1 " << row << endl;
        col = rhs.getCol();
        //cout << "testing testing testing2 " << col << endl;
        b_solution = rhs.getB();
        //cout << "testing testing testing3 " << endl;
        //ranSolution = rhs.getRanSolution();
        //cout << "testing testing testing4 " << endl;
        solution = rhs.getSolution();
        //cout << "testing testing testing5 " << endl;
        grid = rhs.grid;
        //cout << "testing testing testing6 " << endl;

}


the problem that I am having is that it won't work on any other matrix other than the basic one provided by my professor. I was hoping to get a second set of eyes on this thing thanks in advance


Michael Wright
Last edited on
Topic archived. No new replies allowed.