Created
April 3, 2023 15:49
-
-
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
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
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