How does one write a first order differential equation in the form of
dy
f x, y
dx
Example
dy
2y 1.3e x ; y 0 5
dx
is rewritten as
dy
1.3e x 2y ; y 0 5
dx
In this case
f x, y 1.3e x 2y
1
Euler’s Method
y
True
value
y1, Predicted
x0,y0 value
Φ
Step size, h
x
dy
f x, y , y 0 y0
dx
Rise y y
Slope 1 0 f x 0 , y0
Run x1 x 0
y1 y0 f x 0 , y0 x1 x 0
y0 f x 0 , y0 h
2
y
True Value
xi xi+1 x
yi 1 yi f x i , yi h
h x i 1 x i
3
Example
A solid steel shaft at room temperature of 27°C is needed to be contracted so that
it can be shrunk-fit into a hollow hub. It is placed in a refrigerated chamber that
is maintained at −33°C. The rate of change of temperature of the solid shaft q is
given by
Using Euler’s method, find the temperature of the steel shaft after 86400 seconds.
Take a step size of h = 43200 seconds.
dθ
dt
5.33 10 6 3.69 10 6 θ 4 2.33 10 5 θ 3 1.35 10 3 θ 2 5.42 10 2 θ 5.588 θ 33
f t, θ 5 .3 3 1 0 6 3 .69 10 6 θ 4 2 .3 3 1 0 5 θ 3 1 .3 5 1 0 3 θ 2 5 .4 2 1 0 2 θ 5 .5 8 8 θ 3 3
i 1 i f t i , i h
4
Solution
Step 1: i 0, t 0 0, 0 27
1 0 f t 0 , 0 h
27 f 0, 27 43200
3.69 106 27 4 2.33 105 27 3
6
27 5.33 10 27 33 43200
1.35 103 27 2 5.42 102 27 5.588
27 0.0020893 43200
63.258C
43200 1 63.258C
5
Step 2:
i 1, t1 43200, 1 63.258
2 1 f t1, 1 h
63.258 f 43200, 63.258 43200
3.69 106 63.2584 2.33 105 63.2583
63.258 5.33 10
6 63.258 33 43200
3 2 2
1.35 10 63.258 5.42 10 63.258 5.588
63.258 0.0092607 43200
463.32C
86400 2 463.32C
6
% Contoh Matlab Metode Euler untuk menyelesaikan persamaan
diferensial
clc;
close all;
clear all;
% contoh dy/dx = 4x + x2 ;
7
% Metode euler
for i = 1:m ;
y(i+1)=y(i)+num(x(i))* h ;
x(i+1)= x(1) + i*h ;
end
plot(x,y) ;
% jawaban exact
xec=[x(1):0.5:xn];
% jawaban exact
Yec=exact(xec)+C ;
hold on ;
plot(xec,Yec);
8
function y=exact(x);
y = 2*x.^2 +(x.^3)/3 ;
end
function y=num(x);
y = 4*x+x^2 ;
end
9
Heun Method
This method estimates two gradients at intervals, that is at the beginning and
end points. The best value is obtained from averaging between the gradient
at the beginning and end points. The gradient at the earliest end of the
interval obtained by the Euler Method.
yi01 yi f (x i , y) x
f (x i , yi ) f (x i 1 , yi01 )
yi 1 yi x
2
10
% Contoh Matlab Metode Heun untuk menyelesaikan
persamaan diferensial
clc;
close all;
clear all;
% contoh y = 4*x+x^2 ;
% Memasukkan nilai awal, misal y(1)= 2 ;
x(1) = input('nilai x awal = ') ;
y(1) = input('nilai y awal = ‘) ;
% Memasukkan nilai akhir x
xn = input('nilai akhir x= ’) ;
% number of time step of x
m = input('number of time step = ') ;
h= (xn-x(1))/m ;
11
% Metode Heun
for i = 1:m ;
yo(i+1)=y(i)+num(x(i))* h ;
x(i+1)= x(1) + i*h ;
y1=num(x(i));
y2=num(x(i+1));
y(i+1)=y(i)+((y1+y2)/2)*h ;
end
plot(x,y,'-ro') ;
% Membandingkan dengan jawaban exact
xec=[x(1):0.5:xn];
% Mencari koefisien tak tentu dari penyelesaian exact dari
nilai awal
C = y(1)- exact(x(1)) ;
% Jawaban exact
Yec=exact(xec)+C ;
hold on ;
plot(xec,Yec);
legend('numerical','exact');
xlabel('X')
ylabel('Y')
12
function y=exact(x);
y = 2*x.^2 +(x.^3)/3 ;
end
function y=num(x);
y = 4*x+x^2 ;
end
13
Runge-Kutta 2nd Order Method
dy
f (x, y), y(0) y0
dx
Runge Kutta 2nd order method is given by
yi 1 yi a1k1 a 2 k 2 h
where
k1 f x i , yi k 2 f x i p1h, yi q11k1h
a. Heun’s method
1 1
a1 a2 p1 1 q11 1
2 2
resulting in
1 1
yi 1 yi k1 k 2 h
2 2
where
k1 f x i , yi k 2 f x i h, yi k1h
14
b. Midpoint Method
1 1
a1 0 a2 1 p1 q11
2 2
yi 1 yi k 2 h
1 1
k1 f x i , yi k 2 f x i h, yi k1h
2 2
c. Ralston’s Method
1 2 3 3
a1 a2 p1 q11
3 3 4 4
1 2
yi 1 yi k1 k 2 h
3 3
3 3
k1 f x i , yi k 2 f x i h, yi k1h
4 4
15
Example
A solid steel shaft at room temperature of 27°C is needed to be contracted so it can
be shrunk-fit into a hollow hub. It is placed in a refrigerated chamber that is
maintained at −33°C. The rate of change of temperature of the solid shaft q is given
by
dθ 3 .6 9 1 0 6 θ 4 2.3 3 1 0 5 θ 3 1 .3 5 1 0 3 θ 2
5 .3 3 1 0 6 θ 3 3
dt 5 .4 2 1 0 2 θ 5 .5 8 8
0 2 7 C
Find the temperature of the steel shaft after 24 hours. Take a step size of h = 43200
seconds.
dθ
dt
5 .3 3 1 0 6 3.69 10 6 θ 4 2.3 3 10 5 θ 3 1.35 10 3 θ 2 5.4 2 1 0 2 θ 5.5 88 θ 33
f t, θ 5 .3 3 1 0 6 3 .6 9 1 0 6 θ 4 2 .3 3 1 0 5 θ 3 1 .3 5 10 3 θ 2 5 .4 2 10 2 θ 5 .5 8 8 θ 3 3
1 1
i 1 i k1 k 2 h
2 2
16
Solution
Step 1: For i 0, t 0 0, 0 27
1 1 1 1
1 0 k1 k 2 h 27 0.0020893 0.0092607 43200
2 2 2 2
27 0.0056750 43200 218.16C
17
Step 2: i 1, t1 43200, 1 218.16C
1 1 1 1
2 2 2
2 1 k1 k 2 h 218.16 8.4304 1.2638 1017 43200
2
218.16 6.3190 1016 43200 2.7298 1021C
18
The exact solution to this nonlinear equation at t=86400s is
86400 26.099C
19
Comparison of Euler and the Runge-Kutta 2nd Order methods
20
Runge-Kutta 4th Order Method
dy
For f (x, y), y(0) y0
dx
where
k1 f x i , yi
1 1
k 2 f x i h, yi k1h
2 2
1 1
k 3 f x i h, yi k 2 h
2 2
k 4 f x i h, yi k 3h
21
Example
A solid steel shaft at room temperature of 27°C is needed to be contracted so that
it can be shrunk-fit into a hollow hub. It is placed in a refrigerated chamber that
is maintained at −33°C. The rate of change of temperature of the solid shaft q is
given by
dθ 3 .6 9 1 0 6 θ 4 2 .3 3 1 0 5 θ 3 1 .3 5 1 0 3 θ 2
5 .3 3 1 0 6 θ 3 3
dt 5 .4 2 1 0 2 θ 5 .5 8 8
θ 0 27 C
Find the temperature of the steel shaft after 24 hours. Take a step size of h = 43200
seconds.
1
i 1 i k1 2k 2 2k 3 k 4 h
6
22
Solution
Step 1: i 0, t 0 0, 0 27C
6 4 5 3
6 3.69 10 27 2.33 10 27 27 33 0.0020893
k1 f t 0 , 0 f 0, 27 5.33 10
3 2 2
1.35 10 27 5.42 10 27 5.588
1 1 1 1
k 2 f t 0 h, 0 k1h f 0 43200, 27 0.0020893 43200 f 21600, 18.129
2 2 2 2
3.69 106 18.129 4 2.33 105 18.129 3
5.33 10 6 18.129 33 0.00035761
3 2 2
1.35 10 18.129 5.42 10 18.129 5.588
1 1 1 1
k 3 f t 0 h, 0 k 2 h f 0 43200 , 27 0.00035761 43200 f 21600, 19.276
2 2 2 2
3.69 106 19.276 4 2.33 105 19.276 3
5.33 10 6 19.276 33 0.0018924
3 2 2
1.35 10 19.276 5.42 10 19.276 5.588
1
k 4 f t 0 h, 0 k 3h f 0 43200, 27 0.0018924 43200 f 43200, 54.751
2
3.69 106 54.7514 2.33 105 54.7513
5.33 10 6 54.751 33 0.0035147
3 2 2
1.35 10 54.751 5.42 10 54.751 5.588
23
1
1 0 (k1 2k 2 2k 3 k 4 )h
6
1
27 0.0020893 2 0.00035761 2 0.0018924 0.0035147 43200
6
1
27 0.010104 43200
6
45.749C
t t1 t 0 h 0 43200 43200s
43200 1 45.749C
24
Step 2: i 1, t1 43200, 1 45.749
3.69 106 45.749 4 2.33 105 45.749 3
k1 f t1, 1 f 43200, 45.749 5.33 10 6 45.749 33
3 2 2
1.35 10 45.749 5.42 10 45.749 5.588
0.00084673
1 1 1 1
k 2 f t1 h, 1 k1h f 43200 43200 , 45.749 0.00084673 43200 f 64800, 64.038
2 2 2 2
6 4 5 3
6 3.69 10 64.038 2.33 10 64.038
64.038 33 0.010012
5.33 10
3 2 2
1.35 10 64.038 5.42 10 64.038 5.588
1 1 1 1
k 3 f t1 h, 1 k 2 h f 43200 43200 , 45.749 0.010012 43200 f 64800, 262.01
2 2 2 2
3.69 106 262.014 2.33 105 262.013
5.33 10 6 22.588 33 21.636
3 2 2
1.35 10 262.01 5.42 10 262.01 5.588
k 4 f t1 h, 1 k 3h f 43200 43200, 45.749 21.636 43200 f 86400, 9.3474 105
4 3
3.69 106 9.3474 105 2.33 105 9.3474 105
5.33 106 9.3474 105 33 1.4035 1019
2
1.35 10 9.3474 10 5.42 10 9.3474 10 5.588
3 5 2 5
25
1
2 1 k1 2k 2 2k 3 k 4 h
6
1
45.749 0.00084673 2 0.010012 2 21.636 1.4035 1019
6
43200
1
45.749 1.4035 1019 43200
6
1.0105 1023C
26
The solution to this nonlinear equation at t=86400s is
(86400) 26.099C
27
% Contoh Metode Runge Kutta orde 4 untuk menyelesaikan
persamaan diferensial
clc;
close all;
clear all;
28
% Metode Runge Kutta orde 4
for i =1:m ;
k1=num1(x(i),y(i));
k2=num1((x(i)+0.5*h),(y(i)+0.5*k1*h));
k3=num1((x(i)+0.5*h),(y(i)+0.5*k2*h));
k4=num1((x(i)+0.5*h),(y(i)+0.5*k3*h));
y(i+1)=y(i)+(k1+2*k2+2*k3+k4)*h/6 ;
x(i+1)= x(1) + i*h;
end
function f=num1(x,y)
f = 4*x+x^2+y;
end
29
Runge Kutta Persamaan Diferensial Orde Dua
d2x dx
t 4x
2 dt
dt
Jawab :
d2x dx
t 4x (1)
2 dt
dt
30
Misalkan:
d2x dx
t 4x (1)
dx 2 dt
y dt
dt
Maka :
d2x dy
dt 2 dt
Persamaan (1) menjadi :
dy
t y 4x
dt
Sehingga didapatkan dua persamaan diferensial berikut ;
dx dy
y ; t y 4x
dt dt
x(0) 3 ; y(0) 0
31
Penyelesaian dengan Runge-Kutta orde 4 :
Misal :
f t, x, y y ; g t , x, y t y 4x
x 0 3 ; y0 0 ; t 0 0 (syarat awal)
Maka :
1
x(t 0 h) x 0 k1 2k 2 2k 3 k 4 (2)
6
1
y(t 0 h) y0 L1 2L2 2L3 L4 (3)
6
32
h k L
k 2 hf (t 0 , x 0 1 , y0 1 ) (0.1)f (0.05,3, 0.6) (0.1). 0.6 0.06
2 2 2
h k1 L1
L 2 h g(t 0 , x 0 , y0 ) (0.1). g(0.05,3, 0.6)
2 2 2
0.1 0.05 .(0.06 4.3 1.203
h k L
k 3 hf (t 0 , x 0 2 , y0 2 ) (0.1)f (0.05, 2.92, 0.6015)
2 2 2
(0.1). 0.6015 0.06
h k L
L3 h g(t 0 , x 0 2 , y0 2 ) (0.1). g(0.05, 2.93, 0.6015)
2 2 2
0.1 0.05 .(0.6015 4.(2.93 1.191
1
x(t 0 h) 3 0 2.(0.06) 2.(0.06015) (0.1191) = 2.9401
6
1
y(t 0 h) 0 1.2 2.(1.203) 2.( 1.191) (0.1187) = 1.1
6
dx
(0.1)= 1.1
dt
34
Tugas: Buat Matlab code Runge Kutta orde 4
d2x dx
t 4x
dt 2 dt
Syarat awal :
dx
x 3 dan 0 pada t = 0
dt
Buat grafik hubungan x dan t untuk x =3 sampai x =10
35