Komputasi Numerik:Integrasi dan
Differensiasi Numerik
Agus Naba
Physics Dept., FMIPA-UB
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 2
Ordinary Differential Equation
Ordinary Differential Equation (ODE) is a differential equation in which all dependent variables are functions
of a single independent variable.
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 3
ODE’s Problem
00 ')0(';)0(
)),(,()(')(
yyyy
tytftydt
tdy
First-order Ordinary Differential Equation (ODE):
?)t(y
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 4
Euler’s Method
t
tyty
dt
dy
ntnttt
nn
t
n
n
)()(
,..2,1,0 ,
1
0
tytfytdt
dyyy nnn
tnn
n
),(1
)tt(yy);t(yy nnnn 1
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 5
tn tn+1
tdt
dyyy
ntnn 1
It enables us to calculate all of yn = y (tn), given y(t0).
y(t)
tdt
dy
nt
yn
ntdt
dySlope:
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 6
Numerical Errors
Truncation Errors, depending on numerical methods
Round-off Errors, depending on capability of computer in storing floating-point number
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 7
Truncation ErrorsThe curve y(t) is not generally a straight-line between the neighbouring grid-times tn and tn+1 as assumed.
According to Taylor Series:
...t
dt
ydt
dt
dyyy
nn ttnn
2
2
2
2
1
O(t2)Truncation Error
Each step incurs truncation error ~ t2 Net truncation errors of Euler’s Method ~ t
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 8
Round-off Errors
For every type of computer, there is a charasteristic number, , which defined as the smallest number which when added to a number of order unity gives rise to a new number. For example: = 2.2 x 10-16 (for double precision number in IBM-PC ) = 1.19 x 10-7 (for single precision number in IBM-PC )
The net round-off errors of Euler’s Method /t.
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 9
Net Numerical Errors of Euler’s Method
tt
~
At large t, the error is dominated by the truncation errors, whereas the round-off errors dominates at small t .
Minimum net numerical errors are achieved when
2121 // ~;~t
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 10
t
t~1/2
~1/2
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 11
1)0(0)()(
y,,tydt
tdy
t
y(0)=1
tety )(
Numerical Instalibilities
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 12
Solusi Numerik,...,,n,tnttn 210 0
nn y)t(y 11
/t 2 /t 2
Numerical Instabilitiesy(0) y(0)
t t
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 13
Defect of Euler’s Method
Not generally used in scientific computing: Truncation errors is far larger than other,
more advanced, methods. Too prone to numerical instabilities
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 14
Main reason of large truncation errors:
Euler’s method only evaluates derivative at the beginning of the interval [tn,tn+1], i.e., at tn.
( Very asymetric with respect to the beginning and the end of the interval)
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 15
tn tn+1tn+ h/2
h
y(t)
Runge-Kutta (RK) Methods
221
2
ky,
htff nn
nn y,tff 1
11 hfk
k1 k2
yn
f1
f2
y(t)
22 hfk
k1 /2
11 kyy nn
21 kyy nn
The 2nd order RK Method
Euler’s Method
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 16
Modified Euler’s Method
12 ky,htff nn
nn y,tff 1
11 hfk
tn tn + h
h
y(t)
f1
yn
k1
f2
k2
(k1+k2)/2
22 hfk
yn+1 = yn+ (k1+k2)/2
Modified Euler’s Method
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 17
tn tn + h/2 tn + h
f1
f2
f3
f4
The 4th order RK method
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 18
344
233
122
11
43211
22
22
226
1
ky,hthfhfk
ky,
hthfhfk
ky,
hthfhfk
y,thfhfk
kkkkyy
nn
nn
nn
nn
nn
The 4th order RK method
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 19
Method
Equations
Euler (Error of the order h2)
yxfhk
ky
,1
1
Modified Euler (Error of the order h3)
12
1
21
,
,2
1
kyhxfhk
yxfhk
kky
Heun (Error of the order h4)
23
12
1
31
3
2
3
2
3
1
3
1
34
1
ky,hxfhk
ky,hxfhk
y,xfhk
kky
4th order Runge Kutta (Error of the order h5)
34
23
12
1
4321
,
2
1,
2
1
2
1,
2
1
,
226
1
kyhxfhk
kyhxfhk
kyhxfhk
yxfhk
kkkky
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 20
Net Numerical Errors of RK Methods
Method-RK oforder , :nhh
~ n
)n/(n)n/( ~~h 111
:at errors Minimum
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 21
order hmin min
1 1.5 x 10-8 1.5 x 10-8
2 6.1 x 10-6 3.7 x 10-11
3 1.2 x 10-4 1.8 x 10-12
4 7.4 x 10-4 3.0 x 10-13
5 2.4 x 10-3 9.0 x 10-14
RK Methods Performanceon IBM-PC for double precision
hmin increases and min decreases as order gets larger, but needs more computational effort.
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 22
Example
tksinty
k,t?ty
kdt
dyy
tkydt
yd
t
1101
000
2
2
?txtkxdt
tyd
dt
tdx
?)t(xtxdt
)t(dy
dt
tdxdt
)t(dytx
tytx
212
22
121
2
1
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 23
Global integration errors associated with Euler's method (solid curve) and the 4th order Runge-Kutta method (dotted curve) plotted against the
step-length h. Single precision calculation.
err = yanalitic-ynumeric
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 24
Global integration errors associated with Euler's method (solid curve) and the 4th order Runge-Kutta method (dotted curve) plotted
against the step-length h. Double precision calculation.
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 25
Adaptive Integration Method
xktdt
dx
tdt
xd 22
2
41
Consider the following ODE:
Analitic solution:
2tksinx
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 26
Global integration error associated with a xed step-length (h = 0:01), 4th order RK method, plotted against the independent variable, t, for a system of o.d.e.s in which the
variation scale-length decreases rapidly with increasing t. Double precision calculation.
err = xanalitic-xnumeric
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 27
It can be seen that, although the error starts off small, it rises rapidly as the variation scale-length of the solution decreases (i.e., as t increases), and quickly becomes
unacceptably large. Of course, we could reduce the error by simply reducing the step-length, h. However, this is a very
inefficient solution. The step-length only needs to be reduced at large t. There is no need to reduce it, at all, at
small t.
Solution: h should be large at small t but needs to be reduced at large t
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 28
5
1
0
oldnew hh
The step-length h should be increased if the truncation error per step is too small, and vice versa, in such a manner that the
error per step remains relatively constant at 0.
stepper error n truncatiodesired the:0
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 29
Global integration errors associated with fixed step-length (h = 0.01), 4th order RK method (solid curve) and a corresponding adaptive method (0 = 10-8) (dotted curve), plotted against the independent variable, t, for a system of o.d.e.s in which the variation scale-length decreases rapidly with increasing t. Double precision calculation.
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 30
Differentiation
An object is moving through space, its position as a function of time x(t) is recorded in a table.
Problem: Determine the object’s velocity v(t)=dx/dt and acceleration a(t)=d2x/dt2
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 31
Method: Numeric
h
xfhxflimxf
dx
xdfh
0
1
Even a computer runs into errors with such a method because of its subtraction operations: the numerator tends to fluctuate between 0 and the machine precision as the denominator approaches zero.
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 32
Method: Forward Difference (FD)
c denotes a computed expression.
h
xfhxfxfc
1
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 33
x x+h
f(x)
FD: using two points to represent the 1st derivative function by a straight line in the interval from x to x+h
xfc1
xf 1
h
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 34
...xfh
xfh
xhfxfhxf 33
22
1
62
...xfh
xfh
xf
,h
xfhxffc
32
21
1
62
Error
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 35
Example of FD
2bxaxf bxxf 21
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 36
FD solution
This clearly becomes a good approximation only for small h, i.e., h << 2x
bhbx
xfh
xfh
xf
h
xfhxfxfc
2
...62
,
32
21
1
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 37
Method: Central Difference (CD)
h,xfDh
/hxf/hxfxf cc
221
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 38
x-h/2 x x+h/2
f(x)
CD: using two points to represent the function by a straight line in the interval from x-h/2 to x+h/2
21 /hxfc
xf 1
h
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 39
...xfh
xfh
xfh
xfh
xf
...xfh
xfh
xfh
xfh
xf
33
22
1
33
22
1
48822
48822
...xfh
xf
h
/hxf/hxfxfc
32
1
1
24
22
Error
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 40
Example of CD
2bxaxf bxxf 21
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 41
CD solution
CD Method gives the exact answer regardless of the size of h !
bx
...xfh
xf
h
/hxf/hxfxfc
224
22
32
1
1
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 42
Method:Extrapolated Difference (ED)
The error in FD ~ hThe error in CD ~ h2
The error in ED ~ h4
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 43
...xfh
xf
/h
/hxf/hxf/h,xfDc
32
1
96
2
442
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 44
x-h/2 x x+h/2
f(x)
h,xfDc
xf 1
x-h/4 x+h/4
2/h,xfDc
h
h/2
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 45
Extrapolated Difference
3
241 h,xfD/h,xfDxf cc
c
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 46
x-h/2 x x+h/2
f(x)
h,xfDc
xf 1
x-h/4 x+h/4
2/h,xfDc
h
h/2
24 /h,xfDc
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 47
xfh
xf
h,xfD/h,xfDxf cc
c
34
1
1
1641920
3
24
Error
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 48
22448
3
11 hxf
hxf
hxf
hxf
hfc
A Good Way of Computing for ED
It reduces the loss of precision that occurs when large and small numbers are added together, only to be subtracted from other large numbers.
Subtract the large number from each other and then add the difference to the small numbers !
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 49
Attention !
Regardless of the algorithm, evaluating the derivative of f(x) at x requires us to know the values of f surrounding x !
HOW ?Once we have the derivative of f(x) at x, USE the integration methods, ex., RK Method, to approximate the values of f surrounding x !
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 50
Error Analysis
The approximation/truncation errors in numerical differentiation decrease with decreasing step size h while roundoff errors increase with a smaller step size. Total error is minimum if
minimum. This occurs when
rotrunc
rotrunc
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 51
Roundoff Error
The limit of roundoff error is essentially machine precision:
h
hh
xfhxf'f
ro
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 52
Truncation Errors
Truncation Error of FD:
Truncation Error of CD:
24
223
2
hf
hf
cdtrunc
fdtrunc
AGUS NABA-Computational Physics-Physics. Dept., FMIPA-UB 53
Best h
The h value for which roundoff and truncation errors are equal is
Ex., for single precision 10-7 for f(x)=ex or cos(x)
hfd 0.0005 and hcd 0.01
33
22
232
242242
fh
fh
hf
h
hf
h
cdfd
cdro
fdtrunc