Exercise: trapezoid and midpoint

Comparing composite trapezoid, simple midpoint and composite midpoint, with absolute and relative errors.

Comparison on a short integral

ExampleIntegral of x^3 on [0,1]

Compute I=01x3dxI=\int_0^1 x^3\,dx. Compare composite trapezoid with n=2n=2, simple midpoint and composite midpoint with n=2n=2.

  1. The exact value is:

    I=01x3dx=[x44]01=14I=\int_0^1x^3\,dx=\left[\frac{x^4}{4}\right]_0^1=\frac14
  2. For composite trapezoid with n=2n=2, h=1/2h=1/2 and the nodes are 0,1/2,10,1/2,1:

    T2=14[f(0)+2f ⁣(12)+f(1)]=14[0+218+1]=516\begin{aligned}T_2&=\frac14\left[f(0)+2f\!\left(\frac12\right)+f(1)\right]\\&=\frac14\left[0+2\cdot\frac18+1\right]=\frac{5}{16}\end{aligned}
  3. For simple midpoint, the centre of the full interval is m=1/2m=1/2:

    M1=(10)f ⁣(12)=18M_1=(1-0)f\!\left(\frac12\right)=\frac18
  4. For composite midpoint with n=2n=2, the centres are 1/41/4 and 3/43/4:

    M2=12[f ⁣(14)+f ⁣(34)]=12(164+2764)=732\begin{aligned}M_2&=\frac12\left[f\!\left(\frac14\right)+f\!\left(\frac34\right)\right]\\&=\frac12\left(\frac{1}{64}+\frac{27}{64}\right)=\frac{7}{32}\end{aligned}
  5. The absolute and relative errors are:

    meˊtodoIQIQ/IT21/1625%M11/850%M21/3212.5%\begin{array}{c|c|c} \text{método} & |I-Q| & |I-Q|/I \\ \hline T_2 & 1/16 & 25\% \\ M_1 & 1/8 & 50\% \\ M_2 & 1/32 & 12.5\% \end{array}

In this example, composite midpoint with two subintervals is the most accurate of the three. The improvement comes from using two centres instead of one rectangle for all of [0,1].

Using only interior points

ExampleMidpoint with n=4 and n=8

Compute I=∫_0^{π/2} sin(x)e^{-x} dx with composite midpoint using n=4 and n=8.

  1. The exact value is the same as in the previous exercise:

    I=0.396060211824619I=0.396060211824619
  2. The midpoint approximations are:

    M4=0.409710138362011,M8=0.401008515076530M_4=0.409710138362011,\qquad M_8=0.401008515076530
  3. Their absolute errors are:

    E4=0.013649926537392,E8=0.004948303251911|E_4|=0.013649926537392,\qquad |E_8|=0.004948303251911

The error decreases when the number of subintervals increases, but here it remains larger than Simpson's for the same refinement.