The trapezoidal rule is an approximation for the area under a curve that is derived from inscribing several trapezoids under a curve. The area of the ith trapezoid is 21h(bi+bi+1)=21(xi+1−xi)(f(xi)+f(xi+1)). Therefore, we can approximate
∫abf(x)dx≈21i=0∑n−1(xi+1−xi)(f(xi)+f(xi+1))
When all the trapezoids have the same width, or xi+1−xi=nb−a, it is
∫abf(x)dx≈2nb−a[f(x0)+2f(x1)+2f(x2)+⋯+2f(xn−1)+f(xn)]
where x0=a, x1=a+nb−a, x2=a+2nb−a, x3=a+3nb−a, …, xn−1=a+(n−1)nb−a, and xn=b.