Lecture 6
Lecture 6
Lecture 6
Numerical integration
Composite rules
0 / 25
Problem Setting
Rb
Aim: Given f ∈ C[a, b], compute a
f (x)dx.
1 / 25
Problem Setting
Rb
Aim: Given f ∈ C[a, b], compute a
f (x)dx.
Rb P
Idea: Construct xi ∈ [a, b] and αi so that a
f (x)dx ≈ αi f (xi ).
1 / 25
Problem Setting
Rb
Aim: Given f ∈ C[a, b], compute a
f (x)dx.
Rb P
Idea: Construct xi ∈ [a, b] and αi so that a
f (x)dx ≈ αi f (xi ).
Two approaches:
• Equally spaced xi , based on interpolating polynomials.
Examples: rectangular rule, trapezoidal rule, Simpson’s ( 13 ) rule.
• Unequally spaced xi , based on osculating polynomials.
Example: Gaussian quadrature.
1 / 25
Rectangular Rule
Idea:
• Partition [a, b] into
2 / 25
Rectangular Rule
Idea:
• Partition [a, b] into
2 / 25
Rectangular Rule
Idea:
• Partition [a, b] into
2 / 25
Rectangular Rule
b−a
Formula: On each [xi , xi+1 ], write h = n = xi+1 − xi . We have
Z xi+1
xi + xi+1 xi + xi+1
f dx = f h.
xi 2 2
3 / 25
Rectangular Rule
b−a
Formula: On each [xi , xi+1 ], write h = n = xi+1 − xi . We have
Z xi+1
xi + xi+1 xi + xi+1
f dx = f h.
xi 2 2
Thus,
Z b n−1 Z xi+1 n−1
X xi + xi+1 X xi + xi+1
f (x)dx ≈ f dx = f h.
a xi 2 2
i=0 i=0
3 / 25
Rectangular Rule
4 / 25
Rectangular Rule
Rx
Define r (x) := xi
(y − ui )dy. Then r (xi ) = 0 and
xi+1
1
r (xi+1 ) = (y − ui )2 = 0.
2 xi
4 / 25
Rectangular Rule
5 / 25
Rectangular Rule
n−1 3 n−1
X h ′′ h3 1 X ′′
= f (µi ) = ·n· f (µi )
24 24 n
i=0 i=0
(b − a)h2 ′′
= |f (µ)|
24
for some µ ∈ [a, b].
6 / 25
Example
Rπ
Example: Compute 02 sin x dx by the rectangular rule using n = 4
and round your answer to the nearest 4 decimal places. Show that
the truncation error is at most 0.011.
7 / 25
Example
Rπ
Example: Compute 02 sin x dx by the rectangular rule using n = 4
and round your answer to the nearest 4 decimal places. Show that
the truncation error is at most 0.011.
iπ π
Solution: Note that f (x) = sin x, xi = 8 for i = 0, . . . , 4, and h = 8.
7 / 25
Example
Rπ
Example: Compute 02 sin x dx by the rectangular rule using n = 4
and round your answer to the nearest 4 decimal places. Show that
the truncation error is at most 0.011.
iπ π
Solution: Note that f (x) = sin x, xi = 8 for i = 0, . . . , 4, and h = 8.
Z π
2 π π 3π 5π 7π
sin x dx ≈ sin + sin + sin + sin
0 8 16 16 16 16
= 1.0065.
7 / 25
Example
Rπ
Example: Compute 02 sin x dx by the rectangular rule using n = 4
and round your answer to the nearest 4 decimal places. Show that
the truncation error is at most 0.011.
iπ π
Solution: Note that f (x) = sin x, xi = 8 for i = 0, . . . , 4, and h = 8.
Z π
2 π π 3π 5π 7π
sin x dx ≈ sin + sin + sin + sin
0 8 16 16 16 16
= 1.0065.
(b − a)h2 ′′
Error = |f (µ)| for some µ ∈ [0, π2 ],
24
π π 2
( )
≤ 2 8 ≈ 0.0101 < 0.011.
24
7 / 25
Trapezoidal Rule
Idea:
• Partition [a, b] into
8 / 25
Trapezoidal Rule
Idea:
• Partition [a, b] into
8 / 25
Trapezoidal Rule
Idea:
• Partition [a, b] into
8 / 25
Trapezoidal Rule
Formula: On each [xi , xi+1 ], write h = b−a n = xi+1 − xi . We have
Z xi+1 Z xi+1
x − xi x − xi+1
p1 (x) dx = fi+1 + fi dx.
xi xi xi+1 − xi xi − xi+1
Z xi+1
x − xi x − xi+1
= fi+1 − fi dx
xi h h
1 xi+1
= (x − xi )2 fi+1 − (x − xi+1 )2 fi
2h xi
1 2 fi+1 + f i fi+1 + fi
= (xi+1 − xi ) = h.
h 2 2
9 / 25
Trapezoidal Rule
Formula: On each [xi , xi+1 ], write h = b−a n = xi+1 − xi . We have
Z xi+1 Z xi+1
x − xi x − xi+1
p1 (x) dx = fi+1 + fi dx.
xi xi xi+1 − xi xi − xi+1
Z xi+1
x − xi x − xi+1
= fi+1 − fi dx
xi h h
1 xi+1
= (x − xi )2 fi+1 − (x − xi+1 )2 fi
2h xi
1 2 fi+1 + f i fi+1 + fi
= (xi+1 − xi ) = h.
h 2 2
Thus,
Z b n−1
X fi+1 + fi f0 + fn
f (x)dx ≈ h= + f1 + f2 + · · · + fn−1 h.
a 2 2
i=0
9 / 25
Trapezoidal Rule
Truncation error: Assume that f ∈ C 2 [a, b].
On each [xi , xi+1 ], we have
Z xi+1 Z xi+1
[f (x) − p1 (x)]dx = f [xi , xi+1 , x] (x − xi )(x − xi+1 ) dx
xi xi | {z }
≤0
Z xi+1
= f [xi , xi+1 , ξi ] (x − xi )(x − xi+1 )dx for some ξi ∈ [xi , xi+1 ],
xi
Z xi+1
= f [xi , xi+1 , ξi ] (x − xi )(x − xi − h)dx
xi
Z xi+1
= f [xi , xi+1 , ξi ] [(x − xi )2 − (x − xi )h]dx
xi
h3 h3
=− f [xi , xi+1 , ξi ] = − f ′′ (νi ) for some νi ∈ [xi , xi+1 ].
6 12
10 / 25
Trapezoidal Rule
Truncation error cont.: Total truncation error is
n−1 Z
X xi+1
f [xi , xi+1 , x](x − xi )(x − xi+1 )dx
i=0 xi
n−1 3 n−1
X h ′′ h3 1 X ′′
= f (νi ) = ·n· f (νi )
12 12 n
i=0 i=0
2
(b − a)h ′′
= |f (ν)|
12
for some ν ∈ [a, b].
11 / 25
Trapezoidal Rule
Truncation error cont.: Total truncation error is
n−1 Z
X xi+1
f [xi , xi+1 , x](x − xi )(x − xi+1 )dx
i=0 xi
n−1 3 n−1
X h ′′ h3 1 X ′′
= f (νi ) = ·n· f (νi )
12 12 n
i=0 i=0
2
(b − a)h ′′
= |f (ν)|
12
for some ν ∈ [a, b].
Remark: Note that this error seems larger than that of rectangular
rule, which is counter-intuitive. However, note that the ν here and the
µ for the rectangular rule can be radically different: given that both
errors are in the order of h2 , this can have a big impact on the error.
11 / 25
Example
Rπ
Example: Compute 02 sin x dx by the trapezoidal rule using n = 4
and round your answer to the nearest 4 decimal places. Show that
the truncation error is at most 0.021.
12 / 25
Example
Rπ
Example: Compute 02 sin x dx by the trapezoidal rule using n = 4
and round your answer to the nearest 4 decimal places. Show that
the truncation error is at most 0.021.
iπ π
Solution: Note that f (x) = sin x, xi = 8 for i = 0, . . . , 4, and h = 8.
12 / 25
Example
Rπ
Example: Compute 02 sin x dx by the trapezoidal rule using n = 4
and round your answer to the nearest 4 decimal places. Show that
the truncation error is at most 0.021.
iπ π
Solution: Note that f (x) = sin x, xi = 8 for i = 0, . . . , 4, and h = 8.
π
π sin 0 + sin π2
Z
2 π π 3π
sin x dx ≈ + sin + sin + sin
0 8 2 8 4 8
= 0.9871.
12 / 25
Example
Rπ
Example: Compute 02 sin x dx by the trapezoidal rule using n = 4
and round your answer to the nearest 4 decimal places. Show that
the truncation error is at most 0.021.
iπ π
Solution: Note that f (x) = sin x, xi = 8 for i = 0, . . . , 4, and h = 8.
π
π sin 0 + sin π2
Z
2 π π 3π
sin x dx ≈ + sin + sin + sin
0 8 2 8 4 8
= 0.9871.
(b − a)h2 ′′
Error = |f (ν)| for some ν ∈ [0, π2 ]
12
π π 2
( )
≤ 2 8 ≈ 0.0202 < 0.021.
12
12 / 25
Simpson’s Rule
Idea:
• Partition [a, b] into
13 / 25
Simpson’s Rule
Idea:
• Partition [a, b] into
13 / 25
Simpson’s Rule
Idea:
• Partition [a, b] into
13 / 25
Simpson’s Rule
b−a
Formula: On each [xi , xi+2 ], i even, write h = n = xi+1 − xi .
Consider
(x − xi+1 )(x − xi+2 ) (x − xi )(x − xi+2 )
p2 (x) = fi + fi+1
(xi − xi+1 )(xi − xi+2 ) (xi+1 − xi )(xi+1 − xi+2 )
(x − xi )(x − xi+1 )
+ fi+2
(xi+2 − xi )(xi+2 − xi+1 )
(x − xi+1 )(x − xi+2 ) (x − xi )(x − xi+2 )
= fi + fi+1
2h2 −h2
(x − xi )(x − xi+1 )
+ fi+2 .
2h2
We integrate the three quadratics one by one on [xi , xi+2 ].
14 / 25
Simpson’s Rule
Hence,
Z xi+2
h
p2 (x) dx = (fi + 4fi+1 + fi+2 ).
xi 3
15 / 25
Simpson’s Rule
16 / 25
Simpson’s Rule
Truncation error: Assume that f ∈ C 4 [a, b].
17 / 25
Simpson’s Rule
Truncation error: Assume that f ∈ C 4 [a, b].
Rx
Define r (x) := xi (y − xi )(y − xi+1 )(y − xi+2 )dy . Then r (xi ) = 0 and
(substituting y = xi+1 + sh)
Z 1
r (xi+2 ) = h4 (s + 1)s(s − 1)ds = 0.
−1
17 / 25
Simpson’s Rule
18 / 25
Simpson’s Rule
19 / 25
Simpson’s Rule
Truncation error cont.: Consequently,
Z xi+2
f [xi , xi+1 , xi+2 , x](x − xi )(x − xi+1 )(x − xi+2 )dx
xi
20 / 25
Simpson’s Rule
Truncation error cont.: Consequently,
Z xi+2
f [xi , xi+1 , xi+2 , x](x − xi )(x − xi+1 )(x − xi+2 )dx
xi
20 / 25
Example 1
Rπ
Example: Compute 02 sin x dx by the Simpson’s rule using n = 4
and round your answer to the nearest 4 decimal places. Show that
the truncation error is at most 3 × 10−4 .
21 / 25
Example 1
Rπ
Example: Compute 02 sin x dx by the Simpson’s rule using n = 4
and round your answer to the nearest 4 decimal places. Show that
the truncation error is at most 3 × 10−4 .
iπ π
Solution: Note that f (x) = sin x, xi = 8 for i = 0, . . . , 4, and h = 8.
21 / 25
Example 1
Rπ
Example: Compute 02 sin x dx by the Simpson’s rule using n = 4
and round your answer to the nearest 4 decimal places. Show that
the truncation error is at most 3 × 10−4 .
iπ π
Solution: Note that f (x) = sin x, xi = 8 for i = 0, . . . , 4, and h = 8.
Z π
2 1 π π π π 3π
sin x dx ≈ · sin 0 + sin + 2 sin + 4 sin + sin
0 3 8 2 4 8 8
= 1.0001.
21 / 25
Example 1
Rπ
Example: Compute 02 sin x dx by the Simpson’s rule using n = 4
and round your answer to the nearest 4 decimal places. Show that
the truncation error is at most 3 × 10−4 .
iπ π
Solution: Note that f (x) = sin x, xi = 8 for i = 0, . . . , 4, and h = 8.
Z π
2 1 π π π π 3π
sin x dx ≈ · sin 0 + sin + 2 sin + 4 sin + sin
0 3 8 2 4 8 8
= 1.0001.
(b − a)h4 (4)
Error = |f (γ)| for some γ ∈ [0, π2 ],
180
π π 4
( )
≤ 2 8 ≈ 2.08 × 10−4 < 3 × 10−4 .
180
21 / 25
Example 2
R1 2
Example: Compute 0 e−x dx by Simpson’s rule so that the
truncation error is at most 10−4 . Round your answer to the nearest 4
decimal places.
22 / 25
Example 2
R1 2
Example: Compute 0 e−x dx by Simpson’s rule so that the
truncation error is at most 10−4 . Round your answer to the nearest 4
decimal places.
2
Solution: We compute up to the 4th derivative of f (x) = e−x .
2
f ′ (x) = −2xe−x ,
2
f ′′ (x) = (4x 2 − 2)e−x ,
2
f ′′′ (x) = (12x − 8x 3 )e−x ,
2
f (4) (x) = (12 − 48x 2 + 16x 4 )e−x .
22 / 25
Example 2
R1 2
Example: Compute 0 e−x dx by Simpson’s rule so that the
truncation error is at most 10−4 . Round your answer to the nearest 4
decimal places.
2
Solution: We compute up to the 4th derivative of f (x) = e−x .
2
f ′ (x) = −2xe−x ,
2
f ′′ (x) = (4x 2 − 2)e−x ,
2
f ′′′ (x) = (12x − 8x 3 )e−x ,
2
f (4) (x) = (12 − 48x 2 + 16x 4 )e−x .
Thus,
max |f (4) (x)| ≤ 76.
0≤x≤1
22 / 25
Example 2
R1 2
Example: Compute 0 e−x dx by Simpson’s rule so that the
truncation error is at most 10−4 . Round your answer to the nearest 4
decimal places.
2
Solution: We compute up to the 4th derivative of f (x) = e−x .
2
f ′ (x) = −2xe−x ,
2
f ′′ (x) = (4x 2 − 2)e−x ,
2
f ′′′ (x) = (12x − 8x 3 )e−x ,
2
f (4) (x) = (12 − 48x 2 + 16x 4 )e−x .
Thus,
max |f (4) (x)| ≤ 76.
0≤x≤1
One can also use the tighter bound max0≤x≤1 |f (4) (x)| ≤ 20 (Why?). The resulting h and integral are left as an exercise.
22 / 25
Example 2 cont.
23 / 25
Example 2 cont.
23 / 25
Example 3
R1
x2
Example: Compute 0
sin 2 + 1 dx by Simpson’s rule so that the
−4
truncation error is at most 10 . Round your answer to the nearest 4
decimal places.
24 / 25
Example 3
R1
x2
Example: Compute 0
sin 2 + 1 dx by Simpson’s rule so that the
−4
truncation error is at most 10 . Round your answer to the nearest 4
decimal places.
2
Solution: We compute up to the 4th derivative of f (x) = sin x2 + 1 .
2 2 2
x x x
f ′ (x) = x cos + 1 , f ′′ (x) = cos + 1 − x 2 sin +1 ,
2 2 2
2 2
x x
f ′′′ (x) = −x 3 cos + 1 − 3x sin +1 ,
2 2
2 2
(4) 2 x 4 x
f (x) = −6x cos + 1 + (x − 3) sin +1 .
2 2
24 / 25
Example 3
R1
x2
Example: Compute 0
sin 2 + 1 dx by Simpson’s rule so that the
−4
truncation error is at most 10 . Round your answer to the nearest 4
decimal places.
2
Solution: We compute up to the 4th derivative of f (x) = sin x2 + 1 .
2 2 2
x x x
f ′ (x) = x cos + 1 , f ′′ (x) = cos + 1 − x 2 sin +1 ,
2 2 2
2 2
x x
f ′′′ (x) = −x 3 cos + 1 − 3x sin +1 ,
2 2
2 2
(4) 2 x 4 x
f (x) = −6x cos + 1 + (x − 3) sin +1 .
2 2
Thus,
max |f (4) (x)| ≤ 10.
0≤x≤1
24 / 25
Example 3 cont.
Solution cont.: To force error ≤ 10−4 , it suffices to force
b−a 4
h max |f (4) (x)| ≤ 10−4 ,
180 0≤x≤1
25 / 25
Example 3 cont.
Solution cont.: To force error ≤ 10−4 , it suffices to force
b−a 4
h max |f (4) (x)| ≤ 10−4 ,
180 0≤x≤1
25 / 25