Вы находитесь на странице: 1из 37

Diferenciación

Numérica

Dr. Luis Sanchez


Motivation

• How do you evaluate the Time Displacement


(second) (meters)
derivative of a tabulated
function. 0 30.1

• How do we determine the 5 48.2


velocity and acceleration from
tabulated measurements. 10 50.0

15 40.2
Taylor’s Expansion

1
 
f ( x  h)  f ( x )  f ( x ) h  f ( x ) h  O ( h )
2 3

2
1
f ( x)h  f ( x  h)  f ( x)  f ( x)h 2  O(h 3 )
2
f ( x  h)  f ( x ) 1
f ( x)   f ( x)h  O(h 2 )
h 2
Forward Difference Formula for f (x)

f ( x  h)  f ( x )
f ( x)  error  O(h)
h
Geometrically
f (x)

f (x)
f ( x  h)  f ( x)
h

x xh
Backward Difference Formula for f (x)
Similarly

1
f ( x  h)  f ( x)  f ( x)h  f ( x)h 2  O(h3 )
2
Geometrically

f ( x )  f ( x  h)
f ( x)  f (x)
h
error  O(h) f ( x)  f ( x  h)
h

xh x x
Central Difference Formula for f (x)

1 1 1
f ( x  h)  f ( x)  f ( x)h  f ( x)h 2  f ( x)h3  f ( 4) ( x)h 4  O(h5 )
2 6 4!
1 1 1
–) f ( x  h)  f ( x)  f ( x)h  f ( x)h 2  f ( x)h3  f ( 4) ( x)h 4  O(h5 )
2 6 4!
1
f ( x  h)  f ( x  h)  2hf ( x)  f ( x)h3  O( h 5 )
3
1
2hf ( x)  f ( x  h)  f ( x  h)  f ( x)h3  O(h5 )
3
Central Difference Formula for f (x)

f ( x  h)  f ( x  h )
f ( x) 
2h
Geometrically
error  O(h )2

f (x)

f (x)
f ( x  h)  f ( x  h)
2h

xh x xh x
First Derivatives - Example:
Use forward ,backward and centered difference
approximations to estimate the first derivative of
f ( x )  0.1 x 4  0.15 x 3  0.5 x 2  0.25 x  1.2
at x = 0.5 with h = 0.5 and 0.25 (exact sol. = -0.9125)
• Forward Difference
 f (1)  f (0.5) 0.2  0.925
 
h  0.5, f (0.5)    1.45,  t  58.9%
 1  0.5 0.5

h  0.25, f (0.5)  f (0.75)  f (0.5)  0.63632813  0.925  1.155,   26.5%

 0.75  0.5 0.25
t

• Backward Difference
 f (0.5)  f (0) 0.925  1.2
 
h  0.5, f (0.5)    0.55,  t  39.7%
 0.5  0 0.5

h  0.25, f (0.5)  f (0.5)  f (0.25)  0.925  1.10351563  0.714,   21.7%

 0.5  0.25 0.25
t
First Derivatives - Example:

• Central Difference

f (1)  f (0) 0.2  1.2


h  0.5, f (0.5)    1.0,  t  9.6%
1 0 1

f (0.75)  f (0.25)
h  0.25, f (0.5)  
0.75  0.25
0.63632813  1.10351563
 0.934,  t  2.4%
0.5
Example
• Remarks:

• FD, BD, CD each involves 2 function calls, 1 subtraction,


and 1 division: same computation time
• CD is the most accurate (hence, the most recommended
method)
• However, sometimes, CD cannot be applied
Three Formula Summary:
Forward Difference Formula for f (x)
1 1
f ( x  2h)  f ( x)  f ( x)2h  f ( x)(2h) 2  f ( x)(2h)3  O(h 4 )
2 6
1 1

–2) f ( x  h)  f ( x)  f ( x)h  f ( x)h  f ( x)h3  O(h 4 )

 2

2 6
1 4 1
f ( x  2h)  2 f ( x  h)   f ( x)  f ( x)h 2 [2  2  ]  f ( x)h3[  ]  O(h 4 )
2 3 3
f ( x)h2  f ( x  2h)  2 f ( x  h)  f ( x)  f ( x)h3  O(h4 )

f ( x  2 h )  2 f ( x  h)  f ( x )
f ( x) 
h2
error  O(h)
Backward Difference Formula for f (x)
1 1
f ( x  2h)  f ( x)  f ( x)2h  f ( x)(2h)  f ( x)(2h)3  O(h 4 )
 2

2 6
–2) f ( x  h)  f ( x)  f ( x)h  1 f ( x)h 2  1 f ( x)h3  O(h 4 )
2 6
1 3 4 1
 

f ( x  2h)  2 f ( x  h)   f ( x)  f ( x)h [2  2  ]  f ( x)h [  ]  O(h 4 )
2

2 3 3
f ( x)h2  f ( x  2h)  2 f ( x  h)  f ( x)  f ( x)h3  O(h4 )

f ( x )  2 f ( x  h)  f ( x  2h)
f ( x)  2
h
error  O(h)
Central Difference Formula for f (x)
1 1 1
f ( x  h)  f ( x)  f ( x)h  f ( x)h 2  f ( x)h3  f ( 4) ( x)h 4  O(h5 )
2 6 4!
1 1 1
+) f ( x  h)  f ( x)  f ( x)h  f ( x)h 2  f ( x)h3  f ( 4) ( x)h 4  O(h5 )
2 6 4!
1 ( 4)
f ( x  h)  f ( x  h)  2 f ( x)  f ( x)h 2  f ( x ) h 4  O( h 6 )
12

f ( x  h)  f ( x  h)  2 f ( x)
f ( x)  2
 O ( h 2
)
h

Similar remark on the selection of FD|BD|CD applies for f”(x)


Numerical Differentiation
1. Forward difference
Taylor series :

f xi 1   f xi   f xi h  

f ( xi 1 )  f ( xi )
f ( xi ) 
/

h
f /
( x )  f /
( xi )
f ( xi ) 
// i 1
h
f ( xi  2 )  2 f ( xi 1 )  f ( xi )

h2
f ( xi 3 )  3 f ( xi 2 )  3 f ( xi 1 )  f ( xi )
f ( xi ) 
///

h3
Numerical Differentiation

2. Backward difference
f ( xi )  f ( xi 1 )
f ( xi ) 
/

h
f /
( x )  f /
( xi 1 )
f ( xi ) 
// i
h
f ( xi )  2 f ( xi 1 )  f ( xi 2 )

h2
f ( xi )  3 f ( xi 1 )  3 f ( xi 2 )  f ( xi 3 )
f ( xi ) 
///

h3
Numerical Differentiation
3. Centered difference

f ( xi 1 )  f ( xi 1 )
f ( xi ) 
/

2h
f ( xi 1 )  2 f ( xi )  f ( xi 1 )
f ( xi ) 
//

h2

f ( xi 2 )  2 f ( xi 1 )  2 f ( xi 1 )  f ( xi 2 )
f ( xi ) 
///

2h 3
High Accuracy Differentiation
Formulas
• High-accuracy finite-difference formulas can be
generated by including additional terms from the Taylor
series expansion.

• An example: High-accuracy forward-difference formula


for the first derivative.
More Accurate FD Formula for f (x)
1
f ( x  h)  f ( x)  f ( x)h  f ( x)h 2  O(h3 )
2
1 f ( x  2h)  2 f ( x  h)  f ( x)
f ( x  h)  f ( x)  f ( x)h  h 2 [ 2
 O ( h)]  O ( h 3
)
2 h
1 1
 f ( x)  f ( x)h  f ( x  2h)  f ( x  h)  f ( x)  O(h3 )
2 2
1 3
 f ( x  2h)  2 f ( x  h)  f ( x )
f ( x)  2 2  O( h 2 )
h

 f ( x  2h)  4 f ( x  h)  3 f ( x)
f ( x)   O( h 2 )
2h
More Accurate FD Formula (cont)

• Better accuracy can be achieved using this formula


• But, it involves more computations:
• 3 function calls, two +/–, one division
• Trade-off:
• More computation is the price you paid for better
accuracy
• Similar idea applies to more accurate BD formula.
• Similar improved versions can be developed for the
backward and centered formulas as well as for the
approximations of the higher derivatives.
Higher Order Forward Divided Difference

 f ( xi 2 )  4 f ( xi 1 )  3 f ( xi )
f ( xi ) 
/

2h

 f ( xi 3 )  4 f ( xi  2 )  5 f ( xi 1 )  2 f ( xi )
f ( xi ) 
//

h2

f /// ( xi ) 
 3 f ( xi 4 )  14 f ( xi 3 )  24 f ( xi  2 )  18 f ( xi 1 )  5 f ( xi )
2h 3
Higher Order Backward Divided Difference

3 f ( xi )  4 f ( xi 1 )  f ( xi 2 )
f ( xi ) 
/

2h

2 f ( xi )  5 f ( xi 1 )  4 f ( xi 2 )  f ( xi 3 )
f ( xi ) 
//

h2

f /// ( xi )
5 f ( xi )  18 f ( xi 1 )  24 f ( xi 2 )  14 f ( xi 3 )  3 f ( xi 4 )
 3
2h
Higher Order Central Divided Difference

 f ( xi  2 )  8 f ( xi 1 )  8 f ( xi 1 )  f ( xi 2 )
f ( xi ) 
/

12h

f // ( xi )
 f ( xi 2 )  16 f ( xi 1 )  30 f ( xi )  16 f ( xi 1 )  f ( xi 2 )
 2
12h
f /// ( xi )
 f ( xi 3 )  8 f ( xi  2 )  13 f ( xi 1 )  13 f ( xi 1 )  8 f ( xi 2 )  f ( xi 3 )

8h3
Forward Finite-divided differences
Backward finite-divided differences
Centered Finite-Divided Differences
First Derivatives - Example:
Employing the high-accuracy formulas (h=0.25):
xi-2= 0.0 f(0.0) = 1.2
xi-1= 0.25 f(0.0) = 1.103516
xi = 0.5 f(0.5) = 0.925
xi+1 = 0.75 f(0.75) = 0.63633
xi+2 = 1.0 f(1.0) = 0.2
Forward Difference
 f xi  2   4 f xi 1   3 f xi 
f xi  
2h
 0.2  4(0.6363281)  3(0.925)
f 0.5   0.8594
2(0.25)
First Derivatives - Example:
 Backward Difference
3(0.925)  4(1.035156)  1.2
f 0.5   0.8781
2(0.25)

 Central Difference
 0.2  8(0.636328)  8(1.035156)
f 0.5   0.9125
12(0.25)
Summary
True value: f`(0.5) = -0.9125
Basic h = 0.25 Forward Backward Centered
formulas O(h) O(h) O(h2)
Estimate -1.155 -0.714 -0.934
|t| 26.5% 21.7% 2.4%

High- h = 0.25 Forward Backward Centered


Accuracy O(h2) O(h2) O(h4)
formulas Estimate -0.859375 -0.878125 -0.9125
|t| 5.82% 3.77% 0%
Richardson Extrapolation
Idea:
• exact • Use different h to
estimate the truncation
= computed+ error
error
• Use extrapolation to get
• The truncation error is more accurate result
of the form: chk
• where c is some
constant
Richardson Extrapolation (cont)
• Example: CD for f’(x)

f ( x  h)  f ( x  h)
f ( x)   O( h 2 )
2h
• Using Different h (h1, h2):

exact  R1  c1h12
 R2  c2 h22
• c1 and c2 could be different
Richardson Extrapolation (cont)
• If
c1  c2  c
R1  ch12  R2  ch22  exact
R2  R1
 1
2 2
2 
c h  h  R2  R1 c 2
h1  h22
R2  R1 2 R2  R1
exact  R2  2  h  R   h 2

h1  h22  
2 2 2 2
h
h22  12  1
 h2 
R2  R1
 exact  R2 
  1
h1 2
h2
Example
• f(x) = x3. Use CD with Richardson extrapolation to
compute f’(1)

h1  0.1 f (1)  3.01  R1


h2  0.05 f (1)  3.0025  R2

3.0025  3.01
exact  3.0025  2
 3.0000
 0.1 
  1
 0.05 
Derivatives of unequally spaced data
One way to handle nonequispaced data is to fit a Lagrange interpolating polynomial to a
set of adjacent points that bracket the location value at which you want to evaluate the
derivative. Remember that this polynomial does not require that the points be equispaced.
The polynomial can then be differentiated analytically to yield a formula that can be used
to estimate the derivative.
Matlab Built-in functions for numerical
differentiation
MATLAB provides the diff function to use for computing
derivative estimates.

Its syntax is d = diff(x), where x is a vector of values, and the


result is a vector d containing the differences between adjacent
elements in x.

That is, if x has n elements, d will have n - 1 elements, where

d = [x(2) - x(1), x(3) - x(2), . . . , x(n) - x(n -1)].


Polynomial differentiation functions.
Command Description

b = polyder(p) Returns a vector b containing the coefficients


of the derivative of the polynomial represented
by the vector p.

b = Returns a vector b containing the coefficients


polyder(p1,p2) of the polynomial that is the derivative of the
product of the polynomials represented by p1
and p2.

[num, den] = Returns the vectors num and den containing


polyder(p2,p1) the coefficients of the numerator and
denominator polynomials of the derivative of
the quotient p2/p1, where p1 and p2 are
polynomials.

Вам также может понравиться