Page 1
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Numerical Integration andDifferentiation
CS 205A:Mathematical Methods for Robotics, Vision, and Graphics
Justin Solomon
CS 205A: Mathematical Methods Numerical Integration and Differentiation 1 / 34
Page 2
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Today’s Task
Last time: Find f (x)
Today: Find∫ ba f (x) dx
and f ′(x)
CS 205A: Mathematical Methods Numerical Integration and Differentiation 2 / 34
Page 3
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Motivation
erf(x) =2√π
∫ x
0
e−t2
dt
Some functions are defined using integrals!
CS 205A: Mathematical Methods Numerical Integration and Differentiation 3 / 34
Page 4
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Sampling from a Distribution
p(x) ∈ Prob([0, 1])
Cumulative distribution function (CDF):
F (t) ≡∫ t
0
p(x) dx
X distributed uniformly in [0, 1] =⇒F−1(X) distributed according to p
CS 205A: Mathematical Methods Numerical Integration and Differentiation 4 / 34
Page 5
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Sampling from a Distribution
p(x) ∈ Prob([0, 1])
Cumulative distribution function (CDF):
F (t) ≡∫ t
0
p(x) dx
X distributed uniformly in [0, 1] =⇒F−1(X) distributed according to p
CS 205A: Mathematical Methods Numerical Integration and Differentiation 4 / 34
Page 6
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Sampling from a Distribution
p(x) ∈ Prob([0, 1])
Cumulative distribution function (CDF):
F (t) ≡∫ t
0
p(x) dx
X distributed uniformly in [0, 1] =⇒F−1(X) distributed according to p
CS 205A: Mathematical Methods Numerical Integration and Differentiation 4 / 34
Page 7
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Rendering
“Light leaving a surface is the integral of the
light coming in after it is reflected and diffused.”
Rendering equation
CS 205A: Mathematical Methods Numerical Integration and Differentiation 5 / 34
Page 8
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Gaussian Blur
http://www.borisfx.com/images/bcc3/gaussian_blur.jpg
CS 205A: Mathematical Methods Numerical Integration and Differentiation 6 / 34
Page 9
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Bayes’ Rule
P (X|Y ) =P (Y |X)P (X)∫P (Y |X)P (X) dY
Probability of X given Y
CS 205A: Mathematical Methods Numerical Integration and Differentiation 7 / 34
Page 10
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Big Problem
“This leads to a situation where we are trying tominimize an energy function that we cannot evaluate....If we return to our field metaphor, we now find ourselves
in the field without any light whatsoever...., so wecannot establish the height of any point in the field
relative to our own. CD effectively gives us a sense ofbalance, allowing us to the feel the gradient of the field
under our feet.”http://www.robots.ox.ac.uk/~ojw/files/NotesOnCD.pdf
CS 205A: Mathematical Methods Numerical Integration and Differentiation 8 / 34
Page 11
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Quadrature
QuadratureGiven a sampling of n values f (x1), . . . , f (xn),
find an approximation of∫ ba f (x) dx.
xi’s may be fixed or may be chosen by the
algorithm (depends on context)
CS 205A: Mathematical Methods Numerical Integration and Differentiation 9 / 34
Page 12
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Quadrature
QuadratureGiven a sampling of n values f (x1), . . . , f (xn),
find an approximation of∫ ba f (x) dx.
xi’s may be fixed or may be chosen by the
algorithm (depends on context)
CS 205A: Mathematical Methods Numerical Integration and Differentiation 9 / 34
Page 13
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Interpolatory Quadrature
∫ b
a
f(x) dx =
∫ b
a
[∑i
aiφi(x)
]dx
=∑i
ai
[∫ b
a
φi(x) dx
]=∑i
ciai for ci ≡∫ b
a
φi(x) dx
CS 205A: Mathematical Methods Numerical Integration and Differentiation 10 / 34
Page 14
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Riemann Integral
∫ b
a
f(x) = lim∆xk→0
∑k
f(xk)(xk+1 − xk)
≈∑k
f(xk)∆xk
CS 205A: Mathematical Methods Numerical Integration and Differentiation 11 / 34
Page 15
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Quadrature Rules
Q[f ] ≡∑i
wif (xi)
wi describes thecontribution of f (xi)
CS 205A: Mathematical Methods Numerical Integration and Differentiation 12 / 34
Page 16
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Quadrature Rules
Q[f ] ≡∑i
wif (xi)
wi describes thecontribution of f (xi)
CS 205A: Mathematical Methods Numerical Integration and Differentiation 12 / 34
Page 17
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Newton-Cotes Quadrature
xi’s evenly spaced in [a, b] and symmetric
I Closed: includes endpoints
xk ≡ a+(k − 1)(b− a)
n− 1
I Open: does not include endpoints
xk ≡ a+k(b− a)
n+ 1
CS 205A: Mathematical Methods Numerical Integration and Differentiation 13 / 34
Page 18
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Newton-Cotes Quadrature
xi’s evenly spaced in [a, b] and symmetric
I Closed: includes endpoints
xk ≡ a+(k − 1)(b− a)
n− 1
I Open: does not include endpoints
xk ≡ a+k(b− a)
n+ 1
CS 205A: Mathematical Methods Numerical Integration and Differentiation 13 / 34
Page 19
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Midpoint Rule
∫ b
a
f (x) dx ≈ (b− a)f
(a + b
2
)Open
CS 205A: Mathematical Methods Numerical Integration and Differentiation 14 / 34
Page 20
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Trapezoidal Rule
∫ b
a
f (x) dx ≈ (b− a)f (a) + f (b)
2
Closed
CS 205A: Mathematical Methods Numerical Integration and Differentiation 15 / 34
Page 21
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Simpson’s Rule
∫ b
a
f(x) dx ≈ b− a6
(f(a) + 4f
(a+ b
2
)+ f(b)
)Open; from quadratic interpolation
CS 205A: Mathematical Methods Numerical Integration and Differentiation 16 / 34
Page 22
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Composite Rules
Apply rules on subintervals
∆x ≡ b− ak
, xi ≡ a+ i∆x
Composite midpoint:∫ b
a
f(x) dx ≈k∑
i=1
f
(xi+1 + xi
2
)∆x
CS 205A: Mathematical Methods Numerical Integration and Differentiation 17 / 34
Page 23
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Composite Rules
Apply rules on subintervals
∆x ≡ b− ak
, xi ≡ a+ i∆x
Composite midpoint:∫ b
a
f(x) dx ≈k∑
i=1
f
(xi+1 + xi
2
)∆x
CS 205A: Mathematical Methods Numerical Integration and Differentiation 17 / 34
Page 24
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Composite Rules
Composite trapezoid:
∫ b
a
f(x) dx ≈k∑
i=1
(f(xi) + f(xi+1)
2
)∆x
= ∆x
(1
2f(a) + f(x1) + · · ·+ f(xk−1) +
1
2f(b)
)
CS 205A: Mathematical Methods Numerical Integration and Differentiation 18 / 34
Page 25
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Composite Rules
Composite Simpson:
∫ b
a
f(x) dx ≈ ∆x
3
f(a) + 2n−2−1∑i=1
f(x2i) + 4
n/2∑i=1
f(x2i−1) + f(b)
=
∆x
3[f(a) + 4f(x1) + 2f(x2) + · · ·+ 4f(xn−1) + f(b)]
n must be odd!
CS 205A: Mathematical Methods Numerical Integration and Differentiation 19 / 34
Page 26
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Question
Which quadrature rule isbest?
CS 205A: Mathematical Methods Numerical Integration and Differentiation 20 / 34
Page 27
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
On a Single Interval
[On the board.]
I Midpoint and trapezoid:O(∆x3)
I Simpson: O(∆x5)
CS 205A: Mathematical Methods Numerical Integration and Differentiation 21 / 34
Page 28
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Composite
Width of subinterval is O( 1∆x
)
I Midpoint and trapezoid:O(∆x2)
I Simpson: O(∆x4)
CS 205A: Mathematical Methods Numerical Integration and Differentiation 22 / 34
Page 29
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Other Strategies
I Gaussian quadrature: Optimize both wi’s
and xi’s; gets two times the accuracy (but
harder to use!)
I Adaptive quadrature: Choose xi’s where
information is needed (e.g. when quadrature
strategies do not agree)
CS 205A: Mathematical Methods Numerical Integration and Differentiation 23 / 34
Page 30
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Multivariable Integrals I
“Curse of dimensionality”∫Ω
f (~x) d~x,Ω ⊆ Rn
I Iterated integral: Apply one-dimensional
strategy
I Subdivision: Fill with triangles/rectangles,
tetrahedra/boxes, etc.
CS 205A: Mathematical Methods Numerical Integration and Differentiation 24 / 34
Page 31
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Multivariable Integrals II
I Monte Carlo: Randomly draw points in Ω
and average f (~x); converges like 1/√k
regardless of dimension
CS 205A: Mathematical Methods Numerical Integration and Differentiation 25 / 34
Page 32
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Conditioning
|Q[f ]−Q[f ]|‖f − f‖∞
≤ ‖~w‖∞
CS 205A: Mathematical Methods Numerical Integration and Differentiation 26 / 34
Page 33
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Differentiation
I Lack of stabilityI Jacobians vs. f : R→ R
CS 205A: Mathematical Methods Numerical Integration and Differentiation 27 / 34
Page 34
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Differentiation in Basis
f ′(x) =∑i
aiφ′i(x)
φ′i’s basis for derivatives; important for finite
element method!
CS 205A: Mathematical Methods Numerical Integration and Differentiation 28 / 34
Page 35
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Definition of Derivative
f ′(x) ≡ limh→0
f (x + h)− f (x)
h
CS 205A: Mathematical Methods Numerical Integration and Differentiation 29 / 34
Page 36
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
O(h) Approximations
Forward difference:
f ′(x) ≈ f(x+ h)− f(x)
h
Backward difference:
f ′(x) ≈ f(x)− f(x− h)
h
CS 205A: Mathematical Methods Numerical Integration and Differentiation 30 / 34
Page 37
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
O(h) Approximations
Forward difference:
f ′(x) ≈ f(x+ h)− f(x)
h
Backward difference:
f ′(x) ≈ f(x)− f(x− h)
h
CS 205A: Mathematical Methods Numerical Integration and Differentiation 30 / 34
Page 38
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
O(h2) Approximation
Centered difference:
f ′(x) ≈ f(x+ h)− f(x− h)
2h
CS 205A: Mathematical Methods Numerical Integration and Differentiation 31 / 34
Page 39
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
O(h) Approximation of f ′′
Centered difference:
f ′′(x) ≈ f(x+ h)− 2f(x) + f(x− h)
h2
=f(x+h)−f(x)
h− f(x)−f(x−h)
h
h
Geometric interpretation
CS 205A: Mathematical Methods Numerical Integration and Differentiation 32 / 34
Page 40
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Richardson Extrapolation
D(h) ≡ f(x+ h)− f(x)
h= f ′(x) +
1
2f ′′(x)h+O(h2)
D(αh) = f ′(x) +1
2f ′′(x)αh+O(h2)
(f ′(x)f ′′(x)
)=
(1 1
2h
1 12αh
)−1(D(h)D(αh)
)+O(h2)
CS 205A: Mathematical Methods Numerical Integration and Differentiation 33 / 34
Page 41
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Richardson Extrapolation
D(h) ≡ f(x+ h)− f(x)
h= f ′(x) +
1
2f ′′(x)h+O(h2)
D(αh) = f ′(x) +1
2f ′′(x)αh+O(h2)
(f ′(x)f ′′(x)
)=
(1 1
2h
1 12αh
)−1(D(h)D(αh)
)+O(h2)
CS 205A: Mathematical Methods Numerical Integration and Differentiation 33 / 34
Page 42
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Richardson Extrapolation
D(h) ≡ f(x+ h)− f(x)
h= f ′(x) +
1
2f ′′(x)h+O(h2)
D(αh) = f ′(x) +1
2f ′′(x)αh+O(h2)
(f ′(x)f ′′(x)
)=
(1 1
2h
1 12αh
)−1(D(h)D(αh)
)+O(h2)
CS 205A: Mathematical Methods Numerical Integration and Differentiation 33 / 34
Page 43
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Choosing h
I Too big: Bad approximation of f ′
I Too small: Numerical issues
(h small, f (x) ≈ f (x + h))
Next
CS 205A: Mathematical Methods Numerical Integration and Differentiation 34 / 34
Page 44
Introduction Quadrature Newton-Cotes Accuracy More Quadrature Differentiation
Choosing h
I Too big: Bad approximation of f ′
I Too small: Numerical issues
(h small, f (x) ≈ f (x + h))
Next
CS 205A: Mathematical Methods Numerical Integration and Differentiation 34 / 34