TransWikia.com

Jacobi Gauss-Seidel Method

Computational Science Asked by Quan Thanh on November 23, 2020

#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 equationn";
   for (i = 0; i < p; i++) {
      cout << "b[" << i << ", " << j << " ]=";
      cin >> b[i];
   }
   cout << "Enter initial values of xn";
   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 the Jacobi method, what should I change in the algorithm? I tried to do in algorithm before posting but it still gives the wrong answer so I decided to post to ask for help. Can anyone give me an idea, please?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP