You are on page 1of 5

Experiment No:- 09

Name of the Experiment:-

MATLAB program for Euler’s method and Runge-Kutta 2nd order method to solve
ordinary differential equation.

Objectives:-

Objectives of this experiment are-

1. To solve ordinary differential equation using Euler’s method and R-K 2nd order
method.
2. To compare the accuracy of the above mentioned methods.

Theory:-

We can solve ordinary differential equation using various methods among which
Euler’s & R-K 2nd order method will be applied here. Brief description of these two methods
are given below-

Euler’s method:-

Among all the methods of solving differential equations Euler’s method is the
simplest one which uses extrapolation technique to find the solution. Let us consider the
following differential equation with a given initial value-

𝑑𝑦
= 𝑓(𝑥, 𝑦) − − − −(1)
𝑑𝑥
𝑦(𝑥1 ) = 𝑦1 − − − − (2)

At (𝑥1 , 𝑦1 ) slope is-


𝑑𝑦
𝑠1 = 𝑑𝑥 (𝑥1 , 𝑦1 ) = 𝑓(𝑥1 , 𝑦1 ) − − − (3)

Now the next point on the solution curve may be extrapolated by taking a small step in a
direction given by the above slope. The next point is-

𝑦(𝑥1 + ℎ) = 𝑦1 + ℎ ∗ 𝑓(𝑥1 , 𝑦1 ) − − − (4)

In the same way we will get the next solution points. Generally-

𝑦(𝑥𝑖 + ℎ) = 𝑦𝑖 + ℎ ∗ 𝑓(𝑥𝑖 , 𝑦𝑖 ) − − − (5)

In this way we can find out the solution of a given differential equation at any desired point.
Euler’s method is a piecewise linear approximation technique.
Runge-Kutta 2nd order method:-

R-K method is the most used techniques for solving a differential equation. R-K
4 order method gives better accuracy than R-K 2nd order method. Here R-K 2nd order will be
th

applied. Heun’s method is one of the R-K 2nd order method which uses two slopes. Let us
consider the following differential equation with a given initial value-

𝑑𝑦
= 𝑓(𝑥, 𝑦) − − − −(1)
𝑑𝑥
𝑦(𝑥1 ) = 𝑦1 − − − − (2)

At (𝑥1 , 𝑦1 ) slope is-


𝑑𝑦
𝑠1 = 𝑑𝑥 (𝑥1 , 𝑦1 ) = 𝑓(𝑥1 , 𝑦1 ) − − − (3)

At (𝑥1 , 𝑦1 ) a straight line is drawn with slope 𝑠1 which cuts the vertical line at (𝑥1 + ℎ) at
point (𝑥1 + ℎ𝑦2 ′) . Now at (𝑥1 + ℎ𝑦2 ′) slope is –
𝑑𝑦
𝑠2 = 𝑑𝑥 (𝑥2 , 𝑦2 ′) = 𝑓(𝑥2 , 𝑦2 ′) − − − (4)

Again from (𝑥1 , 𝑦1 ) a straight line with slope 𝑠 is drawn which cuts the vertical line at
(𝑥1 + ℎ) at point 𝑦2 .
𝑠1 +𝑠2
𝑠= − − − −(5)
2

Now the point 𝑦2 will be equal to the following-



𝑦2 = 𝑦1 + 2 ∗ 𝑠 − − − (6)

Next solution points will be obtained in the same way. So generally we can write-

𝑦(𝑥𝑖 + ℎ) = 𝑦𝑖 + 2 ∗ 𝑠 − − − (7)

This process continues until solution at desired point is obtained. Here two slopes has been
used that’s why it is known as 2nd order method.
𝒅𝒚
MATLAB program for Euler’s method to solve the ODE + 𝒙𝒚 = 𝟎; 𝒚(𝟎) = 𝟏 from
𝒅𝒙
x=0 to 0.25 :-

clc
clear all
s=input('dy/dx= ','s');
f=eval(['@(x,y)', s]);
x1=input('x1= ');
xf=input('xf= ');
y1=input('y1= ');
h=input('h= ');
n=(xf-x1)/h;
for i=1:n
m1=f(x1,y1);
x2=x1+h;
y2=y1+h*m1;
x1=x2;
y1=y2;
y2
end

Output:-

dy/dx= -x*y

x1= 0

xf= .25

y1= 1

h= .05

y2 = 1

y2 = 0.9975

y2 = 0.9925

y2 = 0.9851

y2 = 0.9752
𝒅𝒚
MATLAB program for R-K 2nd order method to solve the ODE + 𝒙𝒚 = 𝟎; 𝒚(𝟎) = 𝟏
𝒅𝒙
from x=0 to 0.25 :-

clc
clear all
s=input('dy/dx = ','s');
f=eval(['@(x,y)',s]);
x1=input('x1= ');
y1=input('y1= ');
xf=input('xf= ');
h=input('h= ');
n=(xf-x1)/h;
for i=1:n
m1=f(x1,y1);
x2=x1+h;
y2=y1+(h*m1);
m2=f(x2,y2);
m=(m1+m2)/2;
y=y1+(h*m);
x1=x2;
y1=y;
y
end

Output:-

dy/dx = -x*y

x1= 0

y1= 1

xf= .25

h= .05

y = 0.9988

y = 0.9950

y = 0.9888

y = 0.9802

y =0.9692
Conclusion:-

The solution point at y(.25) is slighty different for the two methods. R-K 2nd order
method gives better accuracy than Euler’s method because two slopes are considered here
whereas only one slope is considered in Euler’s method. Here initial value problem had been
solved using both the methods. The term initial value is due to the given initial value with the
differential solution to be solved.

You might also like