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

Unit VIII (Part B) Numerical Solutions of Ordinary Differential Equations

Many engineering problems can be modelled as ordinary differential equations. The solution of
these equations using numerical techniques is an important subject matter.
A simple ordinary differential equation

f l ( x , y)  f ( x, y)  k
d
Where f l ( x , y)  f ( x, y )
dx

Example 1: f l ( x , y)   2e 2 x
The complete solution of this differential equation is

f ( x, y)  e 2 x
Fig. 1 shows the plot of this.

Fig.1: Actual Solution of given ODE


1.2

0.8

0.6

0.4

0.2

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3

One can solve this ODE using numerical techniques. One needs to keep in mind that numerical
solutions produce approximate solutions. There is always an error between true value and the
value computed through numerical methods.
The numerical methods used to solve ODEs are commonly referred Runge-Kutta Methods or RK
Methods. The various RK methods are
(i) RK Method of order 1
(ii) RK Method of order 2
(iii) RK Method of Order 3

Page | 1
Unit VIII (Part B) Numerical Solutions of Ordinary Differential Equations

(iv) RK Method of order 4


These methods differ in complexity with increase in order, but error between true value and
numerically computed value reduces
To solve any ODE by numerical methods two things should be known beforehand i.e. starting
value (Initial Value) and step size (h). Then and only then one can proceed ahead.
The various RK methods are now discussed.
RK Method of Order 1: This is one of the simplest method. This is also known as Euler’s
Method. This is explained below

Assume initial value is given at (x0 , y0) and h is step size,


Then next point where f(x,y) has to be computed is ( x0+h, y0+h)

f ( x0  h, y0  h)  f ( x0 , y0 )  h f l ( x0 , y0 )
f l (x0 , y0) is the value of derivative at (x0 , y0)
Once f( x0+h, y0+h) is known one can compute f( x0+2h, y0+2h) as

f ( x0  2h, y0  2h)  f ( x0  h, y0  h)  h f l ( x0  h, y0  h)
Once f( x0+2h, y0+2h) is known we determine next value f( x0+3h, y0+3h) as

f ( x0  3h, y0  3h)  f ( x0  2h, y0  2h)  h f l ( x0  2h, y0  2h)


Proceeding this way one can determine the numerical solution of the equation.

Example: f l ( x , y)   2e 2 x and f(0,0) =1


Take step size h (i) 0.4, (ii) 0.2, (iii) 0.1
Case I: h=0.4
Now f l (0 , 0)   2e 2(0)  2
And f (0.4, 0.4)  f (0,0)  0.4(2) 1  0.8  0.2

Now f l (0.4, 0.4)   2e 2( 0.4)  2(0.4493)  0.8986


And f (0.8, 0.8)  f (0.4, 0.4)  0.4 f l (0.4, 0.4)  0.2  0.4(0.8986)   0.1596

Now f l (0.8, 0.8)   2e 2( 0.8)  2(0.20189 )  0.4037

And f (1.2,1.2)  f (0.8, 0.8)  0.4 f l (0.8, 0.8)  0.1596  0.4(0.4037 )   0.3211

Now f l (1.2,1.2)   2e 2(1.2)  2(0.0907 )  0.1814

And f (1.6,1.6)  f (1.2,1.2)  0.4 f l (1.2,1.2)  0.3211  0.4(0.1814)   0.3937

Page | 2
Unit VIII (Part B) Numerical Solutions of Ordinary Differential Equations

Now f l (1.6,1.6)   2e 2(1.6)  2(0.04076 )  0.0815

And f (2,2)  f (1.6,1.6)  0.4 f l (1.6,1.6)  0.3937  0.4(0.0815)   0.4263


Case II: h=0.2
Now f l (0 , 0)   2e 2( 0)  2
And f (0.2, 0.2)  f (0,0)  0.2(2) 1  0.4  0.6

Now f l (0.2, 0.2)   2e 2(0.2)  2(0.6703)  1.3406


And f (0.4, 0.4)  f (0.2, 0.2)  0.2 f l (0.2, 0.2)  0.6  0.2(1.3406)  0.3319

Now f l (0.4, 0.4)   2e 2( 0.4)  2(0.4493)  0.8987


And f (0.6, 0.6)  f (0.4, 0.4)  0.2 f l (0.4, 0.4)  0.3319  0.2(0.8987 )  0.1521

Now f l (0.6, 0.6)   2e 2(0.6)  2(0.3012)  0.6024


And f (0.8,0.8)  f (0.6, 0.6)  0.2 f l (0.6, 0.6)  0.1521  0.2(0.6024)  0.03162

Now f l (0.6, 0.6)   2e 2( 0.6)  2(0.3012)  0.6024

And f (0.8,0.8)  f (0.6, 0.6)  0.2 f l (0.6, 0.6)  0.1521  0.2(0.6024)  0.03162
Now f l (0.8, 0.8)   2e 2(0.8)  2(0.2019)  0.4038

And f (1,1)  f (0.8,0.8)  0.2 f l (0.8,0.8)  0.03162  0.2(0.4038)   0.04914

Now f l (1,1)   2e 2(1)  2(0.1353)  0.2707


And f (1.2,1.2)  f (1,1)  0.2 f l (1,1)  0.04914  0.2(0.2707 )   0.1033

Now f l (1.2,1.2)   2e 2(1.2)  2(0.0907 )  0.1814


And f (1.4,1.4)  f (1.2,1.2)  0.2 f l (1.2,1.2)  0.1033  0.2(0.1814)   0.1396

Now f l (1.4,1.4)   2e 2(1.4)  2(0.0608)  0.1216

And f (1.6,1.6)  f (1.4,1.4)  0.2 f l (1.4,1.4)  0.1396  0.2(0.1216)   0.1639


Now f l (1.6,1.6)   2e 2(1.6)  2(0.04076 )  0.0815

And f (1.8,1.8)  f (1.6,1.6)  0.2 f l (1.6,1.6)  0.1639  0.2(0.0815)   0.1802


Now f l (1.8,1.8)   2e 2(1.8)  2(0.0273)  0.0546

And f (2,2)  f (1.8,1.8)  0.2 f l (1.8,1.8)  0.1802  0.2(0.0546)   0.1911

Case III: h=0.1


Now f l (0 , 0)   2e 20  2

Page | 3
Unit VIII (Part B) Numerical Solutions of Ordinary Differential Equations

And f (0.1, 0.1)  f (0,0)  0.1(2) 1  0.2  0.8

Now f l (0.1, 0.1)   2e 2( 0.1)  2(0.8187 )  1.6374

And f (0.2, 0.2)  f (0.1,0.1)  0.1 f / (0.1,0.1)  0.8  0.1(1.6374)  0.6363

Now f l (0.2 , 0.2)   2e 2( 0.2)  2(0.6703)  1.3406

And f (0.3, 0.3)  f (0.2, 0.2)  0.1 f / (0.2, 0.2)  0.6363  0.1(1.3406)  0.5022

Now f l (0.3, 0.3)   2e 2( 0.3)  2(0.5488)  1.0976

And f (0.4, 0.4)  f (0.3, 0.3)  0.1 f / (0.3, 0.3)  0.5022  0.1(1.0976)  0.3905

Now f l (0.4, 0.4)   2e 2( 0.4)  2(0.4493)  0.8986

And f (0.5, 0.5)  f (0.4, 0.4)  0.1 f / (0.4, 0.4)  0.3905  0.1(0.8986)  0.3006

Now f l (0.5 , 0.5)   2e 2( 0.5)  2(0.3679)  0.7358

And f (0.6,0.6)  f (0.5, 0.5)  0.1 f / (0.5, 0.5)  0.3006  0.1(0.7358)  0.2270

Now f l (0.6, 0.6)   2e 2( 0.6)  2(0.3012)  0.6024

And f (0.7, 0.7)  f (0.6, 0.6)  0.1 f / (0.6, 0.6)  0.2270  0.1(0.6024)  0.1668

Now f l (0.7, 0.7)   2e 2( 0.7 )  2(0.2466)  0.4932

And f (0.8, 0.8)  f (0.7, 0.7)  0.1 f / (0.7, 0.7)  0.1668  0.1(0.4932)  0.1175
Now f l (0.8, 0.8)   2e 2( 0.8)  2(0.2019)  0.4038
And f (0.9,0.9)  f (0.8, 0.8)  0.1 f / (0.8, 0.8)  0.1175  0.1(0.4038)  0.07712

Now f l (0.9, 0.9)   2e 2( 0.9)  2(0.1653)  0.3306

And f (1,1)  f (0.9,0.9)  0.1 f / (0.9,0.9)  0.07712  0.1(0.3306)  0.04406


Now f l (1,1)   2e 2(1)  2(0.1353)  0.2706

And f (1.1,1.1)  f (1,1)  0.1 f / (1,1)  0.04406  0.1(0.2706)  0.0.017


Now f l (1.1,1.1)   2e 2(1.1)  2(0.1108)  0.2216

And f (1.2,1.2)  f (1.1,1.1)  0.1 f / (1.1,1.1)  0.017  0.1(0.2216)  0.0052


Similarly proceeding one can compute f(1.6,1.6) and f(2,2).
The plot is shown in Fig.2 for h=0.1, h=0.2 and h=0.4
From the plot one easily finds that error reduces with decrease in step size.
Hence to have a better numerical solution the step size should be taken very small. However this
increases the number of iterations.

Page | 4
Unit VIII (Part B) Numerical Solutions of Ordinary Differential Equations

Fig.2: Numerical Solution of ODE


1.2

0.8

0.6

0.4

0.2

0
0 0.5 1 1.5 2 2.5
-0.2

-0.4

-0.6

f(x,y) h=0.4 h=0.2 h=0.1

The flowchart for RK Method of order 1 is given below;

Page | 5
Unit VIII (Part B) Numerical Solutions of Ordinary Differential Equations

START

Enter starting point (x0 , y0 )


Enter Initial Value f(x0 , y0 )
Enter Step Size h
Enter number of Steps n

Set counter I =1

Output I, (x0 , y0 ), f(x0 , y0 )

x =x0 y= y0

Compute f l ( x, y)

Compute f(x) at (x+h, y+h) using

Output I , (x+h, y+h) , f(x+h, y+h)

I = I+1; x = x+h; y = y+h

No Is I=n Yes

Stop

End

Page | 6
Unit VIII (Part B) Numerical Solutions of Ordinary Differential Equations

Runge-Kutta Second order Method for ODE


RK first order method for determining numerical solution to an ordinary differential equation is
not very accurate and it is hardly used.
The error can be reduced by reducing step size. However that increases the computations.
A slightly better accurate method is RK second order method.
In first order method the slope at the beginning of each step is assumed to be slope of entire step.
However in second order RK method, the slope of the mid-point of the step (interval) is assumed
as the slope of entire range of step h.

In second order RK method we have


1
f ( x  h, y  h)  f ( x, y )  h K 2
2
 1 1 
K 2  f l  x  h , y  K1 h 
 2 2 
K1  f / ( x , y)
Here basic algorithm is
Step 1: Note down the initial value, step (interval) size h
Step 2: Determine slope at the beginning of the step (interval) i.e. K1.
Step 3: Use K1 to determine the midpoint of the step (interval)
Step 4: Determine the slope at the midpoint of the interval. This is K2.
Step 5: Determine the value of the function over the entire step (interval) using K2.
Step6: Determine the end point of the step (interval)
Step7: Repeat from step 2 if number of computations not over.
Home job: Sketch flow chart for RK method of order 2.
Example: f l ( x , y)   2e 2 x and f(0,0) =1 for h=0.4
Now K1  f l (0, 0)   2 e 2( 0)   2
 1 1 
K 2  f l  0  (0.4), 0  ( K1 )(0.4) 
 2 2 
 
K 2  f l  0.2 , (2)(0.4)   f l 0.2,0.4)   2e 2( 0.2)  2(0.6703)   1.3406
1
 2 
And f (0.4,0.4)  f (0 , 0)  K 2 h  1  (0.4)(1.3406) 1  0.53624  0.46376
Now K1  f l (0.4, 0.4)   2 e 2( 0.4)   2(0.4493)   0.8986
 1 1 
K 2  f l  0.4  (0.4), 0.4  ( K1 )(0.4) 
 2 2 
 
K 2  f l  0.6 , 0.4  (0.8986)(0.4)   f l 0.6 ,  0.1797 )    2e 2( 0.6)  2(0.3012)   0.6024
1
 2 
And f (0.8,0.8)  f (0.4 , 0.4)  K 2 h  0.46376  (0.4)(0.6024)  0.2228
Next we calculate f(1.2,1.2) as follows

Page | 7
Unit VIII (Part B) Numerical Solutions of Ordinary Differential Equations

Now K1  f l (0.8 , 0.8)   2e 2(0.8)   2(0.2019)   0.4038

 1 1 
K 2  f l  0.8  (0.4) , 0.8  (0.4038)(0.4) 
 2 2 
K 2  f l 1, 0.7192    2e 2(1)  2(0.1353)  0.2706

And f (1.2 ,1.2 )  f (0.8 , 0.8)  0.4(0.2706)  0.2228  0.10284  0.1146

Similarly one can determine f(1.6, 1.6 ) and f(2, 2)

Runge-Kutta Method of Order3


The accuracy of RK method of order 2 is not very high and is rarely used. Another method
known as RK Method of order 3 has still better accuracy. It is stated as
f ( x  h , y  h)  f ( x , y)  (2K1  3K 2  4K3 )h
Where K1  f ( x , y )
 h h 
K 2  f  x  , y  K1 
 2 2 
 3 3 
K3  f  x  h , y  h K 2 
 4 4 
In the order 3 method slot at beginning of step interval, at middle of step interval and at three-
fourth of step interval is calculated and then average of three slopes is used to calculate next
value.
Runge-Kutta Method of Order 4
The RK method of order 4 is commonly used as it gives highest accuracy as compared to all
other methods. According to this method we have
f ( x  h , y  h)  f ( x , y)  ( K1  2 K 2  2K 3  K 4 )h
Where K1  f ( x , y )
 h h 
K 2  f  x  , y  K1 
 2 2 
 1 1 
K3  f  x  h , y  h K 2 
 2 2 
K 4  f x  h , y  h K 3 
In the order 3 method slot at beginning of step interval, at middle of step interval and at three-
fourth of step interval is calculated and then average of three slopes is used to calculate next
value.

Page | 8
Unit VIII (Part B) Numerical Solutions of Ordinary Differential Equations

Tutorial Problems for Practice

(Q.1) Explain the RK method of order 1. Write the algorithm and flow chart for this method.
(Q.2) Write algorithm, flowchart and computer program for RK methods of order 2, order 3 and
Orderv4.
(Q.3) Compute the solution of following ordinary differential equations.
5
(i) f l ( x, y )  5e 2 x  0 f (0, 0)  h  0.2
2
(ii) f l ( x, y)  4e 2 x  3 f (0, 0)  2 h  0.1
(iii) f l ( x, y)  Sin 2 x ; where f(0,0)= -1 and h=π/4 rad

Page | 9

You might also like