Advertisement
StoneHaos

m_mod_1

Mar 30th, 2021
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <utility>
  6.  
  7. using namespace std;
  8.  
  9. typedef pair<double, double> pdd;
  10. const double dx = 0.0001;
  11.  
  12. double func1(double x) {
  13.     return -pow(x, 2.0 / 3) - pow(1 - x * x, 1.0 / 3);
  14. }
  15.  
  16. double func2(double x) {
  17.     return (func1(x + dx) - func1(x)) / dx;
  18. }
  19.  
  20. double func3(double x) {
  21.     return (func2(x + dx) - func2(x)) / dx;
  22. }
  23.  
  24. void print(pdd p) {
  25.     cout << p.first << " " << p.second << endl;
  26. }
  27.  
  28.  
  29. pdd SvenMin(double x0, double h) {
  30.     double f1 = func1(x0);
  31.     double f2 = func1(x0 + h);
  32.     double f0 = func1(x0 - h);
  33.     int k = 1;
  34.     double x1, x2;
  35.  
  36.     if (f0 >= f1 && f1 >= f2) {
  37.         x1 = x0 + h;
  38.     }
  39.     else if (f0 <= f1 && f1 <= f2) {
  40.         x1 = x0 - h;
  41.         h = -h;
  42.     }
  43.     else {
  44.         return pdd(x0 - h, x0 + h);
  45.     }
  46.  
  47.     x2 = x1;
  48.     x1 = x0;
  49.     do {
  50.         x0 = x1;
  51.         x1 = x2;
  52.         x2 = x1 + pow(2, 1.0 * k) * h;
  53.         k++;
  54.     } while (func1(x2) <= func1(x1));
  55.    
  56.     if (h > 0) {
  57.         return pdd(x0, x2);
  58.     }
  59.     else {
  60.         return pdd(x2, x0);
  61.     }
  62.  
  63. }
  64.  
  65. double NT(pdd p, double eps) {
  66.     double x0;
  67.     double x1 = p.first;
  68.     do {
  69.         x0 = x1;
  70.         x1 = x0 - func2(x0) / func3(x0);
  71.     } while (abs(x1 - x0) > eps);
  72.     return x1;
  73. }
  74.  
  75. int main(void) {
  76.  
  77.     setlocale(LC_ALL, "Russian");
  78.    
  79.     pdd a = SvenMin(0.5, 0.1);
  80.     cout << "Отрезок, в котором содержится точка минимума: ";
  81.     print(a);
  82.  
  83.     double b = NT(a, 0.0001);
  84.     cout << "Точка минимума: ";
  85.  
  86.     cout << "(" << b << ";" << func1(b) << ")" << endl;
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement