Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 10/4/1
- // E = 0.001
- // f1 = 1 + 4 / (x^2+ 1)
- // f2 = x^3
- // f3 = 2^(−x)
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- double eps = 0.001;
- double eps1 = 0.0001;
- double eps2 = 0.0001;
- double f1(double x)
- {
- return 1 + 4 / (x * x + 1);
- //return 1/x;
- }
- double f1_1(double x)
- {
- return - 8 * x / ((x * x + 1) * (x * x + 1));
- //return -1/(x*x);
- }
- double f1_2(double x)
- {
- return 32 * powf(x, 2) / (powf((powf(x, 2) + 1), 3)) - 8 / (powf((x * x + 1), 2));
- }
- double f2(double x)
- {
- return x * x * x;
- }
- double f2_1(double x)
- {
- return 3 * x * x;
- }
- double f2_2(double x)
- {
- return 6 * x;
- }
- double f3(double x)
- {
- return pow(2, (-x));
- }
- double f3_1(double x)
- {
- return -log(2) * pow(2, (-x));
- }
- double f3_2(double x)
- {
- return log(2) * log(2) * pow(2, (-x));
- }
- double root(double (*f)(double), double (*g)(double), double (*f1)(double), double (*g1)(double), double a, double b, double eps1)
- {
- double a1 = 0, b1 = 0;
- while (fabs(a - b) > eps1)
- {
- if (((f(a) - g(a)) < 0) && ((f((a + b) / 2) - g((a + b) / 2)) > ((f(a) - g(a) + f(b) - g(b)) / 2)))
- {
- a1 = a - (b - a) * (f(a) - g(a)) / (f(b) - g(b) - f(a) + g(a));
- b1 = b - (f(b) - g(b)) / (f1(b) - g1(b));
- a = a1;
- b = b1;
- }
- else
- {
- b1 = b - (a - b) * (f(b) - g(b)) / (f(a) - g(a) - f(b) + g(b));
- a1 = a - (f(a) - g(a)) / (f1(a) - g1(a));
- a = a1;
- b = b1;
- }
- }
- return b;
- }
- double integral(double (*f)(double), double a, double b, double eps2)
- {
- double res = 100, h, n = 20, In = 0, I2n = 10;
- h = (b - a) / n;
- for (int i = 0; i < n; i++)
- {
- res += f(a + (i + 0.5) * h);
- }
- res *= h;
- In = res;
- n *= 2;
- h = (b - a) / n;
- for (int i = 0; i < n; i++)
- {
- res += f(a + (i + 0.5) * h);
- }
- res *= h;
- I2n = res;
- double p = 1/3;
- while (p * fabs(In - I2n) > eps2)
- {
- In = I2n;
- n *= 2;
- res = 0;
- h = (b - a) / n;
- for (int i = 0; i < n; i++)
- {
- res += f(a + (i + 0.5) * h);
- }
- res *= h;
- n *= 2;
- I2n = res;
- }
- return I2n;
- }
- int main(void)
- {
- double a = -10, b = 10;
- double x_1_2 = root(f1, f2, f1_1, f2_1, a, b, eps1);
- a = -10;
- b = 10;
- double x_2_3 = root(f2, f3, f2_1, f3_1, a, b, eps1);
- a = -10;
- b = -0.5;
- double x_1_3 = root(f1, f3, f1_1, f3_1, a, b, eps1);
- printf("%lf %lf %lf\n", x_1_2, x_2_3, x_1_3);
- double res = 0, first, sec, third;
- first = integral(f1, x_1_3, x_1_2, eps2);
- sec = integral(f3, x_1_3, x_2_3, eps2);
- third = integral(f2, x_2_3, x_1_2, eps2);
- res = first - sec - third;
- printf("%lf\n", res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement