Skip to content

Instantly share code, notes, and snippets.

@IlievskiV
Created May 26, 2020 20:15
Show Gist options
  • Save IlievskiV/a0e8f3e8c45ea9a8e2e6a89a2d78b425 to your computer and use it in GitHub Desktop.
Save IlievskiV/a0e8f3e8c45ea9a8e2e6a89a2d78b425 to your computer and use it in GitHub Desktop.
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