Lecture 16_Chapter_20_diffrentiation(1)-已轉檔
Lecture 16_Chapter_20_diffrentiation(1)-已轉檔
with MATLAB®
for Engineers and Scientists
4th Edition
Steven C. Chapra
©McGraw-Hill Education. All rights reserved. Authorized only for instructor use in the classroom. No reproduction or further distribution permitted without the prior written consent of McGraw-Hill Education.
Part 5
Chapter 21
Numerical Differentiation
©McGraw-Hill Education. All rights reserved. Authorized only for instructor use in the classroom. No reproduction or further distribution permitted without the prior written consent of McGraw-Hill Education.
Chapter Objectives
Understanding the application of high-accuracy numerical
differentiation formulas for equispaced data.
Knowing how to evaluate derivatives for unequally spaced
data.
Understanding how Richardson extrapolation is applied for
numerical differentiation.
Recognizing the sensitivity of numerical differentiation to
data error.
Knowing how to evaluate derivatives in MATLAB with the
diff and gradient functions.
Knowing how to generate contour plots and vector fields
with MATLAB.
©McGraw-Hill Education.
Differentiation
The mathematical definition of a derivative begins with a
difference approximation:
∆𝑦 𝑓 𝑥𝑖 + ∆𝑥 − 𝑓(𝑥𝑖)
=
∆𝑥 ∆𝑥
and as x is allowed to approach zero, the difference
becomes a derivative:
𝑑𝑦 𝑓 𝑥𝑖 + ∆𝑥 − 𝑓(𝑥𝑖)
= lim
𝑑𝑥 ∆𝑥→0 ∆𝑥
©McGraw-Hill Education.
High-Accuracy Differentiation
Formulas
Taylor series expansion can be used to
generate high-accuracy formulas for
derivatives by using linear algebra to combine
the expansion around several points.
©McGraw-Hill Education.
Forward Finite-Difference
©McGraw-Hill Education.
Backward Finite-Difference
©McGraw-Hill Education.
Centered Finite-Difference
©McGraw-Hill Education.
Richardson Extrapolation
As with integration, the Richardson extrapolation can be used to
combine two lower-accuracy estimates of the derivative to produce a
higher-accuracy estimate.
For the cases where there are two O(h2) estimates and the interval is
halved (h2=h1/2), an improved O(h4) estimate may be formed using:
4 1
𝐷 = 𝐷 ℎ2 − 𝐷(ℎ1)
3 3
For the cases where there are two O(h4) estimates and the interval is
halved (h2=h1/2), an improved O(h6) estimate may be formed using:
16 1
𝐷= 𝐷 ℎ2 − 𝐷(ℎ1)
15 15
For the cases where there are two O(h6) estimates and the interval is
halved (h2=h1/2), an improved O(h8) estimate may be formed using:
64 1
𝐷= 𝐷 ℎ2 − 𝐷(ℎ1)
63 63
©McGraw-Hill Education.
Unequally Spaced Data
One way to calculat derivatives of unequally
spaced data is to determine a polynomial fit
and take its derivative at a point.
©McGraw-Hill Education.
Derivatives and Integrals for Data
with Errors
A shortcoming of numerical differentiation is that it tends to
amplify errors in data, whereas integration tends to smooth
data errors.
One approach for taking derivatives of data with errors is to
fit a smooth, differentiable function to the data and take the
derivative of the function.
©McGraw-Hill Education.
Numerical Differentiation with
MATLAB, 1
MATLAB has two built-in functions to help
take derivatives, diff and gradient:
diff(x)
• Returns the difference between adjacent
elements in x
diff(y)./diff(x)
• Returns the difference between adjacent values
in y divided by the corresponding difference in
adjacent values of x
©McGraw-Hill Education.
Numerical Differentiation with
MATLAB: DIFF command
X = [1 1 2 3 5 8 13 21]; X = [0 5 15 30 50 75 105];
Y = diff(X) Y = diff(X,2)
Y=
Y=
0 1 1 2 3 5 8 5 5 5 5 5
©McGraw-Hill Education.
Numerical Differentiation with
MATLAB: DIFF command
X =[1 2 4 6 7 9]
Y= [1 4 16 36 49 81];
Z = diff(Y)./diff(X)
Z=
3 6 10 13 16
©McGraw-Hill Education.
Numerical Differentiation with
MATLAB
fx = gradient(f, h)
Determines the derivative of the data in f at each of
the points. The program uses forward difference for
the first point, backward difference for the last point,
and centered difference for the interior points. h is
the spacing between points; if omitted h=1.
The major advantage of gradient over diff is
gradient’s result is the same size as the original
data.
Gradient can also be used to find partial derivatives
for matrices:
[fx, fy] = gradient(f, h)
©McGraw-Hill Education.
Visualization
MATLAB can generate contour plots of functions as
well as vector fields. Assuming x and y represent a
meshgrid 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
©McGraw-Hill Education.
Gradient Example
x = -2:0.2:2;
y = x';
z = x.^2 +y.^2;
figure
contour(x,y,z)
[px,py] = gradient(z);
hold on
quiver(x,y,px,py)
hold off
©McGraw-Hill Education.
Example
Employ the gradient function to determine to partial derivatives
for the following two-dimensional function:
𝑓 𝑥, 𝑦 = 𝑦 − 𝑥 − 2𝑥2 − 2𝑥𝑦 − 𝑦2
from x = -2 to 2 and y = 1 to 3. Then use quiver to superimpose a
vector field on a contour plot of the function.
Script:
clear, clc
f=@(x,y) y-x-2*x.^2-2.*x.*y-y.^2;
[x,y]=meshgrid(-2:.25:0, 1:.25:3);
z=f(x,y);
[fx,fy]=gradient(z,0.25);
cs=contour(x,y,z,'b');clabel(cs);
ylim([1 3]),xlim([-2 0])
hold on,quiver(x,y,-fx,-fy);hold off
©McGraw-Hill Education.
Results
©McGraw-Hill Education.