Advertisement
am1x

qtrap1m001.cxx

Apr 10th, 2024 (edited)
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | Fixit | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define N 10000
  5. #define M 100000
  6.  
  7. typedef double (*dfunc)(double);
  8.  
  9.  
  10.  
  11. static double quad_trap(dfunc f, double a, double b, unsigned int n)
  12. {
  13.     double h = (b - a) / n;
  14.     double res = 0.5 * (f(a) + f(b));
  15.     for (unsigned int i = 1; i < n; i++)
  16.         res += f(a + i * h);
  17.  
  18.     return res * h;
  19. }
  20.  
  21. static double f(double x)
  22. {
  23.     return exp(x) - 10.0;
  24. }
  25.  
  26.  
  27. int main()
  28. {
  29.     double s = 0;
  30.     for (int i = 0; i < M; i++) {
  31.         s = quad_trap(f, -1.0, 1.0, N);
  32.     }
  33.     printf("%u %u %.18f \n", N, M, s);
  34.     return 0;
  35. }
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement