Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. double f(double x, double a, double b)
  6. {
  7. double drob_1 = (1.4*x)/(31.7*a+x*x);
  8. double drob_2 = (78*x*exp(-pow(x-1,2)/(2*a*a)))/(56.2+a);
  9. double drob_3 = (14*sin(pi*x/b))/(x*x+3.5*a);
  10. double drob_4 = (cos(pi*x/2*b))/(x*x*x+3);
  11.  
  12. return (drob_1 + drob_2 + drob_3 + drob_4);
  13. }
  14.  
  15. double rectangle_integral(pointFunc f, double a, double b, int n) {
  16. const double h = (b - a) / n;
  17. double sum = 0.0;
  18. #pragma omp parallel for reduction(+:sum)
  19. for (int i = 0; i < n; i++) {
  20. sum += f(a + i*h);
  21. }
  22. return (sum * h);
  23. }
  24.  
  25. int main(){
  26. double a, b, eps;
  27. double s1, s;
  28. int n = 1; //начальное число шагов
  29.  
  30. a =0;
  31. b= 1000;
  32. eps = 0.001;
  33.  
  34. s1 = rectangle_integral(f, a, b, n); //первое приближение для интеграла
  35. do {
  36. s = s1; //второе приближение
  37. n = 2 * n; //увеличение числа шагов в два раза,
  38. //т.е. уменьшение значения шага в два раза
  39. s1 = rectangle_integral(f, a, b, n);
  40. }
  41. while (fabs(s1 - s) > eps); //сравнение приближений с заданной точностью
  42. cout << "\nИнтеграл = " << s1 << endl;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement