Solutions 3: 1 Exercise 5.2.3
Solutions 3: 1 Exercise 5.2.3
February 2, 2016
1 Exercise 5.2.3
Apply composite Simpson’s rule with m = 1, 2, 4 panels to approximate the integrals:
Z 1 Z π/2 Z 1
1
(a) x2 dx = , (b) cos(x) dx = 1, (c) ex dx = e − 1,
0 3 0 0
and report the errors.
Solution
(a) f (x) = x2 .
For m = 1 we put h = (1 − 0)/2 = 1/2, x0 = 0, x1 = 1/2, x2 = 1; y0 = f (x0 ) = 0, y1 = f (x1 ) = 1/4,
y2 = f (x2 ) = 1. The approximation is then I ≈ h/3(y0 + 4y1 + y2 ) = (0 + 4 ∗ 1/4 + 1)/6 = 1/3. The error is
thus 0.
For m = 2 we have h = (1 − 0)/4 = 1/4, (x0 , x1 , . . . , x4 ) = (0, 1/4, 1/2, 3/4, 1), (y0 , y1 , . . . , y4 ) =
(0, 1/16, 1/4, 9/16, 1). The approximation is then I ≈ h/3(y0 + 4y1 + 2y2 + 4y3 + y4 ) = (0 + 4 · 1/16 + 2 ·
1/4 + 4 · 9/16 + 1)/12 = (1/4 + 2/4 + 9/4 + 4/4)/12 = 16/(3 · 4 · 4) = 1/3. The error is thus 0.
One could continue with m = 3, but the error has to be zero. Indeed, Simpson’s rule is based on quadratic
interpolation polynomials, which means that x2 will be represented exactly by the interpolating polynomial
and the quadrature will be exact.
(b) f (x) = cos(x)
√For m = 1 we put h = (π/2 − 0)/2 = π/4, x0 = 0, x1 = π/4, x2 = π/2; y0 = √ f (x0 ) = 1, y1 = f (x1 ) =
1/ 2, y2 = f (x2 ) = 0. The approximation is then I ≈ h/3(y0 + 4y1 + y2 ) = (1 + 4/ 2 + 0) · π/12 ≈ 1.00227.
The error is thus ≈ 0.00227.
For m =√ 2, h = π/8, (x0 , . . . , x4 ) = (0, π/8, π/4, 3π/8, π/2), (y0 , . . . , y4 ) ≈
(1, 0.9238795, 1/ 2, 0.38268343, 0). I ≈ h/3(y0 + 4y1 + 2y2 + 4y3 + y4 ) ≈ 1.000135, giving the error
≈ 0.000135.
For m = 3, h = π/6, I ≈ 1.00002631, errror ≈ 0.00002631.
(c) f (x) = exp(x)
For m = 1 we put h = (1 − 0)/2 = 1/2, x0 = 0, x1 = 1/2, x2 = 1; y0 = f√(x0 ) = 1, y1 = f (x1 ) = e1/2 ,
y2 = f (x2 ) = e. The approximation is then I ≈ h/3(y0 + 4y1 + y2 ) = (1 + 4 e + e)/6 ≈ 1.71886115. The
error is thus ≈ 1.71886115 − e + 1 ≈ 5.79 · 10−4 .
For m = 2, h = 1/4, (x0 , . . . , x4 ) = (0, 1/4, 1/2, 3/4, 1), . . . I ≈ h/3(y0 + 4y1 + 2y2 + 4y3 + y4 ) ≈
1.71831884192175, giving the error ≈ 3.70 · 10−5 .
For m = 3, h = 1/6, I ≈ 1.71828916992083, errror ≈ 7.34 · 10−6 .
2 Exercise 5.2.11
R1
Find the degree of precision of the following approximation for −1 f (x) dx:
First of all, let us evaluate the integral of a monomial xn , n ≥ 0:
Z 1 n+1 x=1 (
n x 0, for odd n,
x dx = =
−1 n + 1 x=−1 2/(n + 1), for even n.
1
(a) f (1) + f (−1):
Let us apply the quadrature to the monomial xn , n ≥ 0:
(
0, for odd n,
1n + (−1)n =
2, for even n,
Therefore the quadrature agrees with the integral for n = 0, 1 (and all odd n), and its degree of precision
is 1. (One could also observe that this is trapezoid quadrature on [−1, 1], which is known to be exact for
polynomials up to degree 1.)
(b) 2/3[f(-1) + f(0) + f(1)]. We check the quadrature on monomials of increasing degree:
2,
for n = 0,
n n n
2/3[(−1) + 0 + (−1) ] = 0, for odd n,
4/3, for even n > 0,
which agrees with the integral for n = 0, 1 (and odd n). Thus degree of precision is 1.
(c) We check the quadrature on monomials of increasing degree:
(
√ n √ n 0, for odd n,
[(−1/ 3) + (1/ 3) ] =
2/3n/2 , for even n,
which agrees with the integral for n = 0, 1, 2, 3 (and odd n). Thus degree of precision is 3. (This is an
example of Gaussian quadrature, which are exact for polynomials of degree up to 2n − 1 when n points are
used.)
3 Exercise 5.2.16
Use the fact that the error term of Boole’s Rule (see Exercise 5.2.15) is proportional to f (6) (c) to find the
exact error term.
Solution
We apply the quadrature to the monomial x6 , for which f (6) (c) ≡ 6!, for all c. Thus
4h x=4h
x7 47 h7
Z
6 2h
x dx = = = (7 · 06 + 32 · h6 + 12 · (2h)6 + 32 · (3h)6 + 7 · (4h)6 ) + C6!
0 7 x=0 7 45
Out[1]:
128h7
−
21
Thus C = −128h7 /(21 · 6!) = −8h7 /945
2
4 Exercise 5.4.1
Apply Adaptive Quadrature by hand, using the Trapezoid Rule with tolerance T OL = 0.05 to approximate
the integrals. Find the approximation error.
Solution
(a) In this case, f (x) = x2 , a0 = 0, b0 = 1.
We begin with n = 1 interval, a = a0 , b = b0 . We will use S[a, b] to denote the Trapezoid quadrature
applied on the interval [a, b].
c = (a+b)/2, S[a, b] = (02 +12 )/2 = 1/2, S[a, c] = (02 +(1/2)2 )/4 = 1/16, S[c, b] = ((1/2)2 +12 )/4 = 5/16.
|S[a, b] − S[a, c] − S[c, b]| = 1/8 = 0.125 < 3 · 0.05 · ((b − a)/(b0 − a0 )) = 0.15. Thus we stop with the
approximation S[a, c] + S[c, b] = 3/8 = 0.375 and an error estimate −(S[a, b] − S[a, c] − S[c, b])/3 = −1/24 ≈
−0.0417. R1
The actual integration error is 0 x2 − 3/8 = 1/3 − 3/8 = −1/24. Our estimate is exact because f 00 is
constant in this case, thus the approximate equation (5.38) in the book is actually exact.
(b) f (x) = cos(x), a0 = 0, b0 = π/2. √
√ = (π/2) · (1 + 0)/2 = π/2 ≈ 1.5708, S[0, π/4] = (π/4) · (1 + 1/ 2)/2 ≈ 0.7854, S[π/4, π, 2] =
S[0, π/2]
(π/4) · (1/ 2 + 0)/2 ≈ 0.27768. |S[a, b] − S[a, c] − S[c, b]| ≈ 0.50772 ≥ 3 · 0.05 · (π/2)/(π/2) = 0.15.
Thus we need to split the √ interval and apply the adaptive algorithm recursively.
S[0, π/4] = (π/4) · (1 + 1/ 2)/2 ≈ 0.7854, S[0, π/8] = (π/8) · (1 + cos(π/8))/2 ≈ 0.37775, S[π/8, π/4] =
(π/8) · (cos(π/8) + cos(π/4))/2 ≈ 0.32024, |S[0, π/4] − S[0, π/8] − S[π/8, π/4]| ≈ 0.087410 ≥ 3 · 0.05 ·
(π/4)/(π/2) = 0.075.
Thus we need to split this interval again apply the adaptive algorithm recursively.
S[0, π/8] = (π/8) · (1 + cos(π/8))/2 ≈ 0.37775, S[0, π/16] = (π/16) · (1 + cos(π/16))/2 ≈ 0.19446,
S[π/16, π/8] = (π/16)·(cos(π/16)+cos(π/8))/2 ≈ 0.18699, |S[0, π/8]−S[0, π/16]−S[π/16, π/8]| ≈ 0.0037 <
3 · 0.05 · (π/8)/(π/2) = 0.0375.
Thus we are done on the interval [0, π/8] with the approximation S[0, π/16] + S[π/16, π/8] ≈ 0.38145.
Let us look at the interval [π/8, π/4] now. S[π/8, π/4] = (π/8) · (cos(π/8) + cos(pi/4))/2 ≈ 0.32024,
S[π/8, 3π/16] = (π/16) · (cos(π/8) + cos(3π/16))/2 ≈ 0.17233, S[3π/16, π/4] = (π/16) · (cos(3π/16) +
cos(π/4))/2 ≈ 0.15105, |S[π/8, π/4] − S[π/8, 3π/16] − S[3π/16, π/4]| ≈ 0.00314 < 3 · 0.05 · (π/8)/(π/2) =
0.0375.
Thus we are done on the interval [π/8, π/4] with the approximation S[π/8, 3π/16] + S[3π/16, π/4] ≈
0.32338.
Let us look at the interval [π/4, π/2] now. S[π/4, π/2] = (π/4) · (cos(π/4) + cos(pi/2))/2 ≈ 0.27768,
S[π/4, 3π/8] = (π/8) · (cos(π/4) + cos(3π/8))/2 ≈ 0.21398, S[3π/8, π/2] = (π/8) · (cos(3π/8) + cos(π/2))/2 ≈
0.075140, |S[π/4, π/2] − S[π/4, 3π/8] − S[3π/8, π/2]| ≈ 0.01144 < 3 · 0.05 · (π/4)/(π/2) = 0.075.
Thus we are done on the interval [π/4, π/2] with the approximation S[π/4, 3π/8]+S[3π/8, π/2] ≈ 0.28912.
There are no intervals left, and therefore the final approximation is ≈ 0.38145 + 0.32338 + 0.28912 =
R π/2
0.99395 whereas the exact integral is 0 cos(x) = 1. Thus the error is ≈ |0.99395 − 1| ≈ 0.006 < 0.05.
(c) In this case, f (x) = exp(x), a0 = 0, b0 = 1.
S[0, 1] = (1 + e)/2 ≈ 1.85914, S[0, 0.5] = (1 + exp(0.5))/4 ≈ 0.66218, S[0.5, 1] = (exp(0.5) + exp(1))/4 ≈
1.09175. |S[a, b] − S[a, c] − S[c, b]| ≈ 0.1052 < 3 · 0.05 · ((b − a)/(b0 − a0 )) = 0.15. Thus we stop with the
approximation S[a, c] + S[c, b] ≈ 1.75393 and an error estimate −(S[a, b] − S[a, c] − S[c, b])/3 ≈ −0.035.
R1
The actual integration error is 0 exp(x) − (S[a, c] + S[c, b]) ≈ −0.036. Our estimate is quite good in this
case.
5 Exercise 5.4.4
Develop an Adaptive Quadrature method for rule (5.28).
Solution
Let c = (a + b)/2, h = b − a and apply the quadrature on [a, b], [a, c], and [c, b] (we use the same notation
as in the book):
3
b
14h5 (4)
Z
f (x) dx = S[a, b] + f (c1 ), c1 ∈ [a, b],
a 45
c b
14(h/2)5 (4) 14(h/2)5 (4)
Z Z
f (x) dx + f (x) dx = S[a, c] + S[c, b] + f (c2 ) + f (c3 ), c2 ∈ [a, c], c3 ∈ [c, b].
a c 45 45
Further assuming that f (4) (c1 ) ≈ f (4) (c2 ) ≈ f (4) (c3 ) we obtain:
b
14h5 (4)
Z
f (x) dx = S[a, b] + f (c1 ),
a 45
c b
1 14h5 (4)
Z Z
f (x) dx + f (x) dx ≈ S[a, c] + S[c, b] + f (c1 ).
a c 16 45
We can now subtract the second equation from the first to obtain:
1 14h5 (4)
S[a, b] − S[a, c] − S[c, b] ≈ 15
f (c1 ).
16 45
Thus the number S[a, b] − S[a, c] − S[c, b] gives an approximation to 15 times the error of the quadrature
S[a, c] + S[c, b].
The rest is exactly the same as for other quadratures; one stops subdividing the interval when |S[a, b] −
S[a, c] − S[c, b]| < 15 · T OL · (b − a)/(borig − aorig ) and returns S[a, c] + S[c, b] as the approximation of the
integral over the interval [a,b].
6 Exercise 5.5.1
Approximate the integrals using n = 2 Gaussian Quadrature. Compare with the correct value and give the
approximation error.
Solution √
Gaussian Quadrature with n = 2 is defined by the points x1,2 = ±1/ 3, w1,2 = 1.
In [2]: x1 = -1/sqrt(3)
x2 = 1/sqrt(3)
w1 = 1
w2 = 1
(a)
# Exact integral
integrate(f(x),(x,-1,1))
Out[3]:
4
Out[4]:
0
Thus the error is 0, which agrees with the theory for Gaussian Quadratures being exact for polynomials
up to degree 2n − 1.
(b)
In [5]: f=Lambda(x,x**4)
# Exact integral
integrate(f(x),(x,-1,1))
Out[5]:
2
5
In [6]: # Numerical quadrature
f(x1)*w1 + f(x2)*w2
Out[6]:
2
9
In [7]: # Approximation error
2/5-2/9
Out[7]:
0.1777777777777778
(c)
In [8]: f=Lambda(x,exp(x))
# Exact integral
integrate(f(x),(x,-1,1))
Out[8]:
1
− +e
e
In [9]: # Numerical quadrature
f(x1)*w1 + f(x2)*w2
Out[9]:
√ √
3 3
e− 3 +e 3
Out[10]:
0.00770629937787234
(d)
5
In [11]: f=Lambda(x,cos(pi*x))
# Exact integral
integrate(f(x),(x,-1,1))
Out[11]:
Out[12]:
√ !
3π
2 cos
3
Out[13]:
0.481237029038818
7 Exercise 5.5.7
Show that the Legendre polynomials p1 (x) = x and p2 (x) = x2 − 1/3 are orthogonal on [−1, 1].
R1 R1
Solution −1 p1 (x)p2 (x) dx = −1 (x3 − x/3) dx = 0 because x3 − x/3 is an odd function.
8 Exercise 5.5.8
Compute Legendre polynomials up to degree 3.
Solution
Legendre polynomials are given by the expression
1 d
pi (x) = [(x2 − 1)i ].
2i i! dxi
Thus
p0 (x) = 1,
p1 (x) = 1/2[(x2 − 1)]0 = x,
p2 (x) = 1/(4 · 2)[(x2 − 1)2 ]00 = 1/8[x4 − 2x2 + 1]00 = 1/8[12x2 − 4] = 3/2(x2 − 3),
p3 (x) = 1/(8 · 6)[(x2 − 1)3 ]000 = 1/48[x6 − 3x4 + 3x2 − 1]000 = 1/48[120x3 − 72x] = 5/2[x3 − 3/5x].
9 Exercise 5.5.9
Verify the coefficients ci and xi in Table 5.1 for degree n = 3.
Solution
2
p have computed p3 (x) = 5/2x(x − 3/5) in Exercise 5.5.8. This immediatly implies that p3 (0) =
We
p3 (± 3/5) = 0 confirming the roots listed in Table 5.1.
The coefficients ci are obtained by integrating the Lagrange polynomials. For example,
6
p p
(x − x1 )(x − x3 ) (x + 3/5)(x − 3/5) 5
L2 (x) = = p p = 1 − x2 ,
(x2 − x1 )(x2 − x3 ) (0 + 3/5)(0 − 3/5) 3
and therefore
Z 1
c2 = [1 − 5/3x2 ] dx = [x − 5/9x3 ]x=1
x=−1 = 2 − 10/9 = 8/9.
−1
Similarly
p p
(x − 0)(x ∓ 3/5) x(x ∓ 3/5)
L1,3 (x) = p p p = ,
(∓ 3/5 − 0)(∓ 3/5 ∓ 3/5) 2 · 3/5
and therefore
Z 1
5 p 5 3 p 52 5
c1,3 = [x2 ∓ x 3/5] dx = [x /3 ∓ 3/5x2 /2]x=1
x=−1 = = .
6 −1 6 63 9
(a) 1.547866
(b) 1.277978
(c) 1.277978