Advertisement
Vladislav_Bezruk

Untitled

Jan 6th, 2022 (edited)
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. struct Vector {
  7.     double x, y;
  8.     Vector() {x = y = 0;}
  9.     Vector(double a, double b) : x(a), y(b) {}
  10.     Vector operator * (const double k) {
  11.         Vector c(x * k, y * k);
  12.         return c;
  13.     }
  14.     Vector operator - (const Vector &a) {
  15.         Vector c(x - a.x, y - a.y);
  16.         return c;
  17.     }
  18. };
  19.  
  20. Vector df(Vector a) {
  21.     Vector grad;
  22.    
  23.     grad.x = 2 * (sin(a.x + 1) - a.y - 1) * cos(a.x + 1) + 8 * a.x + 4 * cos(a.y) - 8;
  24.     grad.y = -2 * sin(a.x + 1) + 2 * a.y + 2 - 2 * (2 * a.x + cos(a.y) - 2) * sin(a.y);
  25.    
  26.     return grad;
  27. }
  28.  
  29. double norma(Vector a) { return sqrt(pow(a.x, 2)) + (pow(a.y, 2)); }
  30.  
  31. void solve(double EPS, double I) {
  32.     Vector prevX, x;
  33.     int i = 0;
  34.     double d;
  35.    
  36.     do {
  37.         prevX = x;
  38.         d = norma(df(x));
  39.         x = prevX - df(prevX) * I;
  40.        
  41.         i++;
  42.     } while (d >= EPS);
  43.    
  44.     cout << "Iteration :: " << i << "\t   >>\tx, y = {" << x.x << ", " << x.y << "}";
  45.    
  46.     return;
  47. }
  48.  
  49. int main() {
  50.    
  51.     solve(1e-4, 1e-3);
  52.        
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement