kxcoze

tanya_cringe_index

Mar 31st, 2022
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. void obr_matr(vector<vector<double>>& a) {
  9.     double det;
  10.     det = a[0][0] * a[1][1] - a[0][1] * a[1][0];
  11.     a[0][0] = a[1][1] / det;
  12.     a[1][1] = a[0][0] / det;
  13.     a[0][1] = -a[0][1] / det;
  14.     a[1][0] = -a[1][0] / det;
  15. }
  16.  
  17. int main() {
  18.     setlocale(LC_ALL, "RUS");
  19.     double x = 1.5, y = 0.5, eps = 0.0001;
  20.     double x_new, y_new;
  21.     int n = 0;
  22.     do {
  23.         x_new = x;
  24.         y_new = y;
  25.         x = 0.8 + sin(y + 1);
  26.         y = 1.3 - sin(x - 1);
  27.         n++;
  28.     } while (abs(x_new - x) > eps || abs(y_new - y) > eps);
  29.     cout << "\nМетод простых итераций \nx = " << x << "\ny = " << y;
  30.     cout << "nКол-во итераций: " << n;
  31.  
  32.     x = 1.5, y = 0.5;
  33.     vector<vector<double>> j(2, vector<double>(2));
  34.     double f1, f2, df1dx, df1dy = 1, df2dx = 1, df2dy, F1, F2;
  35.  
  36.     int k = 0;
  37.     do {
  38.         x_new = x;
  39.         y_new = y;
  40.         f1 = sin(x - 1) - 1.3 + y;
  41.         f2 = x - sin(y + 1) - 0.8;
  42.         df1dx = cos(x - 1);
  43.         df2dy = -cos(y + 1);
  44.         j[0][0] = df1dx;
  45.         j[0][1] = df2dx;
  46.         j[1][0] = df1dy;
  47.         j[1][1] = df2dy;
  48.         obr_matr(j);
  49.         F1 = -j[0][0] * f1 - j[0][1] * f2;
  50.         F2 = -j[1][0] * f1 - j[1][1] * f2;
  51.         x = x + F1;
  52.         y = y + F2;
  53.         cout << k << endl;
  54.         k++;
  55.     } while (abs(x_new - x) > eps && abs(y_new - y) > eps);
  56.     cout << "\n\nМетод Ньютона \nx = " << x << "\ny = " << y;
  57.     cout << "\nКол-во итераций: " << k;
  58.  
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment