fork(4) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cassert>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. //Метод деления отрезка пополам
  9. double bisekcii(double a, double b, double e, int& i, double (*f)(double))
  10. {
  11. i = 0;
  12. double fa = f(a), fb = f(b);
  13. assert(fa*fb < 0);
  14. while(b-a >= e)
  15. {
  16. double c=(a+b)/2;
  17. double fc = f(c);
  18. if (fa*fc <= 0)
  19. b=c;
  20. else
  21. a=c;
  22. i++;
  23. }
  24. return (a+b)/2.0;
  25. }
  26.  
  27.  
  28. double iteracii(double x, double e, int& i, double (*f)(double))
  29. {
  30. i=0;
  31.  
  32. double y = f(x);
  33.  
  34. while(fabs(y-x) >=e )
  35. {
  36. x = y;
  37. y = f(x);
  38. i++;
  39. }
  40. return y;
  41. }
  42.  
  43. double f1(double x)
  44. {
  45. return 1/(1.2*tan(x)+sqrt(x+1))-x;
  46. }
  47.  
  48. double f2(double x)
  49. {
  50. double z = (sin(x)+cos(x));
  51. z = z*z;
  52.  
  53. return z*z/(33.5*33.5*33.5*33.5)/3+sqrt(3./7);
  54. }
  55.  
  56. double f3(double x)
  57. {
  58. return f2(x)-x;
  59. }
  60.  
  61. int main(int argc, char * argv[])
  62. {
  63. int it;
  64. cout << bisekcii(0,2,1e-8,it,f1);
  65. cout << " Iterations: " << it << endl;
  66.  
  67. cout << iteracii(0,1e-8,it,f2);
  68. cout << " Iterations: " << it << endl;
  69.  
  70. cout << bisekcii(0,2,1e-8,it,f3);
  71. cout << " Iterations: " << it << endl;
  72.  
  73. }
  74.  
Success #stdin #stdout 0s 5588KB
stdin
Standard input is empty
stdout
0.520548  Iterations: 28
0.654655  Iterations: 2
0.654655  Iterations: 28