Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define MAX_ITER 1000
- #define EPSILON 1e-6
- double f(double x) {
- return sqrt(1 + pow(x, 5));
- }
- double trapezoidal(double a, double b, int n, double (*func)(double)) {
- double h = (b - a) / n;
- double sum = 0.5 * (func(a) + func(b));
- for (int i = 1; i < n; i++) {
- sum += func(a + i * h);
- }
- return h * sum;
- }
- double runge(double a, double b, double epsilon, double (*func)(double)) {
- double h = b - a;
- double I1 = trapezoidal(a, b, 2, func);
- double I2 = trapezoidal(a, b, 4, func);
- double delta = fabs(I2 - I1) / 3.0;
- int iter = 0;
- while (delta > epsilon && iter < MAX_ITER) {
- h /= 2;
- I1 = I2;
- I2 = trapezoidal(a, b, 4 * (iter + 1), func);
- delta = fabs(I2 - I1) / 3.0;
- iter++;
- }
- return I2;
- }
- int main() {
- double a, b, epsilon;
- printf("Enter the limits of integration (a, b): ");
- scanf("%lf%lf", &a, &b);
- printf("Enter the desired accuracy (epsilon): ");
- scanf("%lf", &epsilon);
- double result = runge(a, b, epsilon, f);
- printf("Approximate value of the integral: %.6lf\n", result);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement