Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.39 KB | None | 0 0
  1. double trapezoidalIntegral(double a, double b, int n, const std::function<double (double)> &f) {
  2. const double width = (b-a)/n;
  3.  
  4. double trapezoidal_integral = 0;
  5. for(int step = 0; step < n; step++) {
  6. const double x1 = a + step*width;
  7. const double x2 = a + (step+1)*width;
  8.  
  9. trapezoidal_integral += 0.5*(x2-x1)*(f(x1) + f(x2));
  10. }
  11.  
  12. return trapezoidal_integral;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement