SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #pragma warning ( disable : 4996)
  3.  
  4. using namespace std;
  5.  
  6. double Input()
  7. {
  8.     double Number;
  9.     while (true)
  10.     {
  11.         printf("%s", "Write epsilon: ");
  12.         scanf("%lf", &Number);
  13.         if (Number > 0.1 || Number < 0.0000001)
  14.         {
  15.             printf("%s", "Incorrect epsilon, epsilon must be more then 10^(-7) and less then 10^(-1) try again...\n");
  16.         }
  17.         else
  18.         {
  19.             return Number;
  20.         }
  21.     }
  22. }
  23.  
  24. double func1(double x)
  25. {
  26.     return x * sqrt(1 + x);
  27. }
  28.  
  29. double func2(double x)
  30. {
  31.     return asin(sqrt(x)) / sqrt(x * (1 - x));
  32. }
  33.  
  34. double func3(double x)
  35. {
  36.     return pow(2, x) / (1 + pow(4, x));
  37. }
  38.  
  39. double integral(double func(double(x)), double left_border, double right_border, long long num_of_divizions)
  40. {
  41.     double area = 0, height = abs((right_border - left_border) / num_of_divizions);
  42.     for (long long i = 0; i < num_of_divizions; i++)
  43.     {
  44.         area += ((func(left_border) + func(left_border + height)) / 2) * height;
  45.         left_border += height;
  46.     }
  47.     return area;
  48. }
  49.  
  50. double integral(double func(double), double left_border, double right_border, double epsilon)
  51. {
  52.     int sign = 1;
  53.     if (left_border > right_border)
  54.     {
  55.         swap(right_border, left_border);
  56.         sign = -1;
  57.     }
  58.     double actual_area;
  59.     double next_area;
  60.     long long i = 2;
  61.     while (true)
  62.     {
  63.         actual_area = integral(func, left_border, right_border, i);
  64.         next_area = integral(func, left_border, right_border, i * 2);
  65.         if (abs(actual_area - next_area) <= epsilon)
  66.         {
  67.             printf("Number of divizions: %lld %c", i * 2, '\n');
  68.             return next_area * sign;
  69.         }
  70.         i*=2;
  71.     }
  72. }
  73.  
  74. int main()
  75. {
  76.     double epsilon;
  77.     epsilon = Input();
  78.     printf("%s %lf %c", "Integral of func #1: ", integral(&func1, 2, 7, epsilon), '\n');
  79.     printf("%s %lf %c", "Integral of func #2: ", integral(&func2, 0.2, 0.3, epsilon), '\n');
  80.     printf("%s %lf %c", "Integral of func #3: ", integral(&func3, -13, -2, epsilon), '\n');
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top