Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <cmath>
  4.  
  5. #include <fstream>
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11.  
  12. double functionUnderIntegral(double x) {
  13.  
  14. return (2 * (x*x*x) + cos(x));
  15.  
  16.  
  17. }
  18.  
  19.  
  20.  
  21. double rightRectangleMethod(double a, double b, double n, double h) {
  22.  
  23. double x;
  24.  
  25. double sum = 0;
  26.  
  27.  
  28. for (int i = 1; i <= n; i++) {
  29.  
  30. x = a + i*h;
  31.  
  32. sum += h*functionUnderIntegral(x);
  33.  
  34. }
  35.  
  36. return sum;
  37.  
  38. }
  39.  
  40.  
  41.  
  42. double leftRectangleMethod(double a, double b, double n, double h) {
  43.  
  44. double x;
  45.  
  46. double sum = 0;
  47.  
  48.  
  49. for (int i = 0; i <= (n - 1); i++) {
  50.  
  51. x = a + i*h;
  52.  
  53. sum += h*functionUnderIntegral(x);
  54.  
  55. }
  56.  
  57. return sum;
  58.  
  59. }
  60.  
  61.  
  62.  
  63. double centralRectangleMethod(double a, double b, double n, double h) {
  64.  
  65. double x;
  66.  
  67. double sum = 0;
  68.  
  69.  
  70. for (int i = 0; i <= (n - 1); i++) {
  71.  
  72. x = a + i*h;
  73.  
  74. sum += h*functionUnderIntegral(x + (h / 2));
  75.  
  76. }
  77.  
  78. return sum;
  79.  
  80. }
  81.  
  82.  
  83.  
  84. double trapeziumMethod(double a, double b, double n, double h) {
  85.  
  86. double x;
  87.  
  88. double sum1 = 0;
  89.  
  90. double sum2 = 0;
  91.  
  92.  
  93. for (int i = 1; i <= (n - 1); i++) {
  94.  
  95. x = a + i*h;
  96.  
  97. sum1 += functionUnderIntegral(x);
  98.  
  99. }
  100.  
  101.  
  102. sum2 = (h / 2)*(functionUnderIntegral(a) + functionUnderIntegral(b)) + h*sum1;
  103.  
  104.  
  105. return sum2;
  106.  
  107. }
  108.  
  109.  
  110.  
  111. double simpsonMethod(double a, double b, double n, double h) {
  112.  
  113. double x;
  114.  
  115. double sum1 = 0;
  116.  
  117. double sum2 = 0;
  118.  
  119. double sum3 = 0;
  120.  
  121.  
  122. for (int i = 1; i <= (n - 1); i++) {
  123.  
  124. if ((i % 2) != 0) {
  125.  
  126. x = a + i*h;
  127.  
  128. sum1 += functionUnderIntegral(x);
  129.  
  130. }
  131.  
  132. }
  133.  
  134.  
  135. for (int i = 2; i <= (n - 2); i++) {
  136.  
  137. if ((i % 2) == 0) {
  138.  
  139. x = a + i*h;
  140.  
  141. sum2 += functionUnderIntegral(x);
  142.  
  143. }
  144.  
  145. }
  146.  
  147.  
  148. sum3 = (h / 3)*(functionUnderIntegral(a) + functionUnderIntegral(b) + 4 * sum1 + 2 * sum2);
  149.  
  150. return sum3;
  151.  
  152. }
  153.  
  154.  
  155.  
  156. double analyticalMethod() {
  157.  
  158. return 2 * pow(3.14,3.0) - 1 ;
  159.  
  160.  
  161. }
  162.  
  163.  
  164.  
  165. int main()
  166. {
  167.  
  168.  
  169. double a = 0;
  170.  
  171. double b = (3.14 / 2);
  172.  
  173. double n;
  174.  
  175. double h;
  176.  
  177.  
  178. cout << "n := "; cin >> n;
  179. cout << endl;
  180.  
  181.  
  182. h = (b - a) / n;
  183.  
  184.  
  185. cout << "Right Rectangle Method := " << rightRectangleMethod(a, b, n, h) << endl; cout << endl;
  186.  
  187. cout << "Central Rectangle Method := " << centralRectangleMethod(a, b, n, h) << endl; cout << endl;
  188.  
  189. cout << "Left Rectangle Method := " << leftRectangleMethod(a, b, n, h) << endl; cout << endl;
  190.  
  191. cout << "Trapezium Method := " << trapeziumMethod(a, b, n, h) << endl; cout << endl;
  192.  
  193. cout << "Simpson Method := " << simpsonMethod(a, b, n, h) << endl; cout << endl;
  194.  
  195. cout << "Analytical Method := " << analyticalMethod() << endl; cout << endl;
  196. system("pause");
  197.  
  198.  
  199. return 0;
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement