Skip to content

Instantly share code, notes, and snippets.

@IlievskiV
Created October 2, 2020 10:48
Show Gist options
  • Save IlievskiV/8004a94dd508d723678d8da57002f157 to your computer and use it in GitHub Desktop.
Save IlievskiV/8004a94dd508d723678d8da57002f157 to your computer and use it in GitHub Desktop.
Implementation of the Riemann-Stieltjes Integration in Python
def derivative(f, a, h=0.01):
'''Approximates the derivative of the function f in a
:param function f: function to differentiate
:param float a: the point of differentiation
:param float h: step size
:return float: the derivative of f in a
'''
return (f(a + h) - f(a - h))/(2*h)
def stieltjes_integral(f, g, a, b, n):
'''Calculates the Riemann-Stieltjes integral based on the composite trapezoidal rule
relying on the Riemann Sums.
:param function f: integrand function
:param function f: integrator 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
'''
eps = 1e-9
h = (b - a)/(n + eps) # width of the rectangle
dg = lambda x: derivative(g, x, h=1e-8) # derivative of the integrator function
result = 0.5*f(a)*dg(a) + sum([f(a + i*h)*dg(a + i*h) for i in range(1, n)]) + 0.5*f(b)*dg(b)
result *= h
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment