Numerical Solution of ODE
Numerical Solution of ODE
Euler Method:
dy
=f ( x , y ) , y (x ¿¿ 0)= y 0 ¿ (1)
dx
Suppose we wish to solve the eq.(1) for the values of y at
x=x n=x 0 +nh , n=1 ,2 , 3 ,. .
y 1= y 0 +∫ f ( x , y ) dx (2)
x0
y 2= y 1 +∫ f ( x , y ) dx
x1
Example 1: Use the Euler method solve the following differential Equation
dy 2
=−2 x y , y ( 0 )=1 for h=0.2 over the interval [0,1].
dx
Solution: we have
y n +1= y n + hf (x ¿ ¿ n , y n) , n=0 ,1 , 2 ,3 , 4 ¿
Example 2: Use the Euler method solve the following differential Equation
dy
=2 y /x , y (1 )=2 for h=0.25 over the interval [0,1].
dx
Solution: We have
y n +1= y n + hf (x ¿ ¿ n , y n), n=0 ,1 , 2 ,3 , 4 ¿…….
Where y (n1 ) is the nth approximation to y 1. The iteration formula (5) can be started
by choosing y (01 ) from Euler’s formula.
Example 2: Use the Modified Euler method solve the following differential
dy
Equation dx =2 y /x , y (1 )=2 for h=0.25 over the interval [0,1].
Solution: We have
h
= y 0+ [f ( x 0 , y 0 ) +f ( x 1 , y 1 ) ],n=0 , 1 ,2 , … …
(n +1) (n)
y1
2
h 0.25
Therefore y 1 = y 0 + 2 [ f ( x 0 , y 0 ) + f ( x 1 , y 1 ) ]=2+ 2 [ 4+ 4.8 ] =3.1
(1 ) ( 0)
3.1
Now, f ( x 1 , y 1 ) =2 × 1.25 =4.96
(1 )
h 0.25
Therefore y 1 = y 0 + 2 [ f ( x 0 , y 0 ) + f ( x 1 , y 1 ) ] =2+ 2 [ 4 +4.96 ] =3.12
(2 ) ( 1)
3.12
Now, f ( x 1 , y 1 ) =2 × 1.25 =4.992
(2 )
h 0.25
Therefore y 1 = y 0 + 2 [ f ( x 0 , y 0 )+ f ( x 1 , y 1 ) ]=2+ 2 [ 4+ 4.992 ]
(3 ) ( 2)
¿ 3.124
∴ y (1.25 )=3.12
Here x 1=1.25 , y 1=3.12 , f ( x 1 , y 1) =4.992
y 2 = y 1 +h [ f ( x 1 , y 1) ]=3.12+0.25 × 4.992=4.368
(0 )
4.368
Further x 2=1.5 , and f ( x 2 , y 2 )=2 × 1.5 =5.824
(0 )
h 0.25
Now y 2 = y 1 + 2 [ f ( x 1 , y 1 ) +f ( x2 , y 2 ) ] =3.12+ 2 [ 4.992+5.824 ]
(1 ) (0 )
¿ 4.472
4.472
Now, f ( x 2 , y 2 ) =2 × 1.5 =5.9627
(1 )
h 0.25
Therefore y 2 = y 1 + 2 [ f ( x1 , y 1 ) +f ( x 2 , y 2 ) ]=3.12+ 2 [ 4.992+ 5.9627 ] =4.4893
(2 ) ( 1)
4.4893
Now, f ( x 2 , y 2 ) =2 × 1.5 =5.9857
(2 )
h 0.25
Therefore y 2 = y 1 + 2 [ f ( x 1 , y 1 ) +f ( x2 , y 2 ) ]=3.12+ 2 [ 4.992+5.9857 ] =4.4922
(3 ) (2 )
∴ y (1.5 )=4.49
5.9867
Further x 3=1.75 , and f ( x 3 , y 3 )=2 × 1.75 =6.8419
(0 )
h 0.25
therefore y 3 = y 2 + 2 [ f ( x 2 , y 2 ) +f ( x3 , y 3 ) ] =4.49+ 2 [ 5.9867+ 6.8419 ] =6.0936
(1 ) (0 )
6.0936
Now, f ( x 3 , y 3 ) =2 × 1.75 =6.9641
(1 )
h 0.25
(2 )
y3 = y2+
2
[ ]
f ( x 2 , y 2 ) + f ( x3 , y (31 )) =4.49+
2
[ 5.9867+6.9641 ] =6.1089
6.1089
Now, f ( x 3 , y 3 ) =2 × 1.75 =6.9816
(2 )
h 0.25
(3 )
y3 = y2+
2
[ ]
f ( x 2 , y 2 ) + f ( x 3 , y (32 )) =4.49+
2
[ 5.9867+ 6.9816 ] =6.1110
∴ y (1.75 )=6.11
7.8557
Further x 4 =2.0 , and f ( x 4 , y 4 ) =2 × 2.0 =7.8557
( 0)
h 0.25
therefore y 4 = y 3 + 2 [ f ( x 3 , y 3 ) + f ( x 4 , y 4 ) ]=6.11+ 2 [ 6.9829+7.8557 ] =7.9648
(1 ) (0 )
7.9648
Now, f ( x 4 , y 4 ) =2× 2.0 =7.9648
( 1)
h 0.25
(2 )
y4 = y3+
2
[ ]
f ( x 3 , y 3 ) + f ( x 4 , y (41) ) =6.11+
2
[ 6.9829+7.9648 ] =7.9785
7.9785
Now, f ( x 4 , y 4 ) =2× 2.0 =7.9785
( 2)
h 0.25
(3 )
y4 = y3+
2
[ ]
f ( x 3 , y 3 ) + f ( x 4 , y (42) ) =6.11+
2
[ 6.9829+7.9785 ] =7.9802
∴ y ( 2.0 )=7.98
Runge-Kutta Method:
Example 2: Use the Runge-Kutta second order (RK-2) method solve the
dy
following differential Equation dx =2 y /x , y (1 )=2 for h=0.25 over the interval [1,2].
k 1=h∗f ( x i , y i )
k 2=h∗f ( x i+ h , y i+ k 1 )
k 1=h × f ( x 1 , y 1) =0.25 × 2 × ( )
3.1
1.25
=1.24
k 2=0.25 × f ( x 1 +h , y 1 +k 1 ) =0.25 × f ( 1.5 , 4.34 )=1.4467
1
y 2= y 1 + [ k + k ) ¿=3.1+0.5 ( 1.24+ 1.4467 )=4.4433
2 1 2
y (1.5)=4.4433
(
k 1=h × f ( x 2 , y 2 )=0.25 × 2 ×
4.4433
1.5 )
=1.4811
(
k 1=h × f ( x 3 , y 3 )=0.25 × 2 ×
6.0302
1.75 )
=1.7229
Example 2: Use the Mid Point method solve the following differential Equation
dy
=2 y /x , y (1 )=2 for h=0.25 over the interval [0,1].
dx
Solution: We have Mid-Point method
y i+ 1= y i+ k 2 ,i=0 ,1 , 2 , … …
k 1=h∗f ( x i , y i )
k 2=h∗f ( x i+ h/2 , y i+ k 1 /2 )
( 21 )=1
k 1=h × f ( x 0 , y 0 )=0.25× 2 ×
y ( 1.25 )=3.1111
(
k 1=h × f ( x 1 , y 1) =0.25 × 2 ×
3.1111
1.25 )
=1.2444
y ( 1.5 )=4.4687
(
k 1=h × f ( x 2 , y 2 )=0.25 × 2 ×
4.4687
1.5 )
=1.4896
y ( 1.75 )=6.0728
(
k 1=h × f ( x 3 , y 3 )=0.25 × 2 ×
6.0728
1.75 )
=1.7351
y ( 2.0 )=7.9236
Example 2: Use the RK-4 method solve the following differential Equation
dy
=2 y /x , y (1 )=2 for h=0.25 over the interval [0,1].
dx
Solution: We have RK-4 method
y i+ 1= y i+ [ k 1 +2 k 2 +2 k 3 +k 4 ) ¿/6 , i=0 , 1 ,2 , … …
k 1=h∗f ( x i , y i )
k 2=h∗f ( x i+ h/2 , y i+ k 1 /2 )
k 3=h∗f ( x i +h/2 , y i+ k 2 /2 )
k 4=h∗f ( xi +h , y i +k 3 )
y ( 1.25 )=3.1247
y ( 1.5 )=4.4994
y ( 1.75 )=6.1240
For i=3 ; x 3=1.75 , y 3=6.1240, h=0.25, we have
(
k 1=h∗f ( x 3 , y 3 ) =0.25 × f ( 1.75 , 6.1240 ) =0.25 × 2×
6.1240
1.75 )=1.7497
y ( 2.0 )=7.9986
Table 1: Example of 2 by different methods.
x Euler Modified Euler RK-2 Mid Point RK-4 Exact y=2 x 2
x=1.25 3.0 3.12 3.10 3.1111 3.1247 3.125
x=1.50 4.2 4.49 4.4433 4.4687 4.4994 4.50
x=1.75 5.6 6.11 6.0302 6.0728 6.1240 6.125
x=2.00 7.2 7.98 7.8608 7.9236 7.9986 8.00