Skip to content

Instantly share code, notes, and snippets.

@Ben1980
Last active April 15, 2019 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ben1980/d0ff3f324bf0aec98eb2d641ce2faeed to your computer and use it in GitHub Desktop.
Save Ben1980/d0ff3f324bf0aec98eb2d641ce2faeed to your computer and use it in GitHub Desktop.
double simpsonIntegral(double a, double b, int n, const std::function<double (double)> &f) {
const double width = (b-a)/n;
double simpson_integral = 0;
for(int step = 0; step < n; step++) {
const double x1 = a + step*width;
const double x2 = a + (step+1)*width;
simpson_integral += (x2-x1)/6.0*(f(x1) + 4.0*f(0.5*(x1+x2)) + f(x2));
}
return simpson_integral;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment