Error formula for Composite Trapezoidal Rule

My textbook gives me the error term for the Composite Trapezoidal Rule as this:

$-\frac{b-a}{12}h^2f''(\mu)$, where $\mu \in(a,b)$ and $f \in C^2 [a,b]$

I am using MatLab to produce approximations with the Composite Trapezoidal Rule for $\int_0^{0.99} \frac{1}{\sqrt{1-x^2}}{\rm d}x$ with the intervals $h = 0.01, 0.005, 0.0025, 0.00125, 0.000625$

Below is my table of the approximations by my code and the absolute error for each interval:

....h............S(h)...........abs. err...
0.010000    1.432052842622   0.002795989152
0.005000    1.429980957924   0.000724104453
0.002500    1.429439827337   0.000182973867
0.001250    1.429302728001   0.000045874530
0.000625    1.429268330467   0.000011476997

Evaluating the error with the error formula, however, gives me a very different number than what my code is spitting out. For example, evaluating the error term for $h = 0.01, a = 0, b = 0.99$, I end up with $0.437161725$. Should my approximation of the error be that off? Am I not using the error term properly?

• How did you compute the error? If you go into the details, the actual error is more $\frac{h^2}{12}\int_a^b|f''(s)|ds$ than $\frac{h^2}{12}(b-a)\max_{s\in[a,b]}|f(s)|$ which makes a difference when there are large changes in magnitude as in this example. – LutzL May 18 '18 at 5:30
• I computed the error using the latter method. – user-2147482428 May 18 '18 at 5:39
• How do you do the trapezoidal summation when the interval length is not a multiple of $h$? Do you cut the last interval short? – LutzL May 18 '18 at 6:18

$${\rm err} = -\frac{b-a}{12}h^2f''(\mu) \tag{1}$$
The meaning is: there is a point $\mu \in (a,b)$ such that the error is given by this expression. To show this is true I calculate $S(h)$ for various values of $h$ and the absolute error $\epsilon$. I then find the value of $\mu$ guaranteed by Eq. (1), that is, the value of $\mu$ such that ${\rm err} = \epsilon$ Note that the error is in the derivation the sum over the error of all the sub-intervals. For each of these intervals you get $$\int_{x_k}^{x_{k+1}}f(s)\,ds-(x_{k+1}-x_k)\frac{f(x_{k+1})+f(x_k)}2=\frac{(x_{k+1}-x_k)^3}{12}f''(μ_k)$$ With constant length of the sub-intervals, $x_{k+1}-x_k=h$, the error is thus a Riemann sum for $$\frac{h^2}{12}\int_a^bf''(s)\,ds=\frac{h^2}{12}(f'(b)-f'(a))$$ For $f(x)=(1-x^2)^{-1/2}$ we get $f'(x)=x(1-x^2)^{-3/2}$ so that with $[a,b]=[0, 0.99]$ the constant in the error formula is $C=(f'(b)-f'(a))/12=29.38829..$. The experimental table extended to include estimates of this second order error constant is \begin{array}{r|lllllccc} n&h&S(h)&E(h)=S(h)-I&E(h)/h^2&E(h)/h^2-C\\\hline 100 &0.0099 &1.4319994706 & 0.00274261713177 &27.983033688 &-1.40525747436 \\ 200 &0.00495 &1.42996674039 & 0.000709886919313 &28.9720199699 & -0.416271192477 \\ 400 &0.002475 &1.42943619958 & 0.000179346106281 &29.2780093918 & -0.110281770601 \\ 800 &0.0012375 &1.42930181596 & 4.49624937289e{-}05 &29.3602652653 & -0.0280258971486 \\ 1600 &0.00061875 &1.42926810213 & 1.1248659324e{-}05 &29.3812548408 & -0.00703632162522 \\ 3200 &0.000309375 &1.42925966614 & 2.81266974667e{-}06 &29.386530156 & -0.00176100644524 \\ \end{array} which shows that indeed the error behaves like $E(h)=29.38829\cdot h^2+O(h^4)$.