Python-Math-Algorithms/integral_approximation/__init__.py

32 lines
628 B
Python
Raw Permalink Normal View History

2023-05-22 16:16:45 +00:00
def integral_approximation(function, divisions, interval):
delta = (interval[1] - interval[0]) / divisions
maximum = 0.0
minimum = 0.0
x0 = interval[0]
for i in range(0, divisions):
x1 = x0 + delta
y0 = function(x0) * delta
y1 = function(x1) * delta
maximum += max(y0, y1)
minimum += min(y0, y1)
x0 = x1
average = (maximum + minimum) / 2
print("maximum: %s minimum: %s average: %s" % (maximum, minimum, average))
def f(x):
return (x - 4) * (x + 8) * (x - 50) * (x - 37) * 0.0001
def test():
integral_approximation(f, 1200, [3, 4])