Algorithms 9
(b) Apply your algorithm to approximate the value of R2
1dx/x. Take n= 4.
Compare the approximation obtained from the midpoint rule with the
approximation obtained from the trapezoidal rule.
(a) GIVEN: the limits of integration aand b
(b) Matching this specific problem to the general pattern Rb
12. Consider the following algorithm for the trapezoidal rule:
GIVEN: the limits of integration aand b
the integrand f
the number of subintervals n
STEP 1: compute h= (b−a)/n; and initialize sum to 0
STEP 2: for ifrom 1 to n−1
add 2f(a+ih) to sum
STEP 3: add f(a) and f(b) to sum
OUTPUT: (h/2)sum
Compare the number of arithmetic operations required by this algorithm to the
number of operations required by the algorithm presented in the text.
Aside from the operations needed to evaluate f, the trapezoidal rule algorithm
presented in the text uses 2n+ 1 additions/subtractions (one in STEP 1, 2(n−1)