Jacobi Method

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
#include<iostream>
#include<conio.h>
using namespace std;
int main(void) {
   float a[10][10], b[10], m[10], n[10];
   int p = 0, q = 0, i = 0, j = 0;
   cout << "Enter size of 2D array : ";
   cin >> p;
   for (i = 0; i < p; i++) {
      for (j = 0; j < p; j++) {
         cout << "a[" << i << ", " << j << " ]=";
         cin >> a[i][j];
      }
   }
   cout << "\nEnter values to the right side of equation\n";
   for (i = 0; i < p; i++) {
      cout << "b[" << i << ", " << j << " ]=";
      cin >> b[i];
   }
   cout << "Enter initial values of x\n";
   for (i = 0; i < p; i++) {
      cout << "x:[" << i<<"]=";
      cin >> m[i];
   }
   cout << "\nEnter the no. of iteration : ";
   cin >> q;
   while (q> 0) {
      for (i = 0; i < p; i++) {
         n[i] = (b[i] / a[i][i]);
         for (j = 0; j < p; j++) {
            if (j == i)
               continue;
            n[i] = n[i] - ((a[i][j] / a[i][i]) * m[j]);
            m[i] = n[i];
         }
         cout<<"x"<<i + 1 << "="<<n[i]<<" ";
      }
      cout << "\n";
      q--;
   }
   return 0;
}

Hi everyone, the code above is the Gauss-Seidel method.
If I want to use Jacobi method, what should I change in the algorithm?
Last edited on
Gauss-Seidel uses "most-recently-updated" value, so that some of the values on an iteration are calculated using values already updated on that iteration. You don't need both m[] and n[] arrays in your current implentation of it: you could work with just m[].

Jacobi uses only values from the previous iteration to calculate new ones.

So you would have to store all the values from the previous iteration and amend line 33 to use them.
I know that I need to update something in line 33. I tried so many times but it still not working. Do you have any idea?
As I said in my previous post, to use Jacobi's method ...
you would have to store all the values from the previous iteration

You aren't doing that.
Sorry but I still have some confusing here. Can you help me to fix it, please?
First fix your Gauss-Seidel implementation. To be honest, if you replaced every n[i] by m[i] that would work. Then delete line 34.


Then, for Jacobi's method:
- After the while statement on line 27, copy all your current solution in m[] into an array to hold the last-iteration values, say m_old[].

- Make sure that line 29 is updating m[i] not n[i] to work on the new iteration.

- Line 33 would become
m[i] = m[i] - ((a[i][j] / a[i][i]) * m_old[j]);
Note that I've just used m[], not n[]. The crucial bit about Jacobi's method is the m_old[] at the end: you must use last-iteration values, not some of those from the new iteration.
Last edited on
I don't know why it still now working. The result still give wrong
Here's a simpler version of your Gauss-Seidel. When you've checked that out you can easily revise it as Jacobi's method in line with my earlier posts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;
int main()
{
   const int SIZE = 4;
   double a[SIZE][SIZE] = { { 4, -1, 0, 0 }, { -1, 4, -1, 0 }, { 0, -1, 4, -1 }, { 0, 0, -1, 4 } };
   double b[SIZE] = { 2, 4, 6, 13 };
   double m[SIZE] = { 0 };
   int p = SIZE, q;
   
   cout << "Enter the number of iterations: ";   cin >> q;
   while ( q-- )
   {
      for ( int i = 0; i < p; i++ )
      {
         m[i] = b[i] / a[i][i];
         for ( int j = 0; j < p; j++ ) if (j != i) m[i] -= ( a[i][j] / a[i][i] ) * m[j];
         cout << m[i] << '\t';
      }
      cout << '\n';
   }
}


Enter the number of iterations: 20
0.5	1.125	1.78125	3.69531	
0.78125	1.64062	2.83398	3.9585	
0.910156	1.93604	2.97363	3.99341	
0.984009	1.98941	2.9957	3.99893	
0.997353	1.99826	2.9993	3.99982	
0.999566	1.99972	2.99989	3.99997	
0.999929	1.99995	2.99998	4	
0.999988	1.99999	3	4	
0.999998	2	3	4	
1	2	3	4	
1	2	3	4	
1	2	3	4	
1	2	3	4	
1	2	3	4	
1	2	3	4	
1	2	3	4	
1	2	3	4	
1	2	3	4	
1	2	3	4	
1	2	3	4	
The problem is I'm getting stuck how to get m_old[] value
Maybe you should show your code.
Can you please just help me to fix it to Jacobi in my code, please? I'm so messing up here.
If you show us your code, we can help you fix it.

If you don't show us your code, how can we know what you're doing wrong?
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
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
    float a[10][10], b[10], m[10], n[10];
    int p = 0, q = 0, i = 0, j = 0;
    cout << "Enter size of 2D array : ";
    cin >> p;
    for (i = 0; i < p; i++) {
        for (j = 0; j < p; j++) {
            cout << "a[" << i << ", " << j << " ]=";
            cin >> a[i][j];
        }
    }
    cout << "\nEnter values to the right side of equation\n";
    for (i = 0; i < p; i++) {
        cout << "b[" << i << ", " << j << " ]=";
        cin >> b[i];
    }
    cout << "Enter initial values of x\n";
    for (i = 0; i < p; i++) {
        cout << "x:[" << i << "]=";
        cin >> m[i];
    }
    cout << "\nEnter the no. of iteration : ";
    cin >> q;
    while (q > 0) {
        float m_old[10];
        for (i = 0; i < p; i++) {
            m[i] = (b[i] / a[i][i]);
            for (j = 0; j < p; j++) {
                if (j == i)
                    continue;
                m[i] = m[i] - ((a[i][j] / a[i][i]) * m_old[j]);
            }
            cout << "x" << i + 1 << "=" << m[i] << " ";
        }
        cout << "\n";
        q--;
    }
   system("pause");
}



here is my code. the result after running give wrong. Please help me to fix it
Last edited on
Immediately after the line
float m_old[10];
copy the current values from m[] into it:
for (i = 0; i < p; i++ ) m_old[i] = m[i];
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
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
    float a[10][10], b[10], m[10], n[10];
    int p = 0, q = 0, i = 0, j = 0;
    cout << "Enter size of 2D array : ";
    cin >> p;
    for (i = 0; i < p; i++) {
        for (j = 0; j < p; j++) {
            cout << "a[" << i << ", " << j << " ]=";
            cin >> a[i][j];
        }
    }
    cout << "\nEnter values to the right side of equation\n";
    for (i = 0; i < p; i++) {
        cout << "b[" << i << ", " << j << " ]=";
        cin >> b[i];
    }
    cout << "Enter initial values of x\n";
    for (i = 0; i < p; i++) {
        cout << "x:[" << i << "]=";
        cin >> m[i];
    }
    cout << "\nEnter the no. of iteration : ";
    cin >> q;
    while (q > 0) {
        float m_old[10];
        for (i = 0; i < p; i++) {
            m_old[i] = m[i];
            m[i] = (b[i] / a[i][i]);
            for (j = 0; j < p; j++) {
                if (j == i)
                    continue;
                m[i] = m[i] - ((a[i][j] / a[i][i]) * m_old[j]);
            }
            cout << "x" << i + 1 << "=" << m[i] << " ";
        }
        cout << "\n";
        q--;
    }
    system("pause");
}

I tried it here. The result after I input value still give wrong.
I solved it. Thank you so much for you help @lastchance
Topic archived. No new replies allowed.