Part7 Differentiation
Part7 Differentiation
Numerical Methods
7. Differentiation
These presentations cannot be used and/or modified without the permission of the authors.
Ankara, Turkey
[email protected]
Numerical Differentiation
X 100
Velocity, Vx (inches/s)
Time (s)
2
Data and video: http://rockyroer.blogspot.com.tr/2011/11/crash-test-data.html
Derivative: Rate of change of a dependent variable with respect to an independent variable.
f(x)
df f ( x i + Dx ) - f ( x i ) tangent
f = f (x ) ® = lim
dx Xi Dx ®0 Dx slope = f ¢(xi)
x
xi
f(x)
df Df f ( x i + Dx ) - f ( x i ) Df
» =
dx Xi Dx Dx
Dx
x
xi
f (x i + 1 ) - f (x i ) f ¢¢(x) f (x i + 1 ) - f (x i )
f ¢( x i ) = - h = - O(h)
h 2! h
• Backward differencing (use 1st order TSE of f(xi-1) around xi. Call this TSE2)
f ¢¢(x) 2
f ( x i - 1 ) = f ( x i ) - f ¢( x i ) h + h where h = xi – xi-1
2!
f (x i ) - f (x i - 1 ) f ¢¢(x) f (x i ) - f (x i - 1 )
f ¢( x i ) = + h = + O(h)
h 2! h
• Centered differencing (use TSE1 – TSE2. But consider 2nd order terms also.)
f ¢¢(x i ) 2
f (x i + 1 ) = f (x i ) + f ¢(x i ) h + h + O(h 3 ) f (x i + 1 ) - f (x i - 1 )
2! f ¢( x i ) = + O(h2 )
f ¢¢(x i ) 2 2h
f ( x i - 1 ) = f ( x i ) - f ¢( x i ) h + h - O(h 3 )
2!
• Forward and backward difference formulas are first order, O(h), accurate. That is the error drops
approximately by a factor of 2 as the step size h drops to h/2.
• Centered difference formula is second order, O(h2). Error drops by a factor of 4 as h drops to h/2.
• Centered difference formula uses the same number of arithmetic operations as forward and
backward formulas, and it offers better accuracy. Therefore it is more efficient.
Example 1: Position of a body moving in a straight path is shown below. Find its velocity.
• To derive them use proper combinations of TSE of f(xi+1), f(xi-1), f(xi+2), f(xi-2)
- f ( x i + 2 ) + 4 f ( x i + 1 ) - 3f ( x i )
• Forward differencing f ¢( x i ) = - O(h2 )
2h
3f ( x i ) - 4 f ( x i - 1 ) + 3f ( x i - 2 )
• Backward differencing f ¢( x i ) = + O(h2 )
2h
- f (x i + 2 ) + 8f (x i + 1 ) - 8f (x i - 1 ) + f (x i - 2 )
• Centered differencing f ¢( x i ) = + O(h 4 )
12 h
• See Section 23.1 in Chapra and Canale (2010) for even more higher order formulas.
Exercise 3: Solve the previous example using the above formulas. Note that for t=0.0 and 0.1
forward differencing must be used. For t=0.4 and 0.5 backward differencing is suitable. Centered
differencing can only be used for t=0.2 and 0.3.
Formulas for the second derivative
f (x i + 2 ) - 2f (x i + 1 ) + f (x i )
• Forward differencing f ¢¢(x i ) = 2
+ O(h)
h
f (x i ) - 2f (x i - 1 ) + f (x i - 2 )
• Backward differencing f ¢¢(x i ) = + O(h)
h2
f (x i + 1 ) - 2f (x i ) + f (x i - 1 )
• Centered differencing f ¢¢(x i ) = + O(h2 )
2
h
• See Section 23.1 for formulas for the 3rd and 4th derivatives.
Exercise 4: Derive the above formulas. Use proper combinations of TSE of f(xi+1), f(xi-1), f(xi+2)
and f(xi-2).
Exercise 5: Use the table given for the first example to calculate the acceleration of the particle.
You can either use the second derivative formulas, or use the first derivative formulas with the
previously calculated velocities. Compare and comment on the results.
How can we improve the derivative estimates?
• Use smaller h values.
• Use higher order approximations.
Exercise 6: The following two tables are for the function f(x) = ex. They use step sizes of 0.2
and 0.1. Calculate the first derivative of the function using O(h2) estimates. Calculate true errors.
Compare the results.
D1 + E1 = D2 + E2
D 2 - D1
Combine this with D2 D » D2 +
( h1 h2 )
2
- 1
D 2 - D1 4 1
A special case of h2 = h1/2 results in D » D2 + = D 2 - D1
3 3 3
Example 2:
Use Richardson Extrapolation to estimate the first derivative of sin(x) at x=p/4 using step sizes of
h1=p/3 and h2=p/6. Use centered differences of O(h2).
4 1
Apply Richardson Extrapolation f ¢(p/4) » 0.675237237 - 0.584772601 = 0.70539215
3 3
|et| = 0.24 %
Romberg Algorithm
Apply multiple Richardson Extrapolation one after the other until the error falls below a specified
tolerance.
Exercise 6: In the above example use h3=p/12 to calculate D3. Combine D1 with D2, and D2 with
D3 to get two O(h4) estimates. Then combine these two estimates to get an estimate of order
O(h6).
Partial Derivatives
The formulation is similar to ordinary derivatives.
¶f f ( x, y + Dy ) - f ( x, y - Dy )
=
¶y 2 Dy
¶f ¶f
( x + Dx, y ) - ( x - Dx, y )
¶2 f ¶ æ ¶f ö ¶y ¶y
= ç ÷=
¶x¶y ¶x è ¶y ø 2 Dx
¶2 f f ( x + Dx, y + Dy ) - f ( x + Dx , y - Dy ) - f ( x - Dx , y + Dy ) + f ( x - Dx , y - Dy )
=
¶x¶y 4 Dx Dy
Differentiating and Integrating Data with Errors