Advertisement
kazi_omar

1

Nov 2nd, 2021 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. //example 5.9
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. float expression(float x)
  6. {
  7.     return 1 / (1 + x);
  8. }
  9.  
  10. float lower, upper, integration = 0.0, stepSize, k;
  11. int subInterval;
  12.  
  13. float third()
  14. {
  15.     for (int i = 1; i <= subInterval - 1; i++)
  16.     {
  17.         k = lower + i * stepSize;
  18.  
  19.         if (i % 2 == 0)
  20.         {
  21.             integration = integration + 2 * (expression(k));
  22.         }
  23.         else
  24.         {
  25.             integration = integration + 4 * (expression(k));
  26.         }
  27.     }
  28.  
  29.     integration = integration * stepSize / 3;
  30.     return integration;
  31. }
  32.  
  33. float eighth()
  34. {
  35.     for (int i = 1; i <= subInterval - 1; i++)
  36.     {
  37.         k = lower + i * stepSize;
  38.  
  39.         if (i % 3 == 0)
  40.         {
  41.             integration = integration + 2 * (expression(k));
  42.         }
  43.         else
  44.         {
  45.             integration = integration + 3 * (expression(k));
  46.         }
  47.     }
  48.  
  49.     integration = integration * stepSize * 3.0 / 8.0;
  50.     return integration;
  51. }
  52.  
  53. float trapezoidal()
  54. {
  55.     for (int i = 1; i <= subInterval - 1; i++)
  56.     {
  57.         k = lower + i * stepSize;
  58.         integration = integration + 2 * (expression(k));
  59.     }
  60.  
  61.     integration = integration * stepSize / 2;
  62.     return integration;
  63. }
  64.  
  65. int main()
  66. {
  67.     cin >> lower >> upper >> subInterval;
  68.     stepSize = (upper - lower) / subInterval;
  69.  
  70.     integration = expression(lower) + expression(upper);
  71.  
  72.     cout << "Required value of integration for 3rd rule is: " << third() << endl;
  73.     cout << "Required value of integration for eighth rule is: " << eighth() << endl;
  74.     cout << "Required value of integration for trapezoidal rule is: " << trapezoidal() << endl;
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement