Created
May 26, 2020 20:15
-
-
Save IlievskiV/a0e8f3e8c45ea9a8e2e6a89a2d78b425 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def calculate_integral(f, a, b, n): | |
| '''Calculates the integral based on the composite trapezoidal rule | |
| relying on the Riemann Sums. | |
| :param function f: the integrand function | |
| :param int a: lower bound of the integral | |
| :param int b: upper bound of theintergal | |
| :param int n: number of trapezoids of equal width | |
| :return float: the integral of the function f between a and b | |
| ''' | |
| w = (b - a)/n | |
| result = 0.5*f(a) + sum([f(a + i*w) for i in range(1, n)]) + 0.5*f(b) | |
| result *= w | |
| return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment