TITLE: Function for which trapezoidal rule outperforms midpoint rule for every $n$
QUESTION [11 upvotes]: Is there a continuous elementary function $f:[0,1]\to [0,\infty)$ such that for every $n$ the trapezoidal approximation to $\int_{0}^{1}f(x)\,dx$ with $n$ trapezoids is strictly better than the midpoint approximation with $n$ rectangles?
The point of this questions is that even though the midpoint approximation to an integral is generally better than the trapezoidal approximation, there is, for each $n$, a continuous function $f:[0,1]\to \mathbb{R}$ such that the trapezoidal approximation to $\int_{0}^{1}f(x)\,dx$ with $n$ trapezoids is better than the midpoint approximation with $n$ rectangles. See here for an example.
I added the restriction that $f$ be elementary so I can talk about the answer with my calculus students. I added the restriction that $f$ be non-negative for simplicity.

REPLY [5 votes]: Consider a function of the form
$$ f(x) = \sum_{j=1}^\infty c_j \cos(2  \pi j x) $$
Then the errors $ME(n)$ and $TE(n)$ in midpoint and trapezoid rules are
$$\eqalign{TE(n) &= \sum_{k=1}^\infty c_{kn} \cr
           ME(n) &= 2 TE(2n) - TE(n) = \sum_{k=1}^\infty (-1)^{k} c_{kn} \cr}$$
For example, let $c_j = (-1)^{d(j)} 2^{-j}$ where $d(j)$ is the $2$-adic order of $j$, i.e. $d(j) = d$ if $2^d$ divides $j$ but $2^{d+1}$ does not.
Then if $d(n) = d$ we have
$$ \eqalign{TE(n) &= (-1)^d \left(\sum_{k\; odd} 2^{-kn} - \sum_{k\; odd} 2^{-2kn} + \sum_{k \; odd} 2^{-4kn} + \ldots\right)\cr
&= (-1)^d \left( \dfrac{2^{-n}}{1-4^{-n}} - \dfrac{2^{-2n}}{1-4^{-2n}} + \dfrac{2^{-4n}}{1-4^{-4n}} - \ldots \right)\cr } $$ 
while
$$ ME(n) = (-1)^{d+1} \left( \dfrac{2^{-n}}{1-4^{-n}} + \dfrac{2^{-2n}}{1-4^{-2n}} - \dfrac{2^{-4n}}{1-4^{-4n}} + \ldots \right) $$
so $|ME(n)| > |TE(n)|$.