Numerical Integration: Rectangle Method
Rectangle Method
Let . The rectangle method utilizes the Riemann integral definition to calculate an approximate estimate for the area under the curve by drawing many rectangles with very small width adjacent to each other between the graph of the function and the axis. For simplicity, the width of the rectangles is chosen to be constant. Let be the number of intervals with and constant spacing . The rectangle method can be implemented in one of the following three ways:
If and are the left and right points defining the rectangle number , then assumes that the height of the rectangle is equal to , assumes that the height of the rectangle is equal to , and assumes that the height of the rectangle is equal to . is called the midpoint rule.
To illustrate the difference, consider the function on the interval . The exact integral can be calculated as
The following three tools show the implementation of the rectangle method for numerically integrating the function using , , and , respectively. For , each rectangle touches the graph of the function at the top left corner of the rectangle. For , each rectangle touches the graph of the function at the mid point of the top side. For , each rectangle touches the graph of the function at the top right corner of the rectangle. The areas of the rectangles are calculated underneath each curve. Use the slider to increase the number of rectangles to see how many rectangles are needed to get a good approximation for the area under the curve.
The following Mathematica code can be used to numerically integrate any function on the interval using the three options for the rectangle method.
I1[f_, a_, b_, n_] := (h = (b - a)/n; Sum[f[a + (i - 1)*h]*h, {i, 1, n}]) I2[f_, a_, b_, n_] := (h = (b - a)/n; Sum[f[a + (i - 1/2)*h]*h, {i, 1, n}]) I3[f_, a_, b_, n_] := (h = (b - a)/n; Sum[f[a + (i)*h]*h, {i, 1, n}]) f[x_] := x^2; I1[f, 0, 1, 13.0] I2[f, 0, 1, 13.0] I3[f, 0, 1, 13.0]
def I1(f, a, b, n): h = (b - a)/n return sum([f(a + i*h)*h for i in range(int(n))]) def I2(f, a, b, n): h = (b - a)/n return sum([f(a + (i + 1/2)*h)*h for i in range(int(n))]) def I3(f, a, b, n): h = (b - a)/n return sum([f(a + (i + 1)*h)*h for i in range(int(n))]) def f(x): return x**2 print("I1:",I1(f, 0, 1, 13.0)) print("I2:",I2(f, 0, 1, 13.0)) print("I3:",I3(f, 0, 1, 13.0))
The following link provides the MATLAB codes for implementing the rectangle method.
Error Analysis
Taylor’s theorem can be used to find how the error changes as the step size decreases. First, let’s consider (the same applies to ). The error in the calculation of rectangle number between the points and will be estimated. Using Taylor’s theorem, such that:
The error in the integral using this rectangle can be calculated as follows:
If is the number of subdivisions (number of rectangles), i.e., , then:
In other words, the total error is bounded by a term that is directly proportional to . When decreases, the error bound decreases proportionally. Of course when goes to zero, the error goes to zero as well.
(Midpoint rule) provides a faster convergence rate, or a more accurate approximation as the error is bounded by a term that is directly proportional to as will be shown here.
Using Taylor’s theorem, such that:
Where . The error in the integral using this rectangle can be calculated as follows:
If is the number of subdivisions (number of rectangles), i.e., , then:
In other words, the total error is bounded by a term that is directly proportional to which provides faster convergence than . The tools shown above can provide a good illustration of this. With only five rectangles, already provides a very good estimate for the integral compared to both , and .
Example
Using the rectangle method with , calculate , , and and compare with the exact integral of the function on the interval . Then, find the values of required so that the total error obtained by and that obtained by are bounded by 0.001.
Solution
Since the spacing can be calculated as:
Therefore, , , , , and . The values of the function at these points are given by:
According to the rectangle method we have:
For , we need to calculate the values of the function at the midpoint of each rectangle:
Therefore:
The exact integral is given by:
Obviously, provides a good approximation with only 4 rectangles!
Error Bounds
The total errors obtained when are indeed less than the error bounds obtained by the formulas listed above. For and , the error in the estimation is bounded by:
The errors and are indeed less than that upper bound. The same formula can be used to find the value of so that the error is bounded by 0.001:
Therefore, to guarantee an error less than 0.001 using , the interval will have to be divided into: rectangles! In this case and
Similarly, when the error in the estimate using is bounded by:
The error is indeed bounded by that upper error. The same formula can be used to find the value of so that the error is bounded by 0.001:
Therefore, to guarantee an error less than 0.001 using , the interval will have to be divided into: rectangles! In this case and
Thank you for uploading informative lectures.
I have a question about error analysis for I2 (midpoint rule).
In this lecture, using Taylor’s theorem, we expand up to three terms (including the remainder term) for I2 while up to two terms for I1 (and I3). Is there a specific reason for this?
(I guess it is because we use two points, x(i-1) and x(i), to calculate the midpoint, but still this does not sound plausible to me.)
I would appreciate it if you could explain this for me.
Again, thank you for uploading your wonderful lectures.
Indeed, intuitively speaking, using the mid-point leads to a decrease in the associated error since there is tendency for positive and negative errors to cancel each other.
When attempting to find a rigorous upper bound for the error, one starts with the Taylor approximation with as many terms as possible. For the I1 and I3, having more than two terms will not enable finding an upper bound. For I2, with three terms, it is possible to find an upper bound as per the derivation above.
Thank you for your explanation. Now I can see it.