Advertisement
Infiniti_Inter

Дихотомии

Oct 7th, 2020 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 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-3;
  8. const double DELTA = 1e-5;
  9. const int INF = 1e8 + 13;
  10.  
  11.  
  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 left = -1;
  22. double right = 3;
  23. double step = 0.1;
  24. double answer = INF;
  25. double answerPoint = -INF;
  26. while (right - left > EPS)
  27. {
  28. double mid = (left + right) / 2;
  29. double F1 = func(mid - DELTA);
  30. double F2 = func(mid + DELTA);
  31. if (F1 > F2)
  32. left = mid;
  33. else
  34. if (F2 > F1)
  35. right = mid;
  36. if (F1 == F2)
  37. return { F1, mid };
  38. }
  39. answer = func((left + right) / 2);
  40. answerPoint = (left + right) / 2;
  41. return make_pair(answer, answerPoint);
  42. }
  43.  
  44. int main()
  45. {
  46. freopen("input.txt", "r", stdin);
  47. freopen("output.txt", "w", stdout);
  48.  
  49. pair<double, double> ans = solve();
  50. cout << "min = " << ans.first << "\nwith x = " << ans.second << endl;
  51.  
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement