Metode Runge Kutta

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Metode Numerik

-Metode Runge-Kutta

PSSM
Genap 2022-2023
Euler vs Actual Solution
Euler Solution
f ( xi , yi )
y

yi fi true solution

h
yi 1  yi  fi h

ti xi+1 = xi + h

RK methods differ in how they estimate fi


For Euler Method (a type of RK method), fi ≈ f(xi, yi)
Taylor Series in One Variable
The n th order Taylor Series expansion of f(x)
n
hi (i ) h n 1
f ( x  h)  f ( x)  f ( n 1) ( x )
i! (n  1)!
i 0

Approximation Error

where x is between x and x  h


3
Taylor Series in Two Variables
 f f 
f ( x  h, y  k )  f ( x, y )   h  k  
 x y 
1  2 2 f  2
f  2
f 
h k 2
 2hk   ...

2!  x 2
y 2
x y 


n
i n 1
1    1    
  h  k  f ( x, y )   h  k  f ( x, y)
i!  x y  ( n  1)!  x y 
i 0
approximation error

( x , y ) is on the line joining between ( x, y ) and ( x  h, y  k )


4
Runge-Kutta Order 2 Method
f ( xi  h, yi   (hf ( xi , yi )))
yi   (hf ( xi , yi ) )
y
f ( xi , yi )
yi 1  yi  fi h
yi
fi
fi  1hf ( xi , yi ) 
2hf ( xi  h, yi  hf ( xi , yi ))
h
Weighted average of two slopes
x
xi xi+αh xi+1
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.

6
Derivation of 2nd Order
Runge-Kutta Methods – 1 /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  2
 O (h )
dx 2 dx
which is written as :
h2 3
yi 1  yi  h f ( xi , yi )  f ' ( xi , yi )  O ( h )
2
7
Derivation of 2nd Order
Runge-Kutta Methods – 2(5)

where f ' ( x, y ) is obtained by chain - rule differentiation


f ( x, y ) f ( x, y ) dy f f
f ' ( x, y )     f ( x, y )
x y dx x y
Substituting :
 f f  h2 3
yi 1  yi  f ( xi , yi )h    f ( xi , yi )   O ( h )
 x y  2

8
Derivation of 2nd Order
Runge-Kutta Methods – 3 (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 )  w2 h f ( xi   h, yi   k1 )

9
Derivation of 2nd Order
Runge-Kutta Methods – 4 (5)

f f
f ( xi  h, yi  k1 )  f ( xi , yi )  h  k1  ...
x y
Substituting :
 f f 
yi 1  yi  w1h f ( xi , yi )  w2 h  f ( xi , yi )  h  k1  ...
 x y 
 f f 
yi 1  yi  ( w1  w2 )h f ( xi , yi )  w2 h  h   k1  ... 
 x y 
2 f 2 f
yi 1  yi  ( w1  w2 )h f ( xi , yi )  w2h  w2 h f ( xi , yi )  ...
x y
10
Derivation of 2nd Order
Runge-Kutta Methods – 5 (5)

We derived two expansions for yi 1 :


f 2 f
yi 1  yi  ( w1  w2 )h f ( xi , yi )  w2h2
 w2 h f ( xi , yi )  ...
x y
 f f  h2
yi 1  yi  f ( xi , yi )h    f ( xi , yi )   O ( h 3 )
 x y  2
Matching 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
11
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
12
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  13
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 )

yi 1  yi  k1  k 2   yi   f ( xi , yi )  f ( xi 1 , yi 1 ) 
1 h 0

2 2
This is Heun' s Method with a Single Corrector

14
Heun’s Method (Pav’s RK-Order 2)
Heun’s method
Slope  f ti  h, xi  k1h 
1 x
1 
2 xi+1
1 Slope  f ti , xi 
2 
2
 1 1
Avg Slope  [ f (ti  h, xi  hk1 )  f (ti , xi )]
 1 2
xi
resulting in
1 1 
xi 1  xi   k1  k 2 h
2 2 
ti+1
x
ti
where
Heun’s method is ~ Trapezoidal Rule in Integration
k 1  f t i , x i 

t i h

k 2  f ti  h, xi  k1h 
x i1  x i  ti
f (r, x) dr
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 Midpoint Method
Midpoint method is ~ Midpoint Rule in Integration
16
Choosing , , w1 and w2
1 1 2 
1  resulting in
xi 1  xi   k1  k 2 h
3 3 3 
2 where
2  k1  f ti , xi 
3
 3 3 
3 k 2  f  ti  h, xi  k1h 
  4 4 
4 Ralston’s Method
3

4
Ralston (1962) and Ralston and Rabinowitiz (1978)
determined that choosing ω2 = 2/3 provides a minimum
bound on the truncation error for the second order RK
algorithms.
2nd Order Runge-Kutta Methods
Alternative Formulas
1 1
 w2  ,  w2  , w1  w2  1
2 2
1 1
Pick any nonzero  number :    , w2  , w1  1 
2 2

Second Order Runge Kutta Formulas (select  0)


k1  h f ( xi , yi )
k2  h f ( xi   h, yi   k1 )
 1  1
yi 1  yi  1  k1  k2
 2  2
18
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

k1  h f ( xi , yi )  1
y
 i 1  y i  k1  k2 
k 2  h f ( xi  h, yi  k1 ) 2
STEP 1 :
2 3
k1  h f (t0  1, x0  4)  0.01(1  x0  t0 )  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
19
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

20
2 3
x (t )  1  x (t )  t , x (1)  4,
Solution for t  [1,2]
Using RK2,   1

CISE301_Topic8L4&5 21
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 Error is lower than Euler, but
yi 1  yi  k1  k2  now there are 2 calls to f(x,y)
2 required per step

Local error is O ( h 3 ) and global error is O ( h 2 )

Note: ω1=1, ω2=0 -> Euler’s Method (Still O(h2) as no


22
averaging
Contoh lainnya
metode Runge-Kutta orde dua

Solusi eksak:
24
Perbandingan penyelesaian eksak dan penyelesaian numerik
dengan metode Runge-Kutta orde dua

t Penyelesaian Penyelesaian Numerik Kesalahan


Eksak dengan RK-2 Mutlak
0.000000000 -2.000000000 -2.0000000000 0.000000000
0.250000000 -1.755081338 -1.7578125000 0.002731162
0.500000000 -1.537882843 -1.5436479710 0.005765128
0.750000000 -1.364851048 -1.3731916729 0.008340624
1.000000000 -1.238405844 -1.2480773083 0.009671464
1.250000000 -1.151716360 -1.1613246045 0.009608244
1.500000000 -1.094851746 -1.1034042776 0.008552531
1.750000000 -1.058624462 -1.0656655347 0.007041072
2.000000000 -1.035972420 -1.0414541993 0.005481779
Listing program

%Metode Runge-Kutta Orde 2


% dy/dt = f(t,y)
%f(y,t) = -(y+1)*(y+3);
%y(0) = -2;

clear all;
dt=0.1;
t=0:dt:2;
N=length(t) - 1;
y = zeros(N+1,1);

y(1) = -2;
y_eks(1) = -2;
err(1) = 0;

for n = 1:N
k1 = -(y(n)+1)*(y(n)+3);
k2 = -(y(n)+dt*k1 + 1)*(y(n)+dt*k1 + 3);
y(n+1) = y(n) + 0.5*dt*(k1 + k2 ); %solusi RK2
y_eks(n+1) = -3 + 2/(1+exp(-2*t(n+1))); %solusi eksak
err(n+1)=abs(y(n+1)-y_eks(n+1)); %kesalahan mutlak
end;

figure(1)
plot(t,y,'ko',t,y_eks,'r');
figure(2); %hold on;
plot(t,err);
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.

27
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 )
28
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 )
29
Higher-Order Runge-Kutta
k1  f ( xi , yi )
1 1
k2  f ( xi  h, yi  k1h )
4 4
1 1 1
k3  f ( xi  h, yi  k1h  k2h )
4 8 8
1 1
k4  f ( xi  h, yi  k2h  k3h )
2 2
3 3 9
k5  f ( xi  h, yi  k1h  k4h )
4 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
30
Example
4th-Order Runge-Kutta Method RK4

dy
 1 y  x 2

dx
y (0)  0.5
h  0.2
Use RK 4 to compute y (0.2) and y (0.4)

31
Example: RK4 See RK4 Formula
Problem : h  0.2
dy
 1 y  x2 , y (0)  0.5 f ( x, y )  1  y  x 2
dx
x0  0, y0  0.5
Use RK 4 to find y (0.2), y (0.4)
k1  f ( x0 , y0 )  (1  y0  x0 2 )  1.5
1 1
k2  f ( x0  h, y0  k1h )  1   y0  0.15   x0  0.12  1.64
2 2
Step 1

1 1
k3  f ( x0  h, y0  k2h )  1   y0  0.164    x0  0.12  1.654
2 2
k4  f ( x0  h, y0  k3h )  1   y0  0.16545   x0  0.2 2  1.7908
h
y1  y0  k1  2k2  2k3  k4   0.8293
6
32
Example: RK4
Problem :
h  0.2
dy
 1 y  x2 , y (0)  0.5 f ( x, y )  1  y  x 2
dx
x1  0.2, y1  0.8293
Use RK 4 to find y (0.2), y (0.4)
k1  f ( x1 , y1 )  1.7893
1 1
k2  f ( x1  h, y1  k1h )  1.9182
2 2
Step 2

1 1
k3  f ( x1  h, y1  k2h )  1.9311
2 2
k4  f ( x1  h, y1  k3h )  2.0555
0.2
y2  y1  k1  2k2  2k3  k4   1.2141
6
33
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
34
CONTOH LAINNYA

35
CISE301_Topic8L4&5 37
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.
38
Masalah nilai awal untuk sistem
persamaan diferensial

(1)
Second order R-K method

40
Contoh

41
43
Fourth order R-K method

44
Contoh:

Solution:

Cek!!!

45
Exercise
1.

a) with step length 0.2 using Runge-Kutta 2 method


b) with step length 0.2 using Runge-Kutta 4 method.

2. Find the solution for interval [0,1], with step length 0.01
using Runge-Kutta 2 method and Runge-Kutta 4 method.

46

You might also like