Solution (source code)

= Solution

Both rules use a uniform partition with $\Delta x=(b-a)/n$. An original implementation in pseudocode is:

``
function trapezoid(f, a, b, n):
    dx = (b-a)/n
    total = (f(a)+f(b))/2
    for j in 1,...,n-1:
        total += f(a+j*dx)
    return dx*total

function midpoint(f, a, b, n):
    dx = (b-a)/n
    total = 0
    for j in 0,...,n-1:
        total += f(a+(j+1/2)*dx)
    return dx*total
``

The <trapezoidal rule> gives each endpoint half weight; the <composite midpoint rule> uses one interior point per subinterval. Both converge for a continuous <integrand> on the closed interval.

\b[The second equality in the printed trapezoidal hint is missing a factor $1/2$.] The average of the left and right rectangle sums is required. For $f=1$, the uncorrected equality would return $2(b-a)$ instead of $b-a$.