Last active
April 15, 2019 05:15
-
-
Save Ben1980/c37f29ea5ed4ece71eb9ca7e7cc78574 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double trapezoidalIntegral(double a, double b, int n, const std::function<double (double)> &f) { | |
const double width = (b-a)/n; | |
double trapezoidal_integral = 0; | |
for(int step = 0; step < n; step++) { | |
const double x1 = a + step*width; | |
const double x2 = a + (step+1)*width; | |
trapezoidal_integral += 0.5*(x2-x1)*(f(x1) + f(x2)); | |
} | |
return trapezoidal_integral; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment