Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- double f(double x);
- double f(double x) {
- return sqrt(1 + pow(x, 5));
- }
- double composite_simpson(double a, double b);
- double composite_simpson(double a, double b) {
- return (b - a) * (f(a) + 4 * f(0.5 * (a + b)) + f(b)) / 6;
- }
- double adaptive_simpson(double a, double b, double eps);
- double adaptive_simpson(double a, double b, double eps) {
- double I;
- double I2;
- if (fabs(b - a) < eps)
- return 0;
- I = composite_simpson(a, b);
- I2 = composite_simpson(a, 0.5 * (a + b)) + composite_simpson(0.5 * (a + b), b);
- // printf("Int [%lf, %lf] I = %lf I2 = %lf\n", a, b, I, I2);
- if (fabs(I - I2) < eps * (b - a))
- return I2;
- return adaptive_simpson(a, 0.5 * (a + b), eps) + adaptive_simpson(0.5 * (a + b), b, eps);
- }
- double func(double x, double alpha, double eps);
- double func(double x, double alpha, double eps) {
- return adaptive_simpson(-x, x, eps) - (3-alpha/3);
- }
- double result(double alpha, double eps);
- double result(double alpha, double eps) {
- double a, b, x;
- if ((3-alpha/3) >= 0) {
- a = 0;
- b = 1;
- }
- else {
- a = -1;
- b = 0;
- }
- if (func(a, alpha, eps) * func(b, alpha, eps) > 0)
- return NAN;
- while (b - a > eps) {
- x = (a + b)/2;
- // printf("[%lf, %lf] f(a)=%lf f(b)=%lf\n", a, b, func(a, alpha, eps), func(b, alpha, eps));
- if (func(x, alpha, eps) * func(b, alpha, eps) < 0) {
- a = x;
- }
- else {
- b = x;
- }
- }
- return a;
- }
- int main(void) {
- double alpha;
- double eps;
- double x = 0;
- printf ("enter parametr: \n");
- if (scanf("%lf", &alpha) != 1) {
- printf ("wrong znachenie!\n");
- return -1;
- }
- printf ("enter epsilon:\n");
- if (scanf("%lf", &eps) != 1) {
- printf ("wrong znach!\n");
- return -1;
- }
- x = result(alpha, eps);
- printf("x = %lf\n", x);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement