fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5.  
  6. double f(double x)
  7. {
  8. return x * x - 2.0 * x - 2.0 * cos(x);
  9. }
  10.  
  11.  
  12. int main()
  13. {
  14. int quantity = 0;
  15. double a, b, c, epsilon = 0.00189;
  16. cin >> a >> b;
  17.  
  18.  
  19. while (fabs(b - a) > epsilon)
  20. {
  21. quantity++;
  22. c = (a + b) / 2;
  23.  
  24. cout<<f(a)<<" "<<f(c)<<" "<<f(b)<< " [a,b]=";
  25. if (f(a) < f(c))
  26. {
  27. b = c;
  28. }
  29. else
  30. {
  31. a = c;
  32. }
  33. cout<<a<<" "<<b<<endl;
  34. }
  35.  
  36.  
  37. double x_min = (b + a) / 2;
  38. cout << "Локальний мінімум: " << x_min << endl;
  39. cout << "Кількість ітерацій: " << quantity << endl;
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5320KB
stdin
0 2
stdout
-2 -2.0806 0.832294 [a,b]=1 2
-2.0806 -0.891474 0.832294 [a,b]=1 1.5
-2.0806 -1.56814 -0.891474 [a,b]=1 1.25
-2.0806 -1.84673 -1.56814 [a,b]=1 1.125
-2.0806 -1.96947 -1.84673 [a,b]=1 1.0625
-2.0806 -2.02652 -1.96947 [a,b]=1 1.03125
-2.0806 -2.05393 -2.02652 [a,b]=1 1.01562
-2.0806 -2.06736 -2.05393 [a,b]=1 1.00781
-2.0806 -2.07401 -2.06736 [a,b]=1 1.00391
-2.0806 -2.07731 -2.07401 [a,b]=1 1.00195
-2.0806 -2.07896 -2.07731 [a,b]=1 1.00098
Локальний мінімум: 1.00049
Кількість ітерацій: 11