Both rules use a uniform partition with . 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.
The second equality in the printed trapezoidal hint is missing a factor . The average of the left and right rectangle sums is required. For , the uncorrected equality would return instead of .

Articles by others on the same topic (0)

There are currently no matching articles.