Advertisement
Dari_

Untitled

May 7th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.91 KB | None | 0 0
  1. // 10/4/1
  2. // E = 0.001
  3. // f1 = 1 + 4 / (x^2+ 1)
  4. // f2 = x^3
  5. // f3 = 2^(−x)
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10.  
  11.  
  12. double eps = 0.001;
  13. double  eps1 = 0.0001;
  14. double  eps2 = 0.0001;
  15.  
  16.  
  17. double f1(double x)
  18. {
  19.     return 1 + 4 / (x * x + 1);
  20.     //return 1/x;
  21. }
  22.  
  23. double f1_1(double x)
  24. {
  25.     return - 8 * x / ((x * x + 1) * (x * x + 1));
  26.     //return -1/(x*x);
  27. }
  28.  
  29. double f1_2(double x)
  30. {
  31.     return 32 * powf(x, 2) / (powf((powf(x, 2) + 1), 3)) - 8 / (powf((x * x + 1), 2));
  32. }
  33.  
  34.  
  35.  
  36.  
  37. double f2(double x)
  38. {
  39.     return x * x * x;
  40. }
  41.  
  42. double f2_1(double x)
  43. {
  44.     return 3 * x * x;
  45. }
  46.  
  47. double f2_2(double x)
  48. {
  49.     return 6 * x;
  50. }
  51.  
  52.  
  53.  
  54.  
  55. double f3(double x)
  56. {
  57.     return pow(2, (-x));
  58. }
  59.  
  60. double f3_1(double x)
  61. {
  62.     return -log(2) * pow(2, (-x));
  63. }
  64.  
  65. double f3_2(double x)
  66. {
  67.     return log(2) * log(2) * pow(2, (-x));
  68. }
  69.  
  70.  
  71.  
  72. double root(double (*f)(double), double (*g)(double), double (*f1)(double), double (*g1)(double), double a, double b, double eps1)
  73. {
  74.     double a1 = 0, b1 = 0;
  75.     while (fabs(a - b) > eps1)
  76.     {
  77.         if (((f(a) - g(a)) < 0) && ((f((a + b) / 2) - g((a + b) / 2)) > ((f(a) - g(a) + f(b) - g(b)) / 2)))
  78.         {
  79.         a1 = a - (b - a) * (f(a) - g(a)) / (f(b) - g(b) - f(a) + g(a));
  80.         b1 = b - (f(b) - g(b)) / (f1(b) - g1(b));
  81.  
  82.         a = a1;
  83.         b = b1;
  84.         }
  85.         else
  86.         {
  87.         b1 = b - (a - b) * (f(b) - g(b)) / (f(a) - g(a) - f(b) + g(b));
  88.         a1 = a - (f(a) - g(a)) / (f1(a) - g1(a));
  89.  
  90.         a = a1;
  91.         b = b1;
  92.         }
  93.     }
  94.     return b;
  95. }
  96.  
  97.  
  98.  
  99. double integral(double (*f)(double), double a, double b, double eps2)
  100. {
  101.     double res = 100, h, n = 20, In = 0, I2n = 10;
  102.     h = (b - a) / n;
  103.     for (int i = 0; i < n; i++)
  104.     {
  105.         res += f(a + (i + 0.5) * h);
  106.     }
  107.     res *= h;
  108.     In = res;
  109.     n *= 2;
  110.  
  111.     h = (b - a) / n;
  112.     for (int i = 0; i < n; i++)
  113.     {
  114.         res += f(a + (i + 0.5) * h);
  115.     }
  116.     res *= h;
  117.     I2n = res;
  118.  
  119.     double p = 1/3;
  120.  
  121.     while (p * fabs(In - I2n) > eps2)
  122.     {
  123.         In = I2n;
  124.         n *= 2;
  125.         res = 0;
  126.         h = (b - a) / n;
  127.         for (int i = 0; i < n; i++)
  128.         {
  129.             res += f(a + (i + 0.5) * h);
  130.         }
  131.         res *= h;
  132.         n *= 2;
  133.         I2n = res;
  134.     }
  135.     return I2n;
  136. }
  137.  
  138.  
  139.  
  140. int main(void)
  141. {
  142.     double a = -10, b = 10;
  143.     double x_1_2 = root(f1, f2, f1_1, f2_1, a, b, eps1);
  144.  
  145.     a = -10;
  146.     b = 10;
  147.     double x_2_3 = root(f2, f3, f2_1, f3_1, a, b, eps1);
  148.  
  149.     a = -10;
  150.     b = -0.5;
  151.     double x_1_3 = root(f1, f3, f1_1, f3_1, a, b, eps1);
  152.  
  153.     printf("%lf    %lf    %lf\n", x_1_2, x_2_3, x_1_3);
  154.  
  155.     double res = 0, first, sec, third;
  156.     first = integral(f1, x_1_3, x_1_2, eps2);
  157.     sec = integral(f3, x_1_3, x_2_3, eps2);
  158.     third = integral(f2, x_2_3, x_1_2, eps2);
  159.  
  160.     res = first - sec - third;
  161.  
  162.     printf("%lf\n", res);
  163.  
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement