I made a function in Python which calculates a definite integral according to the Trapezoidal rule: Trapezoidal rule formula
That's the code:
from math import ceil
def Trapez_rule(f, a, b, n):
'''Calculates an estimation of a definite integral of a function f(x), between the boundries a, b, by dividing the area to n equal areas'''
sum = (f(a) + f(b)) / 2
for i in range(ceil((b * n))):
sum += f(a + i / n)
sum *= (b - a) / n
return sum
The answer it gives is 10 times higher that it should have returned. I can't find the source of the problem.
Assume:
These lines are the problem:
igo from 0 to 99when i = 99 then:
You use n false look at the post solution below, so this should work:
def Trapez_rule(f, a, b, n): h = (b-a) / float(n) sum = (f(a) + f(b)) / 2.w for i in range(1,n-1): sum += f(a + i * h) sum *= h return sum