PERSAMAAN DIFERENSIAL
(DIFFERENTIAL EQUATION) METODE EULER METODE RUNGE-KUTTA
PERSAMAAN DIFERENSIAL • Persamaan paling penting dalam bidang rekayasa, paling bisa menjelaskan apa yang terjadi dalam sistem fisik. • Menghitung jarak terhadap waktu dengan kecepatan tertentu, 50 misalnya.
dx 50 dt
Rate equations
PERSAMAAN DIFERENSIAL • Solusinya, secara analitik dengan integral,
dx 50dt
x 50t C
• C adalah konstanta integrasi • Artinya, solusi analitis tersebut terdiri dari banyak ‘alternatif’ • C hanya bisa dicari jika mengetahui nilai x dan t. Sehingga, untuk contoh di atas, jika x(0) = (x saat t=0) = 0, maka C = 0
KLASIFIKASI PERSAMAAN DIFERENSIAL Persamaan yang mengandung turunan dari satu atau lebih variabel tak bebas, terhadap satu atau lebih variabel bebas. • Dibedakan menurut: • Tipe (ordiner/biasa atau parsial) • Orde (ditentukan oleh turunan tertinggi yang ada) • Liniarity (linier atau non-linier)
SOLUSI PERSAMAAN DIFERENSIAL • Secara analitik, mencari solusi persamaan diferensial adalah dengan mencari fungsi integral nya. • Contoh, untuk fungsi pertumbuhan secara eksponensial, persamaan umum:
dP kP dt
Rate equations
But what you really want to know is… the sizes of the boxes (or state variables) and how they change through time
That is, you want to know: the state equations There are two basic ways of finding the state equations for the state variables based on your known rate equations: 1) 2)
Analytical integration Numerical integration
Suatu kultur bakteria tumbuh dengan kecepatan yang proporsional dengan jumlah bakteria yang ada pada setiap waktu. Diketahui bahwa jumlah bakteri bertambah menjadi dua kali lipat setiap 5 jam. Jika kultur tersebut berjumlah satu unit pada saat t = 0, berapa kira-kira jumlah bakteri setelah satu jam?
SOLUSI PERSAMAAN DIFERENSIAL • Jumlah bakteri menjadi dua kali lipat setiap 5 jam, maka k = (ln 2)/5 • Jika P0 = 1 unit, maka setelah satu jam…
dP kt kP P(t ) P0e dt ( (ln 2 ) )(1) 5 P1 t1 P ( 1 ) 1 ( e ) dP P P t kdt 1 . 1487 0 0 P ln Ck (t t0 ) P0
The Analytical Solution of the Rate Equation is the State Equation
Rate equation
(dsolve in Maple)
State equation
THERE ARE VERY FEW MODELS IN ECOLOGY THAT CAN BE SOLVED ANALYTICALLY.
SOLUSI NUMERIK • Numerical integration • Eulers • Runge-Kutta
Numerical integration makes use of this relationship:
yt t
dy yt t dt
Which you’ve seen before…
Relationship between continuous and discrete time models *You used this relationship in Lab 1 to program the logistic rate equation in Visual Basic:
Nt N t 1 N t rNt 1 K
t ,
where t 1
Fundamental Approach of Numerical Integration yt t
dy yt t dt
y = f(t), unknown yt+t, unknown
yt+t, estimated
y
dy dt yt, known
t, specified
t
, known
N N t t N t rNt 1 t t , K dN
where t 1
dt
Nt/K with time, lambda = 1.7, time step = 1 0.45 0.4 0.35
Nt/K
0.3 0.25 0.2 0.15 0.1 0.05 0 0
10
20
30
40
50
time (years)
Calculate dN/dt*1 at Nt Add it to Nt to estimate Nt+ t
Nt+ t becomes the new Nt Calculte dN/dt * 1 at new Nt Use dN/dt to estimate next Nt+ t
Euler’s Method:
Repeat these steps to estimate the state function over your desired time length (here 30 years)
yt+ t ≈ yt + dy/dt t
EXAMPLE OF NUMERICAL INTEGRATION dy 6 y .007 y 2 dt Analytical solution to dy/dt
Y0 = 10
t = 0.5
point to estimate
Euler’s Method:
yt+ t ≈ yt + dy/dt t
dy 6 y .007 y 2 dt
m1 = dy/dt at yt m1 = 6*10-.007*(10)2
y
analytical y(t+ t)
y = m1*t yest= yt + y estimated y(t+ t)
y yt = 10
t = 0.5
RUNGE-KUTTA METHODS 20
MOTIVATION • •
We seek accurate methods to solve ODEs that do not require calculating high order derivatives. The approach is to use a formula involving unknown coefficients then determine these coefficients to match as many terms of the Taylor series expansion.
21
SECOND ORDER RUNGE-KUTTA METHOD
K1 h f ( xi , yi ) K 2 h f ( xi h, yi K1 ) yi 1 yi w1K1 w2 K 2 Problem : Find , , w1, w2 such that yi 1 is as accurate as possible. 22
TAYLOR SERIES IN ONE VARIABLE The n th order Taylor Series expansion of f(x)
n
f ( x h)
hi (i ) f ( x) i!
h n 1 f ( n 1) ( x ) (n 1)!
i 0
Approximation
Error
where x is between x and x h 23
DERIVATION OF 2ND ORDER RUNGE-KUTTA METHODS – 1 OF 5 Second Order Taylor Series Expansion dy Used to solve ODE : f ( x, y ) dx dy h 2 d 2 y 3 yi 1 yi h O(h ) 2 dx 2 dx which is written as : h2 3 yi 1 yi h f ( xi , yi ) f ' ( xi , yi ) O ( h ) 2 24
DERIVATION OF 2ND ORDER RUNGE-KUTTA METHODS – 2 OF 5 where f ' ( x, y ) is obtained by chain - rule differenti ation f ( x, y ) f ( x, y ) dy f f f ' ( x, y ) f ( x, y ) x y dx x y Substituti ng : f f h2 3 yi 1 yi f ( xi , yi )h f ( xi , yi ) O (h ) x y 2
25
TAYLOR SERIES IN TWO VARIABLES f f f ( x h, y k ) f ( x, y ) h k y x 2 2 1 2 2 f f f 2 h ... k 2hk 2 2 2! x x y y
n
i 0
i
1 1 h k f ( x, y ) h k i! x y ( n 1)! x y approximation
n 1
f ( x, y)
error
( x , y ) is on the line joining between ( x, y ) and ( x h, y k ) 26
DERIVATION OF 2ND ORDER RUNGE-KUTTA METHODS – 3 OF 5 Problem : Find , , w1 , w2 such that K1 h f ( xi , yi ) K 2 h f ( xi h, yi K1 ) yi 1 yi w1K1 w2 K 2 Substituting : yi 1 yi w1h f ( xi , yi ) w2h f ( xi h, yi K1 ) 27
DERIVATION OF 2ND ORDER RUNGE-KUTTA METHODS – 4 OF 5 f f f ( xi h, yi K1 ) f ( xi , yi ) h K1 ... x y Substituti ng : f f yi 1 yi w1h f ( xi , yi ) w2h f ( xi , yi ) h K1 ... x y f f yi 1 yi ( w1 w2 )h f ( xi , yi ) w2h h K1 ... y x 2 f 2 f yi 1 yi ( w1 w2 )h f ( xi , yi ) w2h w2 h f ( xi , yi ) ... x y 28
DERIVATION OF 2ND ORDER RUNGE-KUTTA METHODS – 5 OF 5 We derived two expansions for yi 1 : f 2 f yi 1 yi ( w1 w2 )h f ( xi , yi ) w2h w2 h f ( xi , yi ) ... x y 2
f f h2 yi 1 yi f ( xi , yi )h f ( xi , yi ) O ( h 3 ) x y 2 M atching terms, we obtain the following three equations : 1 1 w1 w2 1 , w2 , and w2 2 2 3 equations with 4 unknowns infinite solutions 1 One possible solution : 1, w1 w2 2 29
2ND ORDER RUNGE-KUTTA METHODS
K1 h f ( xi , yi ) K 2 h f ( xi h, yi K1 ) yi 1 yi w1K1 w2 K 2 Choose , , w1, w2 such that : 1 1 w1 w2 1, w2 , and w2 2 2 30
ALTERNATIVE FORM Second Order Runge Kutta K1 h f ( xi , yi ) K 2 h f ( xi h, y i K1 ) yi 1 yi w1K1 w2 K 2
Alternative Form k1 f ( xi , yi ) k2 f ( xi h, yi h k1 ) yi 1 yi h w1 k1 w2 k2
31
CHOOSING , , W1 AND W2 1 For example, choosing 1, then 1, w1 w2 2 Second Order Runge - Kutta method becomes : K1 h f ( xi , yi ) K 2 h f ( xi h, yi K1 )
1 h 0 yi 1 yi K1 K 2 yi f ( xi , yi ) f ( xi 1, yi 1 ) 2 2 This is Heun ' s Method with a Single Corrector
32
CHOOSING , , W1 AND W2 1 1 Choosing then , w1 0, w2 1 2 2 Second Order Runge - Kutta method becomes : K1 h f ( xi , yi ) h K1 K 2 h f ( xi , yi ) 2 2 h K1 yi 1 yi K 2 yi h f ( xi , yi ) 2 2 This is the M idpoint M ethod 33
2ND ORDER RUNGE-KUTTA METHODS ALTERNATIVE FORMULAS 1 w2 , 2
1 w2 , w1 w2 1 2
1 1 Pick any nonzero number : , w2 , w1 1 2 2 Second Order Runge Kutta Formulas (select 0) K1 h f ( xi , yi ) K 2 h f ( xi h, yi K1 ) 1 1 yi 1 yi 1 K2 K1 2 2 34
SECOND ORDER RUNGE-KUTTA METHOD EXAMPLE Solve the following system to find x (1.02) using RK2 x (t ) 1 x 2 t 3 ,
x (1) 4, h 0.01, 1
STEP 1 : K1 h f (t0 1, x0 4) 0.01(1 x02 t03 ) 0.18 K 2 h f (t0 h, x0 K1 ) 0.01(1 ( x0 0.18)2 (t0 .01)3 ) 0.1662
x (1 0.01) x (1) K1 K 2 / 2
4 (0.18 0.1662) / 2 3.8269 CISE301_Topic8
35
SECOND ORDER RUNGE-KUTTA METHOD EXAMPLE STEP 2 K1 h f (t1 1.01, x1 3.8269) 0.01(1 x12 t13 ) 0.1668 K 2 h f (t1 h, x1 K1 ) 0.01(1 ( x1 0.1668)2 (t1 .01)3 ) 0.1546 1 x (1.01 0.01) x (1.01) K1 K 2 2 1 3.8269 (0.1668 0.1546) 3.6662 2 36
x (t ) 1 x (t ) t , x(1) 4, Solution for t [1,2] 2
3
Using RK2, 1
37
2ND ORDER RUNGE-KUTTA
RK2
Typical value of 1, Know as RK2 Equivalent to Heun' s method with a single corrector k1 f ( xi , yi ) k2 f ( xi h, yi k1 h ) h yi 1 yi k1 k2 2 Local error is O ( h 3 ) and global error is O (h 2 )
38
HIGHER-ORDER RUNGE-KUTTA Higher order Runge-Kutta methods are available. Derived similar to second-order Runge-Kutta. Higher order methods are more accurate but require more calculations.
39
3RD ORDER RUNGE-KUTTA
RK3
Know as RK3 k1 f ( xi , yi ) h 1 k2 f ( xi , yi k1h ) 2 2 k3 f ( xi h, yi k1h 2k2h ) h yi 1 yi k1 4k2 k3 6 4 3 Local error is O (h ) and Global error is O ( h ) 40
4TH ORDER RUNGE-KUTTA
RK4
k1 f ( xi , yi ) h 1 k2 f ( xi , yi k1h ) 2 2 h 1 k3 f ( xi , yi k2h ) 2 2 k4 f ( xi h, yi k3h ) h yi 1 yi k1 2k2 2k3 k4 6 5 4 Local error is O ( h ) and global error is O ( h ) 41
HIGHER-ORDER RUNGE-KUTTA k1 f ( xi , yi ) 1 k2 f ( xi h, 4 1 k3 f ( xi h, 4 1 k4 f ( xi h, 2 3 k5 f ( xi h, 4
1 yi k1h ) 4 1 1 yi k1h k2h ) 8 8 1 yi k2h k3h ) 2 3 9 yi k1h k4h ) 16 16 3 2 12 12 8 k6 f ( xi h, yi k1h k2h k3h k4h k5h ) 7 7 7 7 7 h yi 1 yi 7k1 32k3 12k4 32k5 7k6 90 42
EXAMPLE
RK4 4TH-ORDER RUNGE-KUTTA METHOD
dy 2 1 y x dx y (0) 0.5 h 0.2 Use RK 4 to compute y (0.2) and y (0.4) 43
EXAMPLE: RK4
Problem : dy 1 y x2 , y (0) 0.5 dx Use RK 4 to find y (0.2), y (0.4)
44
4TH ORDER RUNGE-KUTTA
RK4
k1 f ( xi , yi ) h 1 k2 f ( xi , yi k1h ) 2 2 h 1 k3 f ( xi , yi k2h ) 2 2 k4 f ( xi h, yi k3h ) h yi 1 yi k1 2k2 2k3 k4 6 5 4 Local error is O ( h ) and global error is O ( h ) 45
EXAMPLE: RK4
See RK4 Formula
Problem : dy 1 y x2 , y (0) 0.5 dx Use RK 4 to find y (0.2), y (0.4)
h 0.2 f ( x, y ) 1 y x 2 x0 0, y0 0.5
Step 1
k1 f ( x0 , y0 ) (1 y0 x02 ) 1.5 1 1 k2 f ( x0 h, y0 k1h ) 1 y0 0.15 x0 0.12 1.64 2 2 1 1 k3 f ( x0 h, y0 k2h ) 1 y0 0.164 x0 0.12 1.654 2 2 k4 f ( x0 h, y0 k3h ) 1 y0 0.16545 x0 0.2 2 1.7908 y1 y0
h k1 2k2 2k3 k4 0.8293 6 46
EXAMPLE: RK4 Problem : dy 1 y x2 , y (0) 0.5 dx Use RK 4 to find y (0.2), y (0.4)
h 0.2 f ( x, y ) 1 y x 2 x1 0.2, y1 0.8293
k1 f ( x1 , y1 ) 1.7893 1 1 h, y1 k1h ) 1.9182 2 2 1 1 k3 f ( x1 h, y1 k 2 h ) 1.9311 2 2 k4 f ( x1 h, y1 k3h ) 2.0555
Step 2
k2 f ( x1
y2 y1
0.2 k1 2k2 2k3 k4 1.2141 6 47
EXAMPLE: RK4 Problem : dy 1 y x2 , y (0) 0.5 dx Use RK 4 to find y (0.2), y (0.4)
Summary of the solution xi
yi
0.0
0.5
0.2
0.8293
0.4
1.2141 48
SUMMARY • Runge Kutta methods generate an accurate solution without the need to calculate high order derivatives. • Second order RK have local truncation error of order O(h3) and global truncation error of order O(h2). • Higher order RK have better local and global truncation errors. • N function evaluations are needed in the Nth order RK method.
49