Section3.4Integration: Midpoint and Simpson’s Rules
Subsection3.4.1Midpoint rule
If we use the endpoints of the subintervals to approximate the integral, we run the risk that the values at the endpoints do not accurately represent the average value of the function on the subinterval. A point which is much more likely to be close to the average would be the midpoint of each subinterval. Using the midpoint in the sum is called the midpoint rule. On the \(i\)-th interval \([x_{i-1},x_{i}]\) we will call the midpoint \(\bar{x}_{i}\text{,}\) i.e.
While the midpoint method is obviously better than \(L_{n}\) or \(R_{n}\text{,}\) it is not obvious that it is actually better than the trapezoid method \(T_{n}\text{,}\) but it is.
Figure3.4.1.Comparing the trapezoid and midpoint method on a single subinterval. The function is concave up, in which case \(T_{n}\) is too high, while \(M_{n}\) is too low.
If \(f\) is not linear on a subinterval, then it can be seen that the errors for the midpoint and trapezoid rules behave in a very predictable way, they have opposite sign. For example, if the function is concave up then \(T_{n}\) will be too high, while \(M_{n}\) will be too low. Thus it makes sense that a better estimate would be to average \(T_{n}\) and \(M_{n}\text{.}\) However, in this case we can do better than a simple average. The error will be minimized if we use a weighted average. To find the proper weight we take advantage of the fact that for a quadratic function the errors \(EM_{n}\) and \(ET_{n}\) are exactly related by
Notice that we write the subscript as \(2n\text{.}\) That is because we usually think of \(2n\) subintervals in the approximation; the \(n\) subintervals of the trapezoid are further subdivided by the midpoints. We can then number all the points using integers. If we number them this way we notice that the number of subintervals must be an even number.
Note that if we are presented with data \(\{x_{i},y_{i}\}\) where the \(x_{i}\) points are evenly spaced with \(x_{i+1}- x_{i} = \Delta x\text{,}\) it is easy to apply Simpson’s method:
Notice the pattern of the coefficients. The following program will produce these coefficients for \(n\) intervals, if \(n\) is an even number. Try it for \(n = 6,7,100\text{.}\)Program3.4.2.mysimpweights
function w = mysimpweights(n)
% Computes the weights for Simpson's rule
% Input: n -- the number of intervals, must be even
% Output: w -- a (column) vector with the weights, length n+1
if rem(n,2) ~= 0
error('n must be even for Simpsons rule')
end
w = 2*ones(n+1,1); % column vector, starts all 2's
w(1) = 1; w(n+1) = 1; % set ends to 1's
w(2:2:n)=4; % set even # entries to 4.
end
Simpson’s rule is incredibly accurate. We will consider just how accurate in the next section. The one drawback is that the points used must either be evenly spaced, or at least the odd number points must lie exactly at the midpoint between the even numbered points. In applications where you can choose the spacing, this is not a problem. In applications such as experimental or simulated data, you might not have control over the spacing and then you cannot use Simpson’s rule.
The trapezoid, midpoint, and Simpson’s rules are all approximations. As with any approximation, before you can safely use it, you must know how good (or bad) the approximation might be. For these methods there are formulas that give upper bounds on the error. In other words, the worst case errors for the methods. These bounds are given by the following statements:
Suppose \(f''\) is continuous on \([a,b]\text{.}\) Let \(K_{2} = \max_{x \in [a,b]}|f''(x)|\text{.}\) Then the errors \(ET_{n}\) and \(EM_{n}\) of the Trapezoid and Midpoint rules, respectively, applied to \(\int_{a}^{b} f \, dx\) satisfy
Suppose \(f^{(4)}\) is continuous on \([a,b]\text{.}\) Let \(K_{4} = \max_{x \in [a,b]}|f^{(4)}(x)|\text{.}\) Then the error \(ES_{n}\) of Simpson’s rule applied to \(\int_{a}^{b} f \, dx\) satisfies
The most important thing in these error bounds is the dependence on \(h\text{.}\) To emphasize this dependence, we sometimes use the order notation\(O(\;)\text{.}\) The trapezoid and midpoint method errors are \(O(h^{2})\text{,}\) so the methods have order 2. The Simpson’s rule error is \(O(h^{4})\text{,}\) so it has order 4. If \(h\) is just moderately small, then there is a huge advantage with Simpson’s method.
In MATLAB there is a built-in command for definite integrals: integral(f,a,b) where the f is a function and a and b are the endpoints. The command uses “adaptive Simpson quadrature”, a form of Simpson’s rule that checks its own accuracy and adjusts the grid size where needed. Here is an example of its usage:
>> f = @(x) x.^(1/3).*sin(x.^3)
>> I = integral(f,0,1)
Using formulas (3.4.1) and (3.4.2), for the integral \(\int_{1}^{2} \sqrt{x}\, dx\) calculate \(M_{4}\) and \(S_{4}\) (by hand, but use a calculator and a lot of digits). Find the percentage error of these approximations, using the exact value. Compare with Exercise 3.3.5.1.
Write a well-commented MATLAB function program mymidpoint that calculates the midpoint rule approximation for \(\int f\) on the interval \([a,b]\) with \(n\) subintervals. The inputs should be \(f\text{,}\)\(a\text{,}\)\(b\) and \(n\text{.}\) Use your program on the integral \(\int_{1}^{2} \sqrt{x}\, dx\) to obtain \(M_{4}\) and \(M_{100}\text{.}\) Compare these with Exercise 3.4.4.1 and the true value of the integral.
Write a well-commented MATLAB function program mysimpson that calculates the Simpson’s rule approximation for \(\int f\) on the interval \([a,b]\) with \(n\) subintervals. It should call the program mysimpweights (Program 3.4.2) to produce the coefficients. Use your program on the integral \(\int_{1}^{2} \sqrt{x}\, dx\) to obtain \(S_{4}\) and \(S_{100}\text{.}\) Compare these with Exercise 3.4.4.1, Exercise 3.4.4.2, and the true value of the integral.