Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. double simpsonIntegral(double a, double b, int n, const std::function<double (double)> &f) {
  2. const double width = (b-a)/n;
  3.  
  4. double simpson_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. simpson_integral += (x2-x1)/6.0*(f(x1) + 4.0*f(0.5*(x1+x2)) + f(x2));
  10. }
  11.  
  12. return simpson_integral;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement