def trapez(f, a, b, n):
    """
    Computes and returns a trapezoidal approximate integral of f.

    f -- function
    a -- lower boundary
    b -- upper boundary
    n -- number of sections

    returns approximate integral using the trapezoidal method
    """
    h = (b-a)/float(n)
    sum_fx = 0.0
    for i in range(n-1):
        xi = a + (i+1) * h
        sum_fx += f(xi)
    return h * (f(a) + f(b) + 2.0 * sum_fx) / 2.0