Advertisement
Niktiaksk

Untitled

Oct 29th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. const double lowestLimits[3] = {0, 0, -2};
  7. const double highestLimits[3] = {1.7, 2, -1.2};
  8.  
  9. double (*integralFunctions[3])(double, double) = {
  10.     [](double x, double step)
  11.     { return 0.5 * step * (x * atan(x) + (x + step) * atan(x + step)); },
  12.     [](double x, double step)
  13.     { return 0.5 * step * (1 / (1 + sqrt(x)) + (1 / (1 + sqrt(x + step)))); },
  14.     [](double x, double step)
  15.     { return 0.5 * step * (2 / (1 - 4 * x) + (2 / (1 - 4 * (x + step)))); }
  16. };
  17.  
  18. double integral(int n, int indexOfIntegral)
  19. {
  20.     double step = abs(highestLimits[indexOfIntegral] - lowestLimits[indexOfIntegral]) / n;
  21.     double result = 0, sum = 0, x = lowestLimits[indexOfIntegral];
  22.     for (int i = 0; i < n; i++)
  23.     {
  24.         sum = (integralFunctions[indexOfIntegral](x, step));
  25.         x += step;
  26.         result += sum;
  27.     }
  28.     return result;
  29. }
  30.  
  31. int main()
  32. {
  33.     double acc;
  34.     cout << "Input accuracy: ";
  35.     cin >> acc;
  36.     cout << endl;
  37.  
  38.     for (int i = 0; i < 3; i++)
  39.     {
  40.         int n = 1;
  41.         double previousResult, currentResult = integral(n, i);
  42.         do
  43.         {
  44.             ++n;
  45.             previousResult = currentResult;
  46.             currentResult = integral(n, i);
  47.         } while (n < INT_MAX && !(abs(currentResult - previousResult) <= acc));
  48.  
  49.         cout << "### - " << i + 1 << endl;
  50.         cout << "amount of trapezoids = " << n << endl;
  51.         cout << "integral of n = " << previousResult << endl;
  52.         cout << "integral of n + 1 = " << currentResult << endl;
  53.         cout << "delta = " << abs(currentResult - previousResult) << endl
  54.              << endl;
  55.     }
  56.  
  57.     return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement