Differential Equation

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

NUMERICAL METHOD OF SOLVING ORDINARY

DIFFERENTIAL EQUATION

1 Ordinary Differential Equation(ODE)(THEORY)


Let us consider a general first order differential equation,

dy
= f (x, y) (1)
dx
with initial condition: y(x0 ) = y0
Multiple Numerical Method can be used find the solutions of the ODE starting with the initial
conditions.
The First order Differential equations is either integrated by an approximate method or the
function is approximated by Tailor Series expansion.

1.1 Euler’s Method


To solve the differential equation 1 for y at different values of x. Consider, those points are:
xn = x0 + nh, n = 1, 2, 3, · · ·
Between first two points: [x0 , x1 ],
Z x1
y1 = y0 + f (x, y)dx (2)
x0

If we assume, f (x, y) = f (x0 , y0 ), we get an approximate formula:

y1 = y0 + hf (x0 , y0 ), (3)
where h = x1 − x0 . Note that the integration is done here by Rectangle rule.
Similarly, for the next range [x1 , x2 ] :

y2 = y1 + hf (x1 , y1 )

So we can write the general formula which is called Euler’s formula:

yn+1 = yn + hf (xn , yn )

1.1.1 Algorithm:
1. Define the function: f (x, y)

2. Set initial value y0 and step size h.

3. Update y = y + h ∗ f (x, y) and x = x + h. Iterate this step in a loop.

9
1.2 Modified Euler’s Method
R x1
We considered the integral 2 : y1 = y0 + x0 f (x, y)dx. In Euler’s method, we approximated this
by Rectangle rule and obtained the expression 3: y1 ≈ y0 + hf (x0 y0 ). Now we can do better
approximation of the integral by Trapezoidal Rule:
h
y1 = y0 + [f (x0 , y0 ) + f (x1 , y1 )] (4)
2
The equation 4 can be solved iteratively. Given x0 , y0 , we may begin with an approximate value
of y1 on left. This new y1 is put back on the right again and obtain a better approximation and
so on, Thus, we solve for y1 in a self-consistent way.
1. : We predict y1 by Euler,s formula:

(0)
y0 = y0 + hf (x0 , y0 ) (5)
2. : We correct it by

(1) h (0)
y1 = y0 + [f (x0 , y0 ) + f (x1 , y1 )] (6)
2
Putting 5 into 6, we get y1 .This is thus called predictor-corrector method. Next, we use y1 to
get the next approximate value of y1 and so on . We can carry on this according to the level of
accuracy we set.
The general iteration formula:

(n) h (n−1)
y1 = y0 + [f (x0 , y0 ) + f (x1 , y1 )]
2

1.3 Runge-Kutta 2nd Order (RK2):


We started by plugging Euler’s original formula, y1 = y0 + hf (x0 , y0 ) into the modified formula
and obtained:
h
y1 = y0 + [f0 + f (x0 + h, y0 + hf0 )]
2

where f0 ≡ f (x0 , y0 )
Now we designate two slopes, and k1 = hf0 and k2 = hf (x0 + h, y0 + k1 ). Thus, we write the
following steps:

k1 = hf0
k2 = hf (x0 + h, y0 + k1 )
1
y1 = y0 + [k1 + k2 ]
2

1.4 Runge-Kutta 4th Order(RK4)


The general formula for 4th Order Runge − Kutta(RK4)M ethod :

k1 = hf (x0 , y0 )
h
k2 = hf (x0 + , y0 + k1 /2)
2
h
k3 = hf (x0 + , y0 + k2 /2)
2
k4 = hf (x0 + h, y0 + k3 )
1
y1 = y0 + (k1 + 2k2 + 2k3 + k)
6

10
2 Programs:
2.1 Euler’s Method:
dy
Problem: dx = 2xy = f (x, y), with initial condition y(−2) = 1

a. Source Code
#E u l e r ’ s Method o f S o l v i n g ODE

from m a t p l o t l i b . p y p l o t import∗
def f ( x , y ) : return 2∗ x∗y
x , y , h = −2.0 , 1 . 0 , 0 . 0 1
X , Y = [x] , [y]

while x < 2 . 0 :
y = y + h∗ f ( x , y )
x = x + h

Y. append ( y )
X. append ( x )
p l o t (X, Y)
show ( )

b. Output

Figure 1: Graphical solution using Euler Method

11
2.2 Modified Euler Method
dy
Problem: dx = 1 + x + y Given initial condition y(0) = 0 and compare the solution with actual
solution y = 2 expx −x − 2

a. Source Code
#S o l v i n g ! s t Order ODE u s i n g M o d i f i e d E u l e r Method

from numpy import∗


from m a t p l o t l i b . p y p l o t import∗
def f ( x , y ) : return 1+x+y
x0 = l i n s p a c e ( 0 , 2 , 2 0 0 )
def a c t f n ( x ) : return 2∗ exp ( x)−x−2
x ,y ,h = 0.0 ,0.0 ,0.01
X= [ ]
Y= [ ]
y1 = y+h∗ f ( x , y )
while x < 2 . 0 :
y1 = y + h∗ f ( x , y )
y = y + h ∗ 0 . 5 ∗ ( f ( x , y)+ f ( x+h , y1 ) )
x = x+h
X. append ( x )
Y. append ( y )
p l o t (X, Y, ’ ˆ ’ )
p l o t ( x0 , a c t f n ( x0 ) )
show ( )

b. Output

Figure 2: Graphical solution using Modified Euler Method

12
2.3 Runge-Kutta 2nd Order Method Method
dy
Problem: dx = (1 + x)y = f (x, y)

a. Source Code
#RK2 Method o f s o l v i n g ODE

from numpy import∗


from m a t p l o t l i b . p y p l o t import∗
def f ( x , y ) : return (1+x ) ∗ y
x= 0 . 0
y = 1.0
h = 0.001
X = []
Y = []
while x < 5 . 0 :
k1 = h∗ f ( x , y )
k2 = h∗ f ( ( x+h ) , ( y+k1 ) )
y = y + 0 . 5 ∗ ( k1+k2 )
x = x+h
Y. append ( y )
X. append ( x )
p l o t (X, Y)
show ( )

b. Output

Figure 3: Graphical solution using RK2 Method

13
2.4 Runge-Kutta 4th Order Method Method
dy
Problem: dx = x − 2xy = f (x, y)

a. Source Code
#RK4 method o f s o l v i n g ODE

from m a t p l o t l i b . p y p l o t import∗
from numpy import∗
def f ( x , y ) : return x−2∗x∗y
x = 0.0
y = 1.0
h = 0.01
Y= [ ]
X= [ ]
while x < 5 . 0 :
k1 = h∗ f ( x , y )
k2 = h∗ f ( ( x + 0 . 5 ∗ h ) , ( y + 0 . 5 ∗ k1 ) )
k3 = h∗ f ( ( x + 0 . 5 ∗ h ) , ( y + 0 . 5 ∗ k2 ) )
k4 = h∗ f ( ( x+h ) , ( y+k3 ) )
y = y +(1/6)∗( k1 + k4 + 2 . 0 ∗ ( k2 + k3 ) )
x = x+h
# print (x , y)
Y. append ( y )
X. append ( x )
p l o t (X, Y)
show ( )

b. Output

Figure 4: Graphical solution using RK4 Method

14

You might also like