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*totalThe 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.
Articles by others on the same topic
There are currently no matching articles.