Math 300: Mathematical Computing

The Midpoint Rule

One alternative to the composite trapezoidal rule for integrating
functions is to use the midpoint rule. The midpoint rule is simply
a Riemann sum in which the points where the function is evaluated
are the midpoints of subintervals of the interval of integration.
Specifically, consider the integral
\[\int_a^b f(x)\,dx\]
where \(f\) is twice continuously differentiable.
Let \(a=x_0\lt x_1\lt\dots\lt x_n=b\) be an
equally-spaced partition of \([a,b]\),
and let \(z_i=(x_i+x_{i-1})/2\) for \(i=1,2,\dots,n\) denote
the midpoints of the subintervals defined by the \(x_i\) values.
When \(h=(b-a)/n\) is the uniform width of the subintervals,
the midpoint rule is
\[\int_a^b f(x)\,dx =
h\sum_{i=1}^n f(z_i) + O(h^2).
\]
Write a Matlab function that takes four arguments: an integrand function,
the lower and upper limits of integration, and the value of \(n\),
which returns the value of the midpoint approximation to the integral.