Guest User

Untitled

a guest
Oct 23rd, 2019
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. double integral(double func(double(x)), double left_border, double right_border, long long num_of_divizions)
  2. {
  3. double area = 0, height = abs((right_border - left_border) / num_of_divizions);
  4. for (long long i = 0; i < num_of_divizions; i++)
  5. {
  6. area += ((func(left_border) + func(left_border + height)) / 2) * height;
  7. left_border += height;
  8. }
  9. return area;
  10. }
  11.  
  12. double integral(double func(double), double left_border, double right_border, double epsilon)
  13. {
  14. int sign = 1;
  15. if (left_border > right_border)
  16. {
  17. swap(right_border, left_border);
  18. sign = -1;
  19. }
  20. double actual_area;
  21. double next_area;
  22. long long i = 2;
  23. while (true)
  24. {
  25. actual_area = integral(func, left_border, right_border, i);
  26. next_area = integral(func, left_border, right_border, i * 2);
  27. if (abs(actual_area - next_area) <= epsilon)
  28. {
  29. printf("Number of divizions: %lld %c", i * 2, '\n');
  30. return next_area * sign;
  31. }
  32. i*=2;
  33. }
  34. }
RAW Paste Data