Advertisement
Vladislav_Bezruk

Untitled

Jun 6th, 2022
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double f(double x)
  7. {
  8.     return -3 * x / (pow(x, 2) + 1);
  9. }
  10.  
  11. double df(double x)
  12. {
  13.     return 3 * (pow(x, 2) - 1) / pow((pow(x, 2) + 1), 2);
  14. }
  15.  
  16. double d2f(double x)
  17. {
  18.     return -6 * x * (pow(x, 2) - 3) / pow((pow(x, 2) + 1), 3);
  19. }
  20.  
  21. int main()
  22. {
  23.     double x0, x, eps;
  24.     int i = 0;
  25.    
  26.     cout << "x0: ";
  27.     cin >> x0;
  28.    
  29.     cout << "eps: ";
  30.     cin >> eps;
  31.    
  32.     x = x0;
  33.    
  34.     cout << endl;
  35.    
  36.     do {
  37.         cout << "Iteration: " << i << " x = " << x << " f(x) = " << f(x) << " df(x) = " << df(x) << endl;
  38.        
  39.         x = x - df(x) / d2f(x);
  40.        
  41.         i++;
  42.        
  43.     } while (fabs(df(x)) > eps);
  44.    
  45.     cout << "Iteration: " << i << " x = " << x << " f(x) = " << f(x) << " df(x) = " << df(x) << endl;
  46.    
  47.     cout << endl;
  48.    
  49.     cout << "Result: " << "xmin = " << x << " fmin = " << f(x) << endl;
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement