In the following code I have implemented the trapezoidal rule. It runs perfectly. I am wondering if I was to code the composite trapezoidal rule would the code be the same? if not how do I change my code? thanks!
import math
def trapezoidal(f, a, b, n):
h = float(b - a) / n
s = 0.0
s += f(a)/2.0
for i in range(1, n):
s += f(a + i*h)
s += f(b)/2.0
return s * h
print(trapezoidal(lambda x:x**2, 5, 10, 100))
Comments
Post a Comment