fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5.  
  6. // f(x)
  7. double f(double x)
  8. {
  9. return x * atan(x) - 0.5 * log(1 + x * x);
  10. }
  11.  
  12.  
  13. //f'(x) -- похідна
  14. double df(double x)
  15. {
  16. return atan(x) + x/(1+x*x) - (x)/(1+x*x);
  17. //atan(x) + x/(1+x*x) - x/(1+x*x) = просто atan(x)
  18. //але залишимо формально
  19. }
  20.  
  21.  
  22. int main()
  23. {
  24. double a, b;
  25. cin>>a>>b;
  26. double eps;
  27. cin>>eps;
  28.  
  29.  
  30. int it = 0;
  31.  
  32.  
  33. while (fabs(b - a) > eps)
  34. {
  35. it++;
  36. double c = (a + b) / 2.0;
  37. double d = df(c);
  38.  
  39.  
  40. if (fabs(d) < eps) //попадання в точку мінімуму
  41. {
  42. a = b = c;
  43. break;
  44. }
  45.  
  46.  
  47. if (d > 0)
  48. b = c; //мінімум лівіше
  49. else
  50. a = c; //мінімум правіше
  51. }
  52.  
  53.  
  54. double xmin = (a + b)/2.0;
  55. cout<<"xmin = "<<xmin<<endl;
  56. cout<<"iterations = "<<it<<endl;
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0.01s 5284KB
stdin
-10 5 0.01
stdout
xmin = -0.00976562
iterations = 9