Skip to content

Instantly share code, notes, and snippets.

@BlueBeret
Created April 3, 2023 15:49
Show Gist options
  • Save BlueBeret/0f0a19685c9798eb031fe16c755ccb57 to your computer and use it in GitHub Desktop.
Save BlueBeret/0f0a19685c9798eb031fe16c755ccb57 to your computer and use it in GitHub Desktop.
A simple implementation of a function to calculate definite integral with Rieman sum method
from math import exp
def fx(x):
return (x+7) * exp(x)
def jumlahanRiemanKiri(function, a, b, n = 10e7):
"""
Fungsi untuk menghitung integral tentu menggunakan metode jumlah rieman kiri
Parameters
----------
function: fungsi yang ingin dihitung nilai integral tentunya
a = batas bawah
b = batas atas
n = banyak partisi
"""
assert b > a
alas = (b - a) / n
result = 0
x = a
while (x < b):
x += alas
n-=1
tinggi = function(x)
result += alas*tinggi
return result
if __name__ == "__main__":
res = jumlahanRiemanKiri(fx, 3,7)
print(res)
# output : 14075.462023129421
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment