Advertisement
Infiniti_Inter

метод парабол

Oct 7th, 2020 (edited)
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<iostream>
  3. #include<string>
  4. #include<alg.h>
  5.  
  6. using namespace std;
  7. const double EPS = 1e-4;
  8. const double DELTA = 1e-5;
  9. const int INF = 1e8 + 13;
  10. const double goldenRatio1 = (3 - sqrt(5)) / 2; // "Золотое" число
  11. const double goldenRatio2 = (sqrt(5) - 1) / 2; // "Золотое" число
  12.  
  13. double func(double x)
  14. {
  15.     return 2. * x*x - 6 * x - 3;
  16. }
  17.  
  18. pair<double, double> solve()
  19. {
  20.  
  21.     double h = 0.001;
  22.     double x = -1;
  23.    
  24.     if (x == 0)
  25.         x += 0.1;
  26.     while ((func(x + h) - 2 * func(x) + func(x - h)) / (h*h) <= 0)
  27.         x += 0.1;
  28.     double x1;
  29.     x1 = x - 0.5*h*(func(x + h) - func(x - h)) / (func(x + h) - 2 * func(x) + func(x - h));
  30.     while (fabs(x1 - x) > EPS)
  31.     {
  32.         x = x1;
  33.         x1 = x - 0.5*h*(func(x + h) - func(x - h)) / (func(x + h) - 2 * func(x) + func(x - h));
  34.     }
  35.    
  36.     return { x1, func(x1) };
  37. }
  38.  
  39. int main()
  40. {
  41.     freopen("input.txt", "r", stdin);
  42.     freopen("output.txt", "w", stdout);
  43.  
  44.     pair<double, double> ans = solve();
  45.     cout << "min = " << ans.first << "\nwith x =  " << ans.second << endl;
  46.  
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement