Chapter
Chapter
ESOF 3558
Numerical Methods & Modeling
Part 5
Chapters 21: Numerical Differentiation
All images copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter Objectives
• High-accuracy numerical differentiation formulas
• Numerical differentiation of equally spaced data
• Numerical differentiation of unequally spaced data
• Richardson extrapolation of numerical differentiation
• Sensitivity of numerical differentiation to data error
• MATLAB diff and gradient differentiation functions
1
2025-01-21
Numerical Differentiation
• Numerical differentiation formulas from
Chapter 4:
• Forward difference:
𝑓 𝑥 𝑓 𝑥
𝑓 𝑥 𝑂 ℎ
ℎ
• Backward difference:
𝑓 𝑥 𝑓 𝑥
𝑓 𝑥 𝑂 ℎ
ℎ
• Centered difference:
𝑓 𝑥 𝑓 𝑥
𝑓 𝑥 𝑂 ℎ
2ℎ
2
2025-01-21
Forward Finite-Difference
Backward Finite-Difference
3
2025-01-21
Centered Finite-Difference
h h h=0.25 h
𝑥 𝑥 𝑥 0.5 𝑥 𝑥
8
4
2025-01-21
Richardson Extrapolation
• Richardson extrapolation combines two derivative estimates to produce a more
accurate estimate.
• Combining two 𝑂 ℎ estimates, and halving the interval (ℎ ℎ /2), produces
an 𝑂 ℎ estimate:
4 1
𝐷 𝐷 ℎ 𝐷 ℎ
3 3
• Combining two 𝑂 ℎ estimates, and halving the interval (ℎ ℎ /2), produces
an 𝑂 ℎ estimate:
16 1
𝐷 𝐷 ℎ 𝐷 ℎ
15 15
• Combining two 𝑂 ℎ estimates, and halving the interval (ℎ ℎ /2), produces
an 𝑂 ℎ estimate:
64 1
𝐷 𝐷 ℎ 𝐷 ℎ
63 63 10
5
2025-01-21
𝐷 𝐷 ℎ 𝐷 ℎ
12
6
2025-01-21
such cases.
𝑡 𝑡 𝑡 𝑡 𝑡
13
• Example:
• Fit a second-degree Lagrange polynomial to three data x1 x2 x3
points and compute its derivative to obtain:
𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥
𝑓 𝑥 𝑓 𝑥 𝑓 𝑥 𝑓 𝑥
𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥
2𝑥 𝑥 𝑥 2𝑥 𝑥 𝑥 2𝑥 𝑥 𝑥
𝑓′ 𝑥 𝑓 𝑥 𝑓 𝑥 𝑓 𝑥
𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥 𝑥
14
7
2025-01-21
15
Partial Derivatives
• Partial derivative 𝑓 𝑥, 𝑦 along a single dimension
• Definition using calculus:
𝜕𝑓 𝑓 𝑥 Δ𝑥, 𝑦 𝑓 𝑥, 𝑦
lim
𝜕𝑥 → Δ𝑥
• Numerically:
Numerical Technique Formula
Forward Difference 𝜕𝑓 𝑓 𝑥 Δ𝑥, 𝑦 𝑓 𝑥, 𝑦
𝜕𝑥 Δ𝑥
8
2025-01-21
Partial Derivatives
• Partial derivative 𝑓 𝑥, 𝑦 using centred differences:
𝜕𝑓 𝑓 𝑥 Δ𝑥, 𝑦 𝑓 𝑥 Δ𝑥, 𝑦 𝜕𝑓 𝑓 𝑥, 𝑦 Δ𝑦 𝑓 𝑥, 𝑦 Δ𝑦
𝜕𝑥 2Δ𝑥 𝜕𝑦 2Δ𝑦
• For mixed partial derivative, take the partial derivative with respect to
each variable.
𝜕 𝑓 𝜕 𝜕𝑓 𝜕 𝑓 𝑥 Δ𝑥, 𝑦 𝑓 𝑥 Δ𝑥, 𝑦
𝜕𝑦𝜕𝑥 𝜕𝑦 𝜕𝑥 𝜕𝑦 2Δ𝑥
𝜕 𝜕
𝜕 𝑓 𝑓 𝑥 Δ𝑥, 𝑦 𝑓 𝑥 Δ𝑥, 𝑦
𝜕𝑦 𝜕𝑦
𝜕𝑦𝜕𝑥 2Δ𝑥
17
18
9
2025-01-21
19
20
10
2025-01-21
Visualization
• MATLAB can generate contour plots of functions as well as vector
fields.
• Assuming x and y represent a mesh grid of x and y values and z
represents a function of x and y,
• contour(x, y, z) can be used to generate a contour plot
• [fx, fy]=gradient(z,h) can be used to generate partial derivatives
and
• quiver(x, y, fx, fy) can be used to generate vector fields
21
11