Advertisement
greannmhar

интеграл

May 30th, 2024
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. double f(double x);
  4. double f(double x) {
  5. return sqrt(1 + pow(x, 5));
  6. }
  7.  
  8. double composite_simpson(double a, double b);
  9. double composite_simpson(double a, double b) {
  10. return (b - a) * (f(a) + 4 * f(0.5 * (a + b)) + f(b)) / 6;
  11. }
  12.  
  13. double adaptive_simpson(double a, double b, double eps);
  14. double adaptive_simpson(double a, double b, double eps) {
  15. double I;
  16. double I2;
  17.  
  18. if (fabs(b - a) < eps)
  19. return 0;
  20.  
  21. I = composite_simpson(a, b);
  22. I2 = composite_simpson(a, 0.5 * (a + b)) + composite_simpson(0.5 * (a + b), b);
  23.  
  24. // printf("Int [%lf, %lf] I = %lf I2 = %lf\n", a, b, I, I2);
  25.  
  26. if (fabs(I - I2) < eps * (b - a))
  27. return I2;
  28.  
  29. return adaptive_simpson(a, 0.5 * (a + b), eps) + adaptive_simpson(0.5 * (a + b), b, eps);
  30. }
  31.  
  32. double func(double x, double alpha, double eps);
  33. double func(double x, double alpha, double eps) {
  34. return adaptive_simpson(-x, x, eps) - (3-alpha/3);
  35. }
  36.  
  37. double result(double alpha, double eps);
  38. double result(double alpha, double eps) {
  39. double a, b, x;
  40. if ((3-alpha/3) >= 0) {
  41. a = 0;
  42. b = 1;
  43. }
  44. else {
  45. a = -1;
  46. b = 0;
  47. }
  48.  
  49. if (func(a, alpha, eps) * func(b, alpha, eps) > 0)
  50. return NAN;
  51. while (b - a > eps) {
  52. x = (a + b)/2;
  53.  
  54.  
  55. // printf("[%lf, %lf] f(a)=%lf f(b)=%lf\n", a, b, func(a, alpha, eps), func(b, alpha, eps));
  56.  
  57. if (func(x, alpha, eps) * func(b, alpha, eps) < 0) {
  58. a = x;
  59. }
  60.  
  61. else {
  62. b = x;
  63. }
  64. }
  65. return a;
  66. }
  67.  
  68. int main(void) {
  69. double alpha;
  70. double eps;
  71. double x = 0;
  72.  
  73. printf ("enter parametr: \n");
  74.  
  75. if (scanf("%lf", &alpha) != 1) {
  76. printf ("wrong znachenie!\n");
  77. return -1;
  78. }
  79.  
  80. printf ("enter epsilon:\n");
  81.  
  82. if (scanf("%lf", &eps) != 1) {
  83. printf ("wrong znach!\n");
  84. return -1;
  85. }
  86.  
  87. x = result(alpha, eps);
  88.  
  89. printf("x = %lf\n", x);
  90.  
  91. return 0;
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement