Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Trapezoidal rule to composite trapezoidal rule in Python

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