Differential Equation
Differential Equation
Differential Equation
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.
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 )
yn+1 = yn + hf (xn , yn )
1.1.1 Algorithm:
1. Define the function: f (x, y)
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
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
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
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
b. Output
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
b. Output
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
14