0% found this document useful (0 votes)
14 views23 pages

Numerical Differentiation

This document discusses numerical differentiation methods, including high-accuracy formulas for equispaced and unequally spaced data, and the application of Richardson extrapolation. It also highlights the sensitivity of numerical differentiation to data errors and provides MATLAB functions for evaluating derivatives. Several examples illustrate the concepts and calculations involved in numerical differentiation.

Uploaded by

Topon Chowdhury
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views23 pages

Numerical Differentiation

This document discusses numerical differentiation methods, including high-accuracy formulas for equispaced and unequally spaced data, and the application of Richardson extrapolation. It also highlights the sensitivity of numerical differentiation to data errors and provides MATLAB functions for evaluating derivatives. Several examples illustrate the concepts and calculations involved in numerical differentiation.

Uploaded by

Topon Chowdhury
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Part 5

Chapter 19

Numerical Differentiation

Prof. Hae
Hae--Jin Choi

[email protected]

School of Mechanical Engineering


Chung-Ang University
Numerical Methods 2010-2 1
Chapter Objectives
l Understanding the application of high-accuracy numerical
differentiation formulas for equispaced data.
l Knowing how to evaluate derivatives for unequally spaced data.
l Understanding how Richardson extrapolation is applied for
numerical differentiation.
l Recognizing the sensitivity of numerical differentiation to data
error.
l Knowing how to evaluate derivatives in MATLAB with the diff and
gradient functions.
l Knowing how to generate contour plots and vector fields with
MATLAB.

School of Mechanical Engineering


Chung-Ang University
Numerical Methods 2010-2 2
Introduction to Differentiation
The one dimensional forms of some constitutive laws commonly used
Physical Proportional
Law Equation Gradient Flux
Area constat
Fourier’s dT Thermal
q = -k Heat conduction Temperature Heat
law dx conductivity
Fick’s dc
J = -D Mass diffusion Concentration Mass Diffusivity
law dx

D’Arcy’ dh Flow through Hydraulic


q = -k Head Flow
law dx porous media conductivity
Ohm’s dV Electrical
J = -s Current flow Voltage Current
law dx conductivity
Newton’s Dynamic
du Shear
viscosity t = -m Fluids Velocity
dx Stress Viscosity
law
Hooke’s DL Young’s
s =E Elasticity Deformation Stress
law L modulus
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 3
Differentiation
l The mathematical definition of a derivative begins with a
difference approximation:
Dy f (xi + Dx) - f (xi )
=
Dx Dx
and as Δx is allowed to approach zero, the difference
becomes a derivative:
dy� f (xi + Dx) - f (xi )
= lim
dx Dx®0 Dx

School of Mechanical Engineering


Numerical Methods 2010-2 4 Chung-Ang University
High--Accuracy Differentiation
High
Formulas

l Taylor series expansion can be used to


generate high-accuracy formulas for
derivatives by using linear algebra to
combine the expansion around several
points.
l Three categories for the formula include
forward finite-difference, backward finite-
difference, and centered finite-difference.

School of Mechanical Engineering


Chung-Ang University
Numerical Methods 2010-2 5
Differentiation derived
from Taylor series expansions
l There are forward difference, backward difference and centered
difference approximations, depending on the points used:
l Forward:

' f (x i+1 ) - f (x i )
f (x i ) = + O(h)
h
l Backward:
' f (x i ) - f (x i-1 )
f (x i ) = + O(h)
� h
l Centered:
' f (x i+1 ) - f (x i-1 )
f (x i ) = + O(h 2 )
� 2h
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 6
High Accuracy Differentiation
l Forward Taylor series expansion
f ¢¢( xi ) 2
f ( xi +1 ) = f ( xi ) + f ¢( xi )h + h +L
2!

f ( xi +1 ) - f ( xi ) f ¢¢( xi )
f ¢( xi ) = - h + O(h 2 )
h 2!
l Forward-difference approximation of 1st derivative excluding the
second and higher derivative term (In chapter 4)
f ( xi +1 ) - f ( xi )
¢
f ( xi ) = + O ( h)
h
l Forward-difference approximation of 2nd derivative
f ( xi + 2 ) - 2 f ( xi +1 ) + f ( xi )
f ¢¢( xi ) = 2
+ O ( h)
h
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 7
High Accuracy Differentiation
l Forward-difference approximation of 1st derivative including
2nd derivative term

f ( xi +1 ) - f ( xi ) f ( xi + 2 ) - 2 f ( xi +1 ) + f ( xi ) 2
f ¢( xi ) = - 2
h + O ( h )
h 2h

- f ( xi + 2 ) + 4 f ( xi +1 ) - 3 f ( xi )
f ¢( xi ) = + O(h 2 )
2h
l Notice that inclusion of second-derivative term has
improved the accuracy to O(h2) .

School of Mechanical Engineering


Chung-Ang University
Numerical Methods 2010-2 8
Forward Finite-
Finite-Difference

School of Mechanical Engineering


Chung-Ang University
Numerical Methods 2010-2 9
Backward Finite-
Finite-Difference

School of Mechanical Engineering


Chung-Ang University
Numerical Methods 2010-2 10
Centered Finite-
Finite-Difference

School of Mechanical Engineering


Chung-Ang University
Numerical Methods 2010-2 11
Example 19.1 (1/2)
l Q. Recall that at in Ex. 4.4 we estimated the derivative of f(x) at
x=0.5 using forward differences and a step size of h=0.25. The
results are summarized in the table below. The exact value of
f’(0.5)= -0.9125.
f ( x) = -0.1x 4 - 0.15 x 3 - 0.5 x 2 - 0.25 x + 1.2

Backward O(h) Centered O(h2) Forward O(h)


Estimate -0.714 -0.934 -1.155

et 21.7% -2.4% -26.5%

l Repeat the computation with high accuracy formulas.

School of Mechanical Engineering


Chung-Ang University
Numerical Methods 2010-2 12
Example 19.1 (2/2)
l Sol) xi -2 = 0 f ( xi - 2 ) = 1.2
xi -1 = 0.25 f ( xi -1 ) = 1.1035156
xi = 0.5 f ( xi ) = 0.925
xi +1 = 0.75 f ( xi +1 ) = 0.6363281
xi + 2 = 1 f ( xi + 2 ) = 0.2

l Forward difference of O(h2) is computed as


-0.2 + 4(0.6363281) - 3(0.925)
f ¢(0.5) = = -0.859375 e t =5.82 %
2(0.25)
l Backward difference of O(h2) is computed as
3(0.925) - 4(1.1035156) + 1.2
f ¢(0.5) = = -0.878125 e t =3.77 %
2(0.25)
l Backward difference of O(h4) is computed as
-0.2 + 8(0.6363281) - 8(1.1035156) + 1.2
f ¢(0.5) = = -0.9125 e t =0 %
12(0.25)
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 13
Richardson Extrapolation
l As with integration, the Richardson extrapolation can be used to combine
two lower-accuracy estimates of the derivative to produce a higher-
accuracy estimate.
l 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
D = D(h2 ) - D(h1 )
3 3
l 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
� D = D(h2 ) - D(h1 )
15 15
l 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
� D= D(h2 ) - D(h1 )
63 63
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 14
Example 19.2
l Q. Using the same function as in Ex.19.1, estimate the first
derivative at x=0.5 for a step size of h1=0.5, and h2=0.25. Use
the Richardson extrapolation to compute improved estimate.
The exact solution is -0.9125.
f ( x) = -0.1x 4 - 0.15 x 3 - 0.5 x 2 - 0.25 x + 1.2
Sol.) The first derivative with centered difference xi - 2 = 0 f ( xi - 2 ) = 1.2
0.2 - 1.2 xi -1 = 0.25 f ( xi -1 ) = 1.1035156
D (0.5) = = -1.0 e t = - 9.6%
1 xi = 0.5 f ( xi ) = 0.925
0.6363281 - 1.103516
D (0.25) = = -0.934375 e t = - 2.4% xi +1 = 0.75 f ( xi +1 ) = 0.6363281
0.5
xi + 2 = 1 f ( xi + 2 ) = 0.2
Using the Richardson extrapolation, the improved Estimate is

4 1
D= (-0.934375) - (-1) = -0.9125
3 3
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 15
Unequally Spaced Data
• One way to calculated derivatives of
unequally spaced data is to determine a
polynomial fit and take its derivative at a
point.
• As an example, using a second-order
Lagrange polynomial to fit three points and
taking its derivative yields:
2x - x1 - x2 2x - x0 - x2 2x - x0 - x1
f ¢(x) = f (x0 ) + f (x1 ) + f (x2 )
(x0 - x1 )(x0 - x2 ) (x1 - x0 )(x1 - x2 ) (x2 - x0 )(x2 - x1 )

School of Mechanical Engineering


Chung-Ang University
Numerical Methods 2010-2 16

Example 19.3
l A temperature is measured inside the soil as shown below.
Compute the heat flux into the ground at the air-soil interface.
dT
q ( z = 0) = - k
dz z =0

where q(x)=heat flux (W/m2),


k=thermal conductivity for soil (=0.5 W/(m·K),
T=Temperature(K),
z=distance measured from the surface
into the soil.
2(0) - 0.0125 - 0.0375 2(0) - 0 - 0.0375
f ¢ ( 0 ) = 13.5 + 12
( 0 - 0.0125)( 0 - 0.0375) ( 0.0125 - 0 )( 0.0125 - 0.0375)
2(0) - 0 - 0.0125
+10 = -1440 + 1440 - 133.333 = -133.333 K / m
( 0.0375 - 0 )( 0.0375 - 0.0125)
W æ Wö W
q ( z = 0) = -0.5 ç -133.333 ÷ = 66.667
mKè mø m2 School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 17
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.
(a) Data with no error
(b) Resulting numerical
differentiation of curve (a)
(c) Data modified slightly
(d) Resulting numerical
differentiation of curve (a)

à Small data errors are amplified


by numerical differentiation.
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 18
Numerical Differentiation with
MATLAB
• MATLAB has two built-in functions to help take derivatives, diff
and gradient:
• diff(x)
– Returns the difference between adjacent elements in x

>> f = @(x) 0.2+25*x-200*x.^2+675*x.^3-900*x.^4+400*x.^5;


>> x = 0 : 0.1 : 0.8 ;
>> y = f(x) ;

>> diff(x)
ans =
Columns 1 through 5
0.1000 0.1000 0.1000 0.1000 0.1000
Columns 6 through 8
0.1000 0.1000 0.1000
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 19
Numerical Differentiation with
MATLAB
• diff(y)./diff(x)
– Returns the difference between adjacent values in y divided
by the corresponding difference in adjacent values of x

>> d=diff(y)./diff(x)
Columns 1 through 5
10.8900 -0.0100 3.1900 8.4900 8.6900
Columns 6 through 8
1.3900 -11.0100 -21.3100

>> n=length(x);
>> xm=(x(1:n-1)+x(2:n))./2;
% vector d contains derivative estimates corresponding to the
% midpoint between adjacent elements
>> xa=0: .01 : .8 ;
>> ya=25-400*xa+3*675*xa.^2-4*900*xa.^3+5*400*xa.^4;
>> xplot(xm, d, 'o', xa, ya)

School of Mechanical Engineering


Chung-Ang University
Numerical Methods 2010-2 20
School of Mechanical Engineering
Chung-Ang University
Numerical Methods 2010-2 21
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)

>> dy=gradient(y, 0.1)


dy =
Columns 1 through 5
10.8900 5.4400 1.5900 5.8400 8.5900
Columns 6 through 8
5.0400 -4.8100 -16.1600 -21.3100

School of Mechanical Engineering


Chung-Ang University
Numerical Methods 2010-2 22
>> xa=0: .01 : .8 ;
>> ya=25-400*xa+3*675*xa.^2-4*900*xa.^3+5*400*xa.^4;
>> xplot(x, dy, 'o', xa, ya)

School of Mechanical Engineering


Chung-Ang University
Numerical Methods 2010-2 23

You might also like