Advertisement
2607

Untitled

Mar 19th, 2020
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4.  
  5. double F(double x)
  6. {
  7.     return x - sin(x);
  8. }
  9. double integral(double a, double b)
  10. {
  11.     return (pow(b,2)/2 + cos(b)) - (pow(a,2)/2 + cos(a));
  12. }
  13.  
  14.  
  15. double solve(double a, double b, int n)
  16. {
  17.     int i;
  18.     double dx, sum = 0, lastF, nextF;
  19.     dx = (b-a)/n;
  20.     lastF = F(a);
  21.     for(i = 1; i <= n; i++)
  22.     {
  23.         nextF = F(a + i*dx);
  24.         sum += ((lastF + nextF)/2)*dx;
  25.         lastF = nextF;
  26.  
  27.     }
  28.     return sum;
  29. }
  30.  
  31. int main() {
  32.     int n;
  33.     double a, b, result;
  34.    
  35.     printf("Enter number of segments: ");
  36.     scanf("%d", &n);
  37.     printf("\n");
  38.     printf("Enter range (0 9.81): ");
  39.     scanf("%lf %lf", &a, &b);
  40.     printf("\n");
  41.  
  42.     printf("n = %d a = %lf b = %lf\n\n", n, a, b);
  43.  
  44.     result = solve(a, b, n);
  45.     printf("result            %lf\n", result);
  46.     printf("real integral %lf\n", integral(a, b));
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement