You are on page 1of 24

Numerical Differentiation

Prepared by
Rasel Ahmmed
Lecturer, Dept. of EEE
BSMRSTU, Gopalganj
Lecture’s Goals
• Finite Difference Schemes
• Taylor Series Expansion for Differentiation
• Basic Numerical Integration
– Trapezoidal Rule
– Simpson’s Rule
– Midpoint
– Gaussian Quadrature
Calculus - Numerical Methods
Differentiation Integration

v v

dv
dt
t t
a y
b
dv y   vdt
a a
dt
t
Numerical Differentiation
• Estimate the derivatives (slope,
curvature, etc.) of a function by using the
function values at only a set of discrete
points
• Ordinary differential equation (ODE)
• Partial differential equation (PDE)
Numerical Differentiation
• Represent the function by Taylor
polynomials or Lagrange interpolation
• Evaluate the derivatives of the
interpolation polynomial at selected
nodal points
Numerical Differentiation
A Taylor series or Lagrange interpolation of points
can be used to find the derivatives. The Taylor
series expansion is defined as:

f xi   f x0   x
df

x 
2
d2 f

x  d 3 f
3

dx xx0 2! dx 2 x x0
3! dx 3
x x0

x  xi  x0

f xi   f x0   xi  x0  f x0  


 xi  x0 
2
f x0  
 xi  x0 
3
f x0   
2! 3!
First Derivative at a Point

f ( x )

i-2 i-1 i i+1 i+2


Numerical Differentiation
Use the Taylor series expansion to represent three
points about single location:

f xi 1   f xi   xi 1  xi  f xi  


 xi 1  xi 
2
f xi    1
2!
f xi   f xi  2
f xi -1   f xi   xi 1  xi  f xi  
 xi 1  xi 
2
f xi    3
2!
Numerical Differentiation

Assume that the data points are “equally


spaced” and the equations can be written as:

f xi 1   f xi   x  f xi  


x 
2
f x  
x 
3
f xi    1
i
2! 3!
f xi   f xi  2
f xi-1   f xi   x  f xi  
x 
2
f x  
x 
3
f xi   3
i
2! 3!
Forward Differentiation
For a forward first derivative, subtract eqn[2] from eqn[1]:
f xi 1   f xi   x  f xi  
x 
2
f x  
x 
3
f xi   
i
2! 3!
Rearrange the equation:
x  f xi   f xi1   f x   x 
2
f x  
x 
3
f xi   
i i
2! 3!
 f  xi 1   f  xi    x   x 
2

f   xi     f   xi   f   xi   
 x  2! 3!
Forward Differentiation

As the x gets smaller the error will get smaller


 f xi 1   f xi  
f xi      Error
 x 
The error is defined as:

Error 
x  f x   x  2
f xi   
i
2! 3!
Backward Differentiation

Subtract eqn[3] from eqn[2]:


 f xi   f xi-1  
f xi      Error
 x 
The error is defined as:

Error 
x  f x   x  2
f xi   
i
2! 3!
Central Differentiation

Subtract eqn[3] from eqn[1]:


 f xi 1   f xi-1  
f xi      Error
 2x 
The error is defined as:

Error 
x  2
f xi   
3!
Differential Error

Notice that the errors of the forward and backward 1st


derivative of the equations have an error of the order
of O(x) and the central differentiation has an error
of order O(x2). The central difference has an better
accuracy and lower error that the others. This can be
improved by using more terms to model the first
derivative.
Homework
Lagrange Differentiation
Another form of differentiation is to use the
Lagrange interpolation between three points. The
values can be determine for unevenly spaced points.
Given:

Lx   L1 x y1  L2 x y2  L3 x y3


 x  x2 x  x3 
y1 
 x  x1 x  x3 
y2 
x  x1 x  x2 
y3
x1  x2 x1  x3  x2  x1 x2  x3  x3  x2 x3  x1 
Lagrange Differentiation
Differentiate the Lagrange interpolation
2 x  x2  x3
f x   Lx   y1
x1  x2 x1  x3 
2 x  x1  x3 2 x  x1  x2
 y2  y3
x2  x1 x2  x3  x3  x2 x3  x1 
Assume a constant spacing
2 x  x2  x3 2 x  x1  x3 2 x  x1  x2
f x   2
y1  2
y2  2
y3
2x  x 2x
Lagrange Differentiation
Differentiate the Lagrange interpolation
2 x  x2  x3 2 x  x1  x3 2 x  x1  x2
f x   2
y1  2
y2  2
y3
2x  x 2x
Various locations
2x  x  x 2x  x  x 2 x1  x1  x2  3 y1  4 y2  y3
f x1   1 2 2 3 y1  1 1 2 3 y2  y3 
2x  x 2x 2 2x
2 x2  x2  x3 2 x2  x1  x3 2 x2  x1  x2 y3  y1
f x2   y1  y 2  y 3 
2x 2  x 2 2x 2 2x

2 x3  x2  x3 2 x3  x1  x3 2 x3  x1  x2 y1  4 y2  3 y3
f x3   y1  y 2  y3 
2x 2  x 2 2x 2 2x
Lagrange Differentiation
To find a higher order derivative from the Lagrange
interpolation for a three point Lagrange

2 x  x2  x3 2 x  x1  x3 2 x  x1  x2
f x   2
y1  2
y2  2
y3
2x  x 2x

Take the derivative

1 2 1 y1  2 y2  y3
f x   2 y1  2
y 2  2 y3 
x  x x x 2
Example
• Find a Lagrange differential x f(x)
f’(x) to interpolate the data.
3 3

4 5

5 1

6 6

7 9

20
Partial Derivatives
• Straightforward extension of one-dimensional
formula

(i, j+2)

(i-1, j+1) (i, j+1) (i+1, j+1)

(i-2, j) (i-1, j) (i, j) (i+1, j) (i+2, j)

(i-1, j-1) (i, j-1) (i+1, j-1)

(i, j-2)
u 1
ux    1  0  1 
 x 2h
 2u 1
u xx  2
 2  1  2  1 
x h
(i, j+2)

(i-1, j+1) (i, j+1) (i+1, j+1)

(i-2, j) (i-1, j) (i, j) (i+1, j) (i+2, j)

(i-1, j-1) (i, j-1) (i+1, j-1)

(i, j-2)
Partial Derivatives
Laplacian Operator
2
 u  u xx  u yy 
 1  j+1
 | 
1  
2 
1  4  1  j
h  
|
 
 1  j-1
i-1 i i+1
Partial Derivatives
Mixed Derivative

  1  0  1  j+1
 | | | 
1  
u xy  2  0  0  0  j
4h 
| | | 
 
 1  0   1  j-1
i-1 i i+1

You might also like