Advertisement
DarkDevourer

Практика - задание 2 (нет графики)

Jun 30th, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <math.h>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9.  
  10. double func(double x); //Вычисляет значение функции f(x)=x*e^(x)*sin(x)
  11.  
  12. double f2(double x, double h); //Вторая производная
  13.  
  14. int main(int argc, char * argv[])
  15. {
  16. int n = 48;
  17. double a = 0, b = 1, h, e = 0.001, S, f2max, f2n, fa, fb, fx;
  18. int i;
  19. setlocale(LC_ALL, "RUSSIAN");
  20. cout << "Нахождение определенного интеграла функции f(x)=x*e^(x)*sin(x) на отрезке [0;1]. Отрезок разбивается на 48 частей." << endl;
  21. while (1)
  22. {
  23. h = (b - a) / n;
  24. fa = func(a);
  25. fb = func(b);
  26. S = h * (fa + fb) / 2;
  27. for (i = 0; i < n; i++)
  28. {
  29. fx = func(a + h * i);
  30. S = S + fx*h;
  31. }
  32. f2max = f2(a, h);
  33. for (i = 0; i < n; i++)
  34. {
  35. f2n = f2(a + h * i, h);
  36. if (f2n > f2max) f2max = f2n;
  37. }
  38. double check = f2max * pow(b - a, 3) / (12 * n*n);
  39. if (check > e)
  40. {
  41. n = n + 5;
  42. }
  43. else
  44. {
  45. cout << "Значение интеграла = " << S << endl;
  46. break;
  47. }
  48.  
  49. }
  50. system("pause");
  51. return 0;
  52. }
  53. double func(double x) //Вычисляет значение функции f(x)=x*e^(x)*sin(x)
  54. {
  55. return x * exp(x)*sin(x);
  56. }
  57.  
  58. double f2(double x, double h) //Вторая производная
  59. {
  60. return (func(x + h) - 2 * func(x) + func(x - h)) / (h * h);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement