Advertisement
DacCum

Метод Зейделя(in dev...)

Oct 4th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. /*
  2. -0.92 -0.03 0 -0.04
  3. 0.51 -1 0.27 -0.08
  4. 0.33 0 -1.37 0.21
  5. 0.11 0 0.03 -0.42
  6.  
  7. 1.2 -0.81 0.92 -0.17
  8. */
  9.  
  10. #include <iostream>
  11. #include <cmath>
  12. using namespace std;
  13.  
  14. double EPS = 0.00001;
  15.  
  16. double** createMatrix(int line, int column) {
  17.     double** c = new double* [line];
  18.     for (int i = 0; i < line; i++)
  19.         c[i] = new double[column];
  20.    
  21.     return c;
  22. }
  23.  
  24. double** inputMatrix(int line, int column) {
  25.     double** c = createMatrix(line, column);
  26.     for (int i = 0; i < line; i++)
  27.         for (int j = 0; j < column; j++)
  28.             cin >> c[i][j];
  29.  
  30.     return c;
  31. }
  32.  
  33. bool compare_EPS(double* a, int n, double _EPS) {
  34.     bool f = 0;
  35.     for (int i = 0; i < n; i++) {
  36.         if (fabs(a[i]) > _EPS)
  37.             f = 1;
  38.         else
  39.             f = 0;
  40.     }
  41.  
  42.     return f;
  43. }
  44.  
  45. bool is_compatible(double** A, double** B, int line, int column) {
  46.  
  47. }
  48.  
  49. bool is_convergence(double** A, double** B, int line, int column) {
  50.  
  51. }
  52.  
  53. double** culc(double** A, double** B, int line, int column) {
  54.     double** X = createMatrix(line, 1);
  55.     double** X0 = createMatrix(line, 1);
  56.     double* dif = new double[line];
  57.     for (int i = 0; i < line; i++) {
  58.         X[i][0] = 0;
  59.         dif[i] = 0;
  60.     }
  61.  
  62.     do {
  63.         for (int i = 0; i < line; i++)
  64.             X0[i][0] = X[i][0];
  65.         for (int i = 0; i < line; i++) {
  66.             double tmp = 0;
  67.             for (int j = 0; j < column; j++) {
  68.                 if (j == i)
  69.                     continue;
  70.                 else
  71.                     tmp -= A[i][j] * X[j][0];
  72.             }
  73.             X[i][0] = (B[i][0] + tmp) / A[i][i];
  74.             dif[i] = X[i][0] - X0[i][0];
  75.         }
  76.     } while (compare_EPS(dif, line, EPS));
  77.  
  78.     return X;
  79. }
  80.  
  81. int main() {
  82.     int n, m;
  83.     cout << "Enter size A(n m): ";
  84.     cin >> n >> m;
  85.     cout << "Enter Matrix A: " << endl;
  86.     double** A = inputMatrix(n, m);
  87.     cout << "Enter Matrix B: " << endl;
  88.     double** B = inputMatrix(n, 1);
  89.     cout << "Enter EPS: ";
  90.     cin >> EPS;
  91.  
  92.     double** X = culc(A, B, n, m);
  93.     for (int i = 0; i < n; i++)
  94.         cout << "x" << i+1 << " = " << X[i][0] << endl;
  95.    
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement