Advertisement
Falexom

Untitled

May 3rd, 2022
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef vector<vector<double>> real_matrix;
  6. const double EPS = 1E-9;
  7.  
  8. void input_matrix(real_matrix &arr, int n) {
  9. cout << "Вводите матрицу" << endl;
  10. arr.resize(n);
  11. for (int i = 0; i < n; i++) {
  12. cout << "Введите строчку " << to_string(i) << endl;
  13. arr[i].resize(n);
  14. for (int j = 0; j < n; j++)
  15. cin >> arr[i][j];
  16. }
  17. }
  18.  
  19. vector<double> iterations_method(real_matrix a, vector<double> b, int n) {
  20. vector<double> previous_x(n, 0.0);
  21. for (int i = 0; i < n; i++)
  22. previous_x[i] = b[i];
  23.  
  24. while (true) {
  25. vector<double> current_x(n);
  26.  
  27. for (int i = 0; i < n; i++) {
  28. current_x[i] = b[i];
  29. for (int j = 0; j < n; j++)
  30. if (i != j)
  31. current_x[i] -= a[i][j] * previous_x[j];
  32. current_x[i] /= a[i][i];
  33. }
  34.  
  35. double error = 0.0;
  36. for (int i = 0; i < n; i++)
  37. error += abs(current_x[i] - previous_x[i]);
  38. if (error < EPS)
  39. break;
  40.  
  41. previous_x = current_x;
  42. }
  43.  
  44. return previous_x;
  45. }
  46.  
  47. int main() {
  48. int n;
  49. cout << "Введите n: " << endl;
  50. cin >> n;
  51.  
  52. real_matrix a;
  53. input_matrix(a, n);
  54.  
  55. vector<double> b;
  56. b.resize(n);
  57. cout << "Введите вектор свободных членов: " << endl;
  58. for (int i = 0; i < n; i++)
  59. cin >> b[i];
  60.  
  61. vector<double> x = iterations_method(a, b, n);
  62.  
  63. for (int i = 0; i < x.size(); i++)
  64. cout << "x" << to_string(i) << " = " << x[i] << endl;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement