Advertisement
alexdmin

hauss

Oct 12th, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include<iostream>
  2. #include<math.h>
  3. #include <algorithm>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. double* Gauss(double** Array_A, double* Array_B, int n);
  9. double* Vector_F(double** Array_A, double* Array_B, double* X, int n);
  10. double norma(double* F, int n);
  11. double* Vector_B2(double** Array_A, double* X, int n);
  12. double* difference(double* X1, double* X2, int n);
  13.  
  14. int main()
  15. {
  16.     setlocale(0, "");
  17.     int n;
  18.     cout << "Введите порядок матрицы: ";
  19.    
  20.     do
  21.     {
  22.         cin >> n;
  23.     } while (n < 0);
  24.    
  25.     double** Array_A = new double* [n];
  26.     double* Array_B = new double[n];
  27.     double** Copy_A = new double* [n];
  28.    
  29.     double* Copy_B = new double[n];
  30.    
  31.     cout << "\nВведите матрицу коэффициентов А: \n";
  32.    
  33.     for (int i = 0; i < n; i++)
  34.     {
  35.         Array_A[i] = new double[n];
  36.         Copy_A[i] = new double[n];
  37.         for (int j = 0; j < n; j++)
  38.         {
  39.             cin >> Array_A[i][j];
  40.             Copy_A[i][j] = Array_A[i][j];
  41.         }
  42.     }
  43.    
  44.     cout << "\nВведите вектор свободных членов В: \n";
  45.    
  46.     for (int i = 0; i < n; i++)
  47.     {
  48.         cin >> Array_B[i];
  49.         Copy_B[i] = Array_B[i];
  50.     }
  51.    
  52.     double* X = Gauss(Array_A, Array_B, n);
  53.     cout << "\nРешение СЛАУ: ";
  54.    
  55.     for (int i = 0; i < n; i++)
  56.     {
  57.         cout << X[i] << " ";
  58.     }
  59.    
  60.     cout << endl;
  61.     double* F = Vector_F(Copy_A, Copy_B, X, n);
  62.     cout << "\nВектор невязки F: ";
  63.    
  64.     for (int i = 0; i < n; i++)
  65.     {
  66.         cout << F[i] << " ";
  67.     }
  68.    
  69.     cout << endl;
  70.     cout << "Норма вектора невязки F: ";
  71.     double Norma = norma(F, n);
  72.     cout << Norma;
  73.     double* B2 = Vector_B2(Copy_A, X, n);
  74.     double* X2 = Gauss(Copy_A, B2, n);
  75.     cout << "\nРешение вспомогательной системы: ";
  76.    
  77.     for (int i = 0; i < n; i++)
  78.     {
  79.         cout << X2[i] << " ";
  80.     }
  81.    
  82.     cout << endl;
  83.     cout << "Относительная погрешность: " << setprecision(16) << norma(difference(X, X2, n), n) / norma(X, n);
  84.     cout << endl;
  85.     system("pause");
  86.     return 0;
  87. }
  88.  
  89. double* Gauss(double** Array_A, double* Array_B, int n)
  90. {
  91.     double* X = new double[n];  //массив ответов
  92.     for (int k = 0; k < n; k++) // прямой ход
  93.     {
  94.         for (int i = k + 1; i < n; i++)
  95.         {
  96.             if (abs(Array_A[i][k]) > abs(Array_A[k][k]))
  97.             {
  98.                 swap(Array_A[i], Array_A[k]);  //меняются адреса т.к. двумерный массив
  99.                 swap(Array_B[i], Array_B[k]);   //меняются значения
  100.             }
  101.         }
  102.         double A_Main = Array_A[k][k];
  103.         if (A_Main == 0)
  104.         {
  105.             cout << "error\n";
  106.         }
  107.         for (int i = k; i < n; i++)
  108.         {
  109.             Array_A[k][i] /= A_Main;
  110.         }
  111.         Array_B[k] /= A_Main;
  112.         for (int i = k + 1; i < n; i++)
  113.         {
  114.             double s = Array_A[i][k];
  115.             for (int j = k; j < n; j++)
  116.             {
  117.                 Array_A[i][j] -= s * Array_A[k][j];
  118.             }
  119.             Array_B[i] -= s * Array_B[k];
  120.         }
  121.     }
  122.     for (int k = n - 1; k >= 0; k--)  //обратный ход
  123.     {
  124.         X[k] = Array_B[k];
  125.         for (int i = n - 1; i > k; i--)
  126.         {
  127.             X[k] -= Array_A[k][i] * X[i];
  128.         }
  129.     }
  130.     return X;
  131. }
  132.  
  133. double* Vector_F(double** Array_A, double* Array_B, double* X, int n)
  134. {
  135.     double* result = new double[n];
  136.     for (int i = 0; i < n; i++)
  137.     {
  138.         result[i] = -Array_B[i];
  139.         for (int j = 0; j < n; j++)
  140.         {
  141.             result[i] += Array_A[i][j] * X[j];
  142.         }
  143.     }
  144.     return result;
  145. }
  146.  
  147. double norma(double* F, int n)
  148. {
  149.     double Norma = abs(F[0]);
  150.     for (int i = 0; i < n; i++)
  151.     {
  152.         Norma = max(F[i], Norma);
  153.     }
  154.     return Norma;
  155. }
  156.  
  157. double* Vector_B2(double** Array_A, double* X, int n)
  158. {
  159.     double* B2 = new double[n];
  160.     for (int i = 0; i < n; i++)
  161.     {
  162.         B2[i] = 0;
  163.         for (int j = 0; j < n; j++)
  164.         {
  165.             B2[i] += Array_A[i][j] * X[j];
  166.         }
  167.     }
  168.     return B2;
  169. }
  170.  
  171. double* difference(double* X1, double* X2, int n)
  172. {
  173.     double* x = new double[n];
  174.     for (int i = 0; i < n; i++)
  175.     {
  176.         x[i] = X2[i] - X1[i];
  177.     }
  178.     return x;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement