Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. const double pi = 3.1415;
  7. const double e = 2.7182;
  8.  
  9. typedef double(*pointFunc)(double);
  10.  
  11. double f1(double x1) {
  12. return (1/(1+pow(2*x1,0.5)));
  13. }
  14. double f2(double x2) {
  15. return (pow(e,x2)*sin(x2));
  16. }
  17. double f3(double x3) {
  18. return (1/(3+2*cos(x3)));
  19. }
  20. double simpson_integral(pointFunc f, double a, double b, int n) {
  21. const double h = (b-a)/n;
  22. double k1 = 0, k2 = 0;
  23. for(int i = 1; i < n; i += 2) {
  24. k1 += f(a + i*h);
  25. k2 += f(a + (i+1.)*h);
  26. }
  27. return h/3*(f(a) + 4*k1 + 2*k2);
  28. }
  29. int main() {
  30. setlocale(LC_ALL,"ru");
  31. double a1=0,a2=0,a3=0, b1=1,b2=pi/4,b3=pi/6, eps;
  32. double s1,s2,s3,s;
  33. int n = 1;
  34. cout << "\nВведите требуемую точность eps = ";
  35. cin >> eps;
  36. s1 = simpson_integral(f1, a1, b1, n);
  37. do {
  38. s = s1;
  39. n = 2 * n;
  40. s1 = simpson_integral(f1, a1, b1, n);
  41. }
  42. while (fabs(s1 - s) > eps);
  43.  
  44. s2 = simpson_integral(f2, a2, b2, n);
  45. do {
  46. s = s2;
  47. n = 2 * n;
  48. s2 = simpson_integral(f2, a2, b2, n);
  49. } while (fabs(s2 - s) > eps);
  50.  
  51. s3 = simpson_integral(f3, a3, b3, n);
  52. do {
  53. s = s3;
  54. n = 2 * n;
  55. s3 = simpson_integral(f3, a3, b3, n);
  56. } while (fabs(s3 - s) > eps);
  57. cout << "\n (1/(1+pow(2*x,0.5) Интеграл = " << s1 << " количество разбиений = " << n << endl;
  58. cout << "\n (pow(e,x)*sin(x)) Интеграл = " << s2 << " количество разбиений =" << n << endl;
  59. cout << "\n (1/(3+2*cos(x)) Интеграл = " << s3 << " количество разбиений =" << n << endl;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement