Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6. // s = 3.059116539645953
  7.  
  8.  
  9. double F(double x)
  10. {
  11. return pow(M_E, x) / x; // функция
  12. }
  13.  
  14.  
  15. double riemann_sum(double a, double b, int n) //прямоугольник
  16. {
  17. double s = 0, h, x;
  18. h = (b - a) / n;
  19.  
  20. for (int i = 0; i < n; i++)
  21. {
  22. x = a + i * h;
  23. s += F(x + h/2);
  24. }
  25.  
  26. s *= h;
  27. //s += ((h * h) / 24) * 1.84726; // R1 считай сам
  28. return s;
  29. }
  30.  
  31.  
  32. double trapezoidal_rule(double a, double b, int n) //трапеция
  33. {
  34. double s = 0, x1, x2, h;
  35. h = (b - a) / n;
  36.  
  37. for (int i = 0; i < n; i++)
  38. {
  39. x1 = a + i * h;
  40. x2 = x1 + h;
  41. s += F(x1) + F(x2);
  42. }
  43. s *= h / 2;
  44. //s -= ((h * h) / 12) * 1.84726; // R2 считай сам
  45. return s;
  46. }
  47.  
  48.  
  49. double simpsons_rule(double a, double b, int n) // парабола
  50. {
  51. double s = 0, x1, x2, x3, h;
  52. h = (b - a) / n;
  53.  
  54. for (int i = 0; i < n / 2; i++)
  55. {
  56. x1 = a + 2 * i * h;
  57. x2 = x1 + h;
  58. x3 = x2 + h;
  59. s += F(x1) + 4 * F(x2) + F(x3);
  60. }
  61. s *= h / 3;
  62. //s -= ((h * h * h * h) / 180) * 6.36019; // R3 считай сам
  63. return s;
  64. }
  65.  
  66. double runge_rule_difference(double (*method)(double, double, int), int n)
  67. {
  68. double I1, I2;
  69.  
  70. I1 = (*method)(1, 2, n);
  71. I2 = (*method)(1, 2, n * 2);
  72.  
  73. return fabs(I2 - I1);
  74. }
  75.  
  76.  
  77. int main() {
  78. double s_riemann, s_trapezoidal, s_simpson, e;
  79.  
  80. e = 0.0000001;
  81.  
  82.  
  83. cout << "The program calculates the integration e ^ x / x on the spider [1;2]" << endl
  84. << "Input e " ;
  85. //cin >> e;
  86. cout << endl;
  87.  
  88. int n = 1;
  89. while (e < runge_rule_difference(riemann_sum, n) / 3) { n *= 2; }
  90. s_riemann = riemann_sum(1, 2 , n * 2);
  91.  
  92. cout << "Result for the rectangle method " << s_riemann << endl
  93. << "n = " << n * 2 << endl
  94. << "Runge rule "<< runge_rule_difference(riemann_sum, n) / 3 << endl << endl;
  95.  
  96. n = 1;
  97. while (e < runge_rule_difference(trapezoidal_rule, n) / 3) { n *= 2; }
  98. s_trapezoidal = trapezoidal_rule(1, 2 , n * 2);
  99.  
  100. cout << "Result for the trapezoid method " << s_trapezoidal << endl
  101. << "n = " << n * 2 << endl
  102. <<"Runge rule " << runge_rule_difference(trapezoidal_rule, n) / 3 << endl << endl;
  103.  
  104. n = 1;
  105.  
  106. while (e < runge_rule_difference(simpsons_rule, n) / 15) { n *= 2; }
  107.  
  108. s_simpson = simpsons_rule(1, 2 , n * 2);
  109.  
  110. cout << "Result for Simpson formula " << s_simpson << endl
  111. << "n = " << n * 2 << endl
  112. << "Runge rule " << runge_rule_difference(simpsons_rule, n) / 15 << endl << endl;
  113.  
  114.  
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement