Documente Academic
Documente Profesional
Documente Cultură
Numerical
Differentiation,
Ordinary differential
equations(ODE)
Aman W
Department of Applied Physics
University of Gondar
Contents
Numerical Differentiation,
Ordinary differential equations(ODE)
Forward
backward difference &
Central difference
Introduction
()
= ()
These equations may have analytical solutions, and
ODE algorithm
y' (t ) f (t , y(t ))
y(t 0 ) y0
Ordinary differential equations
In order to solve these equations, we need
1. A numerical method that permits an approximate solution
using only arithmetic and
2. Some Initial conditions.
The first an approximation to the derivative is suggested by the
definition from basic calculus: remember the elementary
definition of the derivative of a function f at a point x:
() + () + ()
= lim
0
df x f ( x x) f ( x)
lim
dx x 0 x
Backward difference
df ( x) f ( x) f ( x x)
lim
dx x 0 x
Centered difference
df ( x) f ( x x) f ( x x)
lim
dx x 0 2x
x 2
x 3
x 4
f ( x x) f ( x) xf ' ( x) f '' ( x) f ''' ( x) f '''' ( x) ...
2! 3! 4!
df x f ( x x) f ( x)
lim ?
dx x 0 x
Taylor Series
For the forward difference that leads to :
f ( x x) f ( x) 1 x 2 '' x 3 '''
xf '
( x ) f ( x ) f ( x ) ...
x x 2! 3!
x
f ' ( x) f '' ( x )
2
f ' ( x) O(x)
f ( x x / 2) f ( x x / 2) 1 x 3
xf ( x)
'
f ( x) ...
'''
x x 3!
f ' ( x) O(x 2 )
x h
()
= ()
Where is a decay constant. Numerical approximation. Change
in a time interval is proportional to N, the proportionality
constant is
+
= ()
N + = ()
from pylab import *
Recipe to solve t = 0.
exponential decay law: dt = 0.1
Lamda=L = 0.5 # decay constant
define the initial
N = 10000.0 # Number of atoms at t = 0
ta = [t] # list for storing time
condition at t = 0 na = [N] # and instantaneous number of
atoms
repeat the following while t < 5:
until some final value dn = -L * N * dt
N = N + dn
for t is reached t = t + dt
ta.append(t)
Use above equation () to na.append(N)
determine the solution
plot(ta,na)
at t + t show()
The explicit(forward difference) solution can be unstable for
large step sizes.
Compare anyway for the following
=dt= 0.01
dt= 0.05
dt= 0.15
Example
Let us take a simple ordinary differential equation(ode)
= 6
The solution of this differential equation is the following
= 6
Let us use the Euler method to solve and plot it along side with
the exact result, to be able to judge the accuracy of the
numerical method.
To solve this equation using the Euler method we rewrite the
forward Euler formula above with a different look
+
=
+
= 6 or
+ = 6
When we are ready to start coding in
= 0.1 # step's size
N=100; # number of steps
y(1)=1.0
listx= [] # list for storing x and
listy= [] # list for storing y
listx. append()
Listy.append ( )
for i in range(N):
yi+1=yi- *6yi # y(i+)= y(i)- (6*y(i))
Listy.append ( yi+1)
xi+1=i*
listx. append()
plot(listx,listy)
The approximate gradient is calculated using values of f at x
greater than xt+h, this algorithm is known as the forward
difference method.
Where h would be a separation between points
Note this is calculating a series of derivatives at the points f(x)
Just how good is this estimate?
0.0
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
Example: First Derivative
Use central difference approximation 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 ( = ) = 0.5 and 0.25 (exact sol. = -0.9125)
Central Difference
f ( 1 ) f ( 0 ) 0.2 1.2
h 0.5 , f ( 0.5 ) 1.0 , t 9.6%
10 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 more