gashink_t

m_Simple iterat&Seidel(lab_2, BM)

Feb 16th, 2021 (edited)
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. void print(float** MTRX_ORIG, int N);
  7. void print_iterat(float* ARR, int N);
  8. void print_mtrx(float** MTRX, int N);
  9. float** create(int N);
  10. float** create_rand(int N);
  11. bool conver(float** MTRX, int N);
  12. float* simple_iterat_method(float** MTRX, float* ARR, float E, int N);
  13. float* m_Seidel(float** MTRX, float* ARR, float E, int N);
  14.  
  15. int main()
  16. {
  17.     int n, c;
  18.     float** mtrx_orig;
  19.     cout << "Enter the number of equations n = ";
  20.     cin >> n;
  21.     cout << endl << "Would you like to create the matrix? Random(0)/By hand(1)" << endl;
  22.     cin >> c;
  23.     if (c == 0)
  24.         mtrx_orig = create_rand(n);
  25.     else
  26.         mtrx_orig = create(n);
  27.     cout << endl << "Your matrix:" << endl;
  28.     print(mtrx_orig, n);
  29.  
  30.     float* arr = new float[n];
  31.     float** mtrx = new float* [n];
  32.     for (int i = 0; i < n; i++)
  33.     {
  34.         mtrx[i] = new float[n];
  35.     }
  36.     for (int i = 0; i < n; i++)
  37.         for (int j = 0; j <= n; j++)
  38.         {
  39.             if (j != n)
  40.                 mtrx[i][j] = mtrx_orig[i][j];
  41.             else arr[i] = mtrx_orig[i][n];
  42.         }
  43.     cout << endl << "Your array: " << endl;
  44.     for (int i = 0; i < n; i++)
  45.         cout << setw(10) << round(arr[i] * 1000) / 1000;
  46.     cout << endl << "Your matrix: " << endl;
  47.     print_mtrx(mtrx, n);
  48.  
  49.     /*Проверка на сходимость*/
  50.     bool con = conver(mtrx, n);
  51.     if (!con)
  52.     {
  53.         cout << "A function has not been tested for convergence!" << endl;
  54.         return 0;
  55.     }
  56.     cout << "The function passed the convergence test!" << endl;
  57.  
  58.     float e;
  59.     cout << "Entrer the Epsilant E = ";
  60.     cin >> e;
  61.  
  62.     cout << endl << "Choose a solution method: The method of simple iterations(0)/ Seidel's method(1)" << endl;
  63.     cin >> c;
  64.     float* answer = new float[n];
  65.     if (c == 0)
  66.     {
  67.         answer = simple_iterat_method(mtrx, arr, e, n);
  68.     }
  69.     else answer = m_Seidel(mtrx, arr, e, n);
  70.    
  71.     if (!answer) return 0;
  72.     cout << "Answer:" << endl;
  73.     print_iterat(answer, n);
  74.  
  75.     return 0;
  76. }
  77.  
  78.  
  79. void print(float** MTRX_ORIG, int N)
  80. {
  81.     for (int i = 0; i < N; i++)
  82.     {
  83.         for (int j = 0; j <= N; j++)
  84.         {
  85.             if (j < N)
  86.                 cout << setw(10) << round(MTRX_ORIG[i][j] * 1000) / 1000;
  87.             else
  88.                 cout << setw(10) << "|" << round(MTRX_ORIG[i][j] * 1000) / 1000;
  89.         }
  90.         cout << endl;
  91.     }
  92.     cout << endl;
  93. }
  94.  
  95. void print_iterat(float* ARR, int N)
  96. {
  97.     for (int i = 0; i < N; i++)
  98.     {
  99.         cout << "x" << i + 1 << " = " << setw(5) << round(ARR[i] * 1000) / 1000;
  100.         cout << endl;
  101.     }
  102.        
  103. }
  104.  
  105. void print_mtrx(float** MTRX, int N)
  106. {
  107.     for (int i = 0; i < N; i++)
  108.     {
  109.         for (int j = 0; j < N; j++)
  110.             cout << setw(10) << round(MTRX[i][j] * 1000) / 1000;
  111.         cout << endl;
  112.     }
  113.     cout << endl;
  114. }
  115.  
  116. float** create(int N)
  117. {
  118.     float** MTRX_ORIG = new float* [N];
  119.     for (int i = 0; i < N; i++)
  120.     {
  121.         MTRX_ORIG[i] = new float[N + 1];
  122.     }
  123.  
  124.     cout << endl << "Enter the element matrix:" << endl;
  125.     for (int i = 0; i < N; i++)
  126.         for (int j = 0; j <= N; j++)
  127.         {
  128.             cin >> MTRX_ORIG[i][j];
  129.         }
  130.     return MTRX_ORIG;
  131. }
  132.  
  133. float** create_rand(int N)
  134. {
  135.     float** MTRX_ORIG = new float* [N];
  136.     for (int i = 0; i < N; i++)
  137.     {
  138.         MTRX_ORIG[i] = new float[N + 1];
  139.     }
  140.     for (int i = 0; i < N; i++)
  141.         for (int j = 0; j <= N; j++)
  142.         {
  143.             MTRX_ORIG[i][j] = rand() % 100;
  144.         }
  145.     return MTRX_ORIG;
  146. }
  147.  
  148. bool conver(float** MTRX, int N)
  149. {
  150.     float sum = 0;
  151.     for (int i = 0; i < N; i++)
  152.     {
  153.         for (int j = 0; j < N; j++)
  154.         {
  155.             sum += abs(MTRX[i][j]);
  156.         }
  157.         if (abs(MTRX[i][i]) <= sum - abs(MTRX[i][i]))
  158.             return false;
  159.         sum = 0;
  160.     }
  161.     return true;
  162. }
  163.  
  164. float* simple_iterat_method(float** MTRX, float* ARR,float E, int N)
  165. {
  166.     float* ANSWER = new float[N];
  167.     for (int i = 0; i < N; i++)
  168.         ANSWER[i] = 0;
  169.     float* X = new float[N];
  170.     bool c = true;
  171.     int k = 0;
  172.     while (c)
  173.     {
  174.         for (int i = 0; i < N; i++)
  175.         {
  176.             X[i] = ARR[i] / MTRX[i][i];
  177.             for (int j = 0; j < N; j++)
  178.             {
  179.                 if (i != j)
  180.                     X[i] -= MTRX[i][j] * ANSWER[i] / MTRX[i][i];
  181.             }
  182.         }
  183.  
  184.         for (int i = 0; i < N; i++)
  185.         {
  186.             if (E > abs(X[i] - ANSWER[i]))
  187.             {
  188.                 c = false;
  189.                 break;
  190.             }
  191.         }
  192.  
  193.         for (int i = 0; i < N; i++)
  194.             ANSWER[i] = X[i];
  195.         k++;
  196.         cout << endl << "Iteration " << k << endl;
  197.         print_iterat(ANSWER, N);
  198.         cout << endl;
  199.     }
  200.    
  201.     return ANSWER;
  202. }
  203.  
  204. float* m_Seidel(float** MTRX, float* ARR, float E, int N)
  205. {
  206.     float* ANSWER = new float[N];
  207.     for (int i = 0; i < N; i++)
  208.         ANSWER[i] = 0;
  209.     float* X = new float[N];
  210.     bool c = true;
  211.     int k = 0;
  212.     while (c)
  213.     {
  214.         for (int i = 0; i < N; i++)
  215.         {
  216.             X[i] = ARR[i] / MTRX[i][i];
  217.             for (int j = 0; j < N; j++)
  218.             {
  219.                 if (i != j)
  220.                 {
  221.                     if (j < i)
  222.                         X[i] -= MTRX[i][j] * X[i - 1] / MTRX[i][i];
  223.                     else X[i] -= MTRX[i][j] * ANSWER[i] / MTRX[i][i];
  224.                 }
  225.             }
  226.         }
  227.  
  228.         for (int i = 0; i < N; i++)
  229.         {
  230.             if (E > abs(X[i] - ANSWER[i]))
  231.             {
  232.                 c = false;
  233.                 break;
  234.             }
  235.         }
  236.  
  237.         for (int i = 0; i < N; i++)
  238.             ANSWER[i] = X[i];
  239.         k++;
  240.         cout << endl << "Iteration " << k << endl;
  241.         print_iterat(ANSWER, N);
  242.         cout << endl;
  243.     }
  244.  
  245.     return ANSWER;
  246. }
  247.  
Add Comment
Please, Sign In to add comment