Anda di halaman 1dari 35

Ordinary Differential Equation

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

Graphical interpretation of the first step of Euler’s method

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

yi+1 Predicted value


Φ
yi
h
Step size

xi xi+1 x

General graphical interpretation of Euler’s method

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

dθ   3.6 9  1 0  6 θ 4  2 .33  1 0  5 θ 3  1.3 5  10  3 θ 2 


  5.3 3  10  6    θ  33 
dt   5.4 2  1 0  2 θ  5.5 88 
 
  0   27  C

Using Euler’s method, find the temperature of the steel shaft after 86400 seconds.
Take a step size of h = 43200 seconds.


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 106  27 4  2.33 105  27 3  
 6
 27   5.33  10    27  33  43200
  1.35 103  27 2  5.42  102  27   5.588  
   
 27   0.0020893 43200
 63.258C

1 is the approximate temperature at :


t  t1  t 0  h  0  43200  43200s

  43200   1  63.258C

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 106  63.2584  2.33 105  63.2583  
 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.32C

2 is the approximate temperature at :

t  t 2  t1  h  43200  43200  86400 s

  86400   2  463.32C

6
% Contoh Matlab Metode Euler untuk menyelesaikan persamaan
diferensial
clc;
close all;
clear all;

% contoh dy/dx = 4x + x2 ;

% 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 ;

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];

% 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);

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.

yi01  yi  f (x i , y) x

f (x i , yi )  f (x i 1 , yi01 )
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.


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

  3.69 106  27 4  2.33  105  27 3  


k1  f  t 0 , o   f  0, 27    5.33 106    27  33 
  3 2 2  
  1.35  10  27   5.42  10  27   5.588  
 0.0020893

k 2  f  t 0  h, 0  k1h   f  0  43200, 27   0.0020893 43200   f  43200, 63.278 


  3.69 106  63.278 4  2.33 105  63.278 3  
  5.33  106    63.278  33 
  3 2 2  
  1.35  10  63.278   5.42  10  63.278   5.588  
 0.0092607

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.16C

17
Step 2: i  1, t1  43200, 1  218.16C

k1  f  t1, 1   f  43200, 218.16 


  3.69 106  218.16 4  2.33 105  218.16 3  
  5.33 106    218.16  33 
  3 2 2  
  1.35  10  218.16   5.42  10  218.16   5.588  
 8.4304
k 2  f  t1  h, 1  k1h   f  43200  43200, 218.16   8.4304  43200   f  86400, 364410 
  3.69 106 (364410)4  2.33 105 (364410)3  
6
  5.33 10    364410  33 
 3 2 2
 1.35 10  364410   5.42 10  364410   5.588  
   
 1.2638 1017

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  1021C

18
The exact solution to this nonlinear equation at t=86400s is
  86400   26.099C

Comparison of Euler and the Runge-Kutta 2nd Order methods

Step size, (86400)


h Euler Heun Midpoint Ralston
86400 −153.52 −58466 −774.64 −12163
43200 −464.32 −2.7298×1021 −0.33691 −19.776
21600 −29.541 −24.537 −24.069 −24.268
10800 −27.795 −25.785 −25.808 −25.777
5400 −26.958 −26.027 −26.039 −26.032

  86400   26.099C  exact 

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

Runge Kutta 4th order method is given by


1
yi 1  yi   k1  2k 2  2k 3  k 4  h
6

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.

dθ   3.69  1 0  6 θ 4  2.33  10  5 θ 3  1 .35  10  3 θ 2 


  5 .33  10  6    θ  33 
dt   5.42  1 0  2 θ  5.588 
 
  3 .6 9  1 0  6 θ 4  2 .3 3  1 0  5 θ 3  1 .3 5  1 0  3 θ 2 
f  t, θ    5 .3 3  1 0  6   θ  3 3 
  5 .4 2  1 0  2 θ  5 .5 8 8 
 

1
i 1  i   k1  2k 2  2k 3  k 4  h
6
22
Solution

Step 1: i  0, t 0  0, 0  27C

  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  106  18.129 4  2.33  105  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  106 19.276 4  2.33  105 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  106  54.7514  2.33  105  54.7513  

 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.749C

1 is the approximate temperature at

t  t1  t 0  h  0  43200  43200s

  43200   1  45.749C

24
Step 2: i  1, t1  43200, 1  45.749
  3.69 106  45.749 4  2.33 105  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 106  262.014  2.33  105  262.013  

 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 106  9.3474 105   2.33 105  9.3474  105   
  5.33 106    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  1023C

2 is the approximate temperature at :

t 2  t1  h  43200  43200  86400

  86400   2  1.1015 1023C

26
The solution to this nonlinear equation at t=86400s is

(86400)  26.099C

Value of temperature at 86400 seconds for different step sizes

Step Size, h   86400  Et |t | %

86400 −5.3468×1028 −5.2468×1028 2.0487×1029


43200 −1.0105×1023 −0.034808×1023 3.8718×1023
21600 −26.061 −0.038680 0.14820
10800 −26.094 −0.0050630 0.019400
5400 −26.097 −0.0015763 0.0060402

  86400   26.099C  exact 

27
% Contoh Metode Runge Kutta orde 4 untuk menyelesaikan
persamaan diferensial
clc;
close all;
clear all;

% contoh dy/dx = 4*x+x^2+y ;


% 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 ;

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

k1  hf (t 0 , x 0 , y0 )  (0.1)f (0,3,0)  (0.1).0  0

L1  h. g(t 0 , x 0 , y0 )  (0.1). g(0,3,0)  (0.1). (0).(0)  4.3  1.2

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

k 4  hf (t 0  h, x 0  k 3 , y0  L3 )  (0.1)f (0.05, 2.93  1.191)


 (0.1).(1.191)  0.1191

L 4  hg(t 0  h, x 0  k 3 , y0  L3 )  (0.1)g(0.05, 2.93  1.191)


 (0.1). (0.1)( 1.191)  4.(2.93)   1.187 33
Substitusi k1 , k2 , k3 dan k4 pada persamaan (2)

1
x(t 0  h)  3   0  2.(0.06)  2.(0.06015)  (0.1191)  = 2.9401
6

Substitusi L1 , L2 , L3 dan L4 pada persamaan (2)

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

Anda mungkin juga menyukai