Advertisement
greannmhar

Адаптивный

May 31st, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define MAX_ITER 1000
  5. #define EPSILON 1e-6
  6.  
  7. double f(double x) {
  8. return sqrt(1 + pow(x, 5));
  9. }
  10.  
  11. double trapezoidal(double a, double b, int n, double (*func)(double)) {
  12. double h = (b - a) / n;
  13. double sum = 0.5 * (func(a) + func(b));
  14. for (int i = 1; i < n; i++) {
  15. sum += func(a + i * h);
  16. }
  17. return h * sum;
  18. }
  19.  
  20. double runge(double a, double b, double epsilon, double (*func)(double)) {
  21. double h = b - a;
  22. double I1 = trapezoidal(a, b, 2, func);
  23. double I2 = trapezoidal(a, b, 4, func);
  24. double delta = fabs(I2 - I1) / 3.0;
  25. int iter = 0;
  26.  
  27. while (delta > epsilon && iter < MAX_ITER) {
  28. h /= 2;
  29. I1 = I2;
  30. I2 = trapezoidal(a, b, 4 * (iter + 1), func);
  31. delta = fabs(I2 - I1) / 3.0;
  32. iter++;
  33. }
  34.  
  35. return I2;
  36. }
  37.  
  38. int main() {
  39. double a, b, epsilon;
  40. printf("Enter the limits of integration (a, b): ");
  41. scanf("%lf%lf", &a, &b);
  42. printf("Enter the desired accuracy (epsilon): ");
  43. scanf("%lf", &epsilon);
  44.  
  45. double result = runge(a, b, epsilon, f);
  46. printf("Approximate value of the integral: %.6lf\n", result);
  47.  
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement