thespeedracer38

JacobiMethod

Jan 17th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. class Jacobi{
  7. private:
  8.     float a[10][10], b[10], x[10];
  9.     int n;
  10. public:
  11.     Jacobi(float x[10][10], float b1[10], int m);
  12.     void jacobi();
  13.     void display();
  14. };
  15.  
  16. Jacobi::Jacobi(float x[10][10], float b1[10], int m){
  17.     int i, j;
  18.     n = m;
  19.     for(i = 0; i < n; i++){
  20.         for(j = 0; j < n; j++)
  21.             a[i][j] = x[i][j];
  22.         b[i] = b1[i];
  23.     }
  24. }
  25.  
  26. void Jacobi::jacobi(){
  27.     float s;
  28.     float xg[10], e[10], eps;
  29.     int i, j, k, itr, flag;
  30.    
  31.     flag = 0;
  32.     itr = 0; // itr = number of iterations
  33.     eps = 1e-6; // eps = lowest value of difference of two consecutive roots
  34.    
  35.     // To initialize guess values
  36.     for(i = 0; i < n; i++){
  37.         xg[i] = 0; 
  38.     }
  39.     // Jacobi iteration starts
  40.     while(flag == 0){
  41.         // Assuming convergence is reached
  42.         flag = 1;
  43.         itr = itr + 1;
  44.         for(i = 0; i < n; i++){
  45.             s = 0;
  46.             for(j = 0; j < n; j++)
  47.                 if(i != j){
  48.                     // For Jacobi
  49.                     s = s + a[i][j] * xg[j];
  50.                 }
  51.             x[i] = (b[i] - s) / a[i][i];
  52.             e[i] = x[i] - xg[i];
  53.             if(e[i] < 0)
  54.                 e[i] = -e[i];
  55.         }
  56.             // To check value of e[]
  57.             for(i = 0; i < n; i++)
  58.                 if(e[i] > eps){
  59.                     flag = 0;
  60.                     break;
  61.                 }
  62.             // To change guess values
  63.             for(i = 0; i < n; i++){
  64.                 xg[i] = x[i];
  65.             }
  66.         }
  67.     cout << "\nTotal number of Iterations taken  = " << itr << endl;
  68.     display();
  69. }
  70.  
  71. void Jacobi::display(){
  72.     int i;
  73.     cout << "Solutions of Linear Simultaneous Equations: \n";
  74.     for(i = 0; i < n; i++){
  75.         cout << "x[" << i + 1 << "] = " << x[i] << endl;
  76.     }
  77. }
  78.  
  79. int main(void) {
  80.     system("cls");
  81.     float a[10][10], b[10];
  82.     int i, j, n;
  83.     cout << "Enter the number of simultaneous equations(1-10): ";
  84.     cin >> n;
  85.     cout << "Enter the coefficients of all equations\n";
  86.     for(i = 0; i < n; i++){
  87.         for(j = 0; j < n; j++){
  88.             cout << "a[" << i << "][" << j << "] = ";
  89.             cin >> a[i][j];
  90.         }
  91.         cout << "b[" << i << "] = ";
  92.         cin >> b[i];
  93.     }
  94.     Jacobi J(a, b, n);
  95.     J.jacobi();
  96.     return 0;
  97. }
Add Comment
Please, Sign In to add comment