thespeedracer38

Gauss Seidal Method

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