This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods. The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon! < 21.2 Riemann’s Integral | Contents | 21.4 Simpson’s Rule > Trapezoid Rule The Trapezoid Rule fits a trapezoid into each subinterval and sums the areas of the trapezoid to approximate the total integral. This approximation for the integral to an arbitrary function is shown in the following figure. For each subinterval, the Trapezoid Rule computes the area of a trapezoid with corners at (xi , 0), (xi+1 , 0), (xi , f (xi )) , and (xi+1 , f (xi+1 )), which is h f (xi )+f (xi+1 ) 2 . Thus, the Trapezoid Rule approximates integrals according to the expression n−1 b ∫ f (x)dx ≈ ∑ h a f (xi ) + f (xi+1 ) . 2 i=0 TRY IT! You may notice that the Trapezoid Rule “double-counts” most of the terms in the series. To illustrate this fact, consider the expansion of the Trapezoid Rule: n−1 ∑h f (xi ) + f (xi+1 ) h = 2 i=0 2 [(f (x0 ) + f (x1 )) + (f (x1 ) + f (x2 )) + (f (x2 ) +f (x3 )) + ⋯ + (f (xn−1 ) + f (xn ))] . Computationally, this is many extra additions and calls to f (x) than is really necessary. We can be more computationally efficient using the following expression. b ∫ f (x)dx ≈ a n−1 h 2 (f (x0 ) + 2 (∑ f (xi )) + f (xn )) . i=1 To determine the accuracy of the Trapezoid Rule approximation, we first take Taylor series expansion of f (x) around y i = xi+1 +xi 2 , which is the midpoint between xi and xi+1 . This Taylor series expansion is f ′ f (x) = f (y i ) + f (y i )(x − y i ) + Computing the Taylor series at xi and xi+1 and noting that xi ′′ (y i )(x − y i ) 2 + ⋯ 2! − yi = − h 2 and xi+1 following expressions: ′ f (xi ) = f (y i ) − hf (y i ) 2 h f ′′ + 2 (y i ) − ⋯ 8 and ′ f (xi+1 ) = f (y i ) + hf (y i ) 2 2 h f ′′ + (y i ) + ⋯. 8 Taking the average of these two expressions results in the new expression, f (xi+1 ) + f (xi ) 2 2 = f (y i ) + O(h ). − yi = h 2 , results in the Solving this expression for f (y i ) yields f (xi+1 ) + f (xi ) f (y i ) = 2 + O(h ). 2 Now returning to the Taylor expansion for f (x) , the integral of f (x) over a subinterval is xi+1 ∫ xi+1 xi f ′ f (x)dx = ∫ (f (y i ) + f (y i )(x − y i ) + ′′ (y i )(x − y i ) 2 + ⋯) dx. 2! xi Distributing the integral results in the expression xi+1 xi+1 f (x)dx = ∫ ∫ xi xi+1 f (y i )dx + ∫ xi xi+1 ′ f f (y i )(x − y i )dx + ∫ xi ′′ (y i )(x − y i ) 2 dx + ⋯ 2! xi Now since xi and xi+1 are symmetric around y i , the integrals of the odd powers of (x − y i )p disappear and the even powers resolve to a multiple hp+1 . xi+1 3 ∫ f (x)dx = hf (y i ) + O(h ). xi Now if we substitute f (y i ) with the expression derived explicitly in terms of f (xi ) and f (xi+1 ), we get xi+1 ∫ f (x)dx = h ( f (xi+1 ) + f (xi ) 2 3 + O(h )) + O(h ), 2 xi which is equivalent to h( f (xi+1 ) + f (xi ) 2 3 ) + hO(h ) + O(h ) 2 and therefore, xi+1 ∫ f (x)dx = h ( f (xi+1 ) + f (xi ) 2 xi Since h 2 (f (xi+1 ) + f (xi )) 3 ) + O(h ). is the Trapezoid Rule approximation for the integral over the subinterval, it is O(h3 ) for a single subinterval and O(h2 ) over the whole interval. π TRY IT! Use the Trapezoid Rule to approximate ∫0 sin(x)dx interval. Compare this value to the exact value of 2. import numpy as np a b n h x f = = = = = = 0 np.pi 11 (b - a) / (n - 1) np.linspace(a, b, n) np.sin(x) I_trap = (h/2)*(f[0] + \ 2 * sum(f[1:n-1]) + f[n-1]) err_trap = 2 - I_trap print(I_trap) print(err_trap) 1.9835235375094546 0.01647646249054535 < 21.2 Riemann’s Integral | Contents | 21.4 Simpson’s Rule > © Copyright 2020. with 11 evenly spaced grid points over the whole