Advertisement
Aodai

Trapezoidal

Apr 23rd, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. float f(float x)
  6. {
  7.     return 1 / (x+1);
  8. }
  9.  
  10. int main()
  11. {
  12.     float a, b, epsilon = powf(10, -5), II, IO, h, s;
  13.     int n = 1, counter=1;
  14.     printf("a=>");
  15.     scanf("%f", &a);
  16.     printf("b=>");
  17.     scanf("%f", &b);
  18.     //printf("epsilon=>");
  19.     //scanf("%f", &epsilon);
  20.     II = (f(a) + f(b)) * (b - a) / 2;
  21.     do {
  22.         n *= 2;
  23.         h = (b - a) / n;
  24.         IO = II;
  25.         s = 0;
  26.         for (int i = 1; i <= n - 1; i++)
  27.             s += f(a + i * h);
  28.         II = (f(a) + 2 * s + f(b)) * h / 2;
  29.         counter++;
  30.     } while (fabs(II - IO) >= epsilon);
  31.     printf("The value of the integral part with precision %f is %f in %d steps.\n", epsilon, II, counter);
  32.     system("pause");
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement