You are on page 1of 5

Experiment No:- 09

Name of the Experiment:MATLAB program for Eulers method and Runge-Kutta 2 nd order method to solve
ordinary differential equation.
Objectives:Objectives of this experiment are1. To solve ordinary differential equation using Eulers method and R-K 2 nd order
method.
2. To compare the accuracy of the above mentioned methods.
Theory:We can solve ordinary differential equation using various methods among which
Eulers & R-K 2nd order method will be applied here. Brief description of these two methods
are given belowEulers method:Among all the methods of solving differential equations Eulers method is the
simplest one which uses extrapolation technique to find the solution. Let us consider the
following differential equation with a given initial valuedy
=f ( x , y )(1)
dx
y ( x1 ) = y 1( 2)
At (x 1 , y 1) slope iss 1=

dy
( x , y ) =f ( x 1 , y 1 ) (3)
dx 1 1

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 isy ( x1 +h )= y 1 +hf ( x 1 , y 1 ) ( 4)
In the same way we will get the next solution points. Generallyy ( xi +h )= y i +hf ( x i , yi ) (5)

In this way we can find out the solution of a given differential equation at any desired point.
Eulers 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 2 nd order method. Here R-K 2nd order will be
applied. Heuns 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 valueth

dy
=f ( x , y )(1)
dx
y ( x1 ) = y 1( 2)
At (x 1 , y 1) slope iss 1=

At

( x1 , y1 )

dy
( x , y ) =f ( x 1 , y 1 ) (3)
dx 1 1

a straight line is drawn with slope

s1

which cuts the vertical line at

( x 1+ h ) at point ( x 1+ h y 2 ' ) . Now at ( x 1+ h y 2 ' ) slope is


s 2=

Again from

dy
( x , y ' ) =f ( x 2 , y 2 ' ) (4)
dx 2 2

( x 1 , y 1 ) a straight line with slope s is drawn which cuts the vertical line at

( x 1+ h ) at point y 2 .
s=

s 1 +s 2
(5)
2

Now the point

y2

will be equal to the followingh


y 2= y 1 + s(6)
2

Next solution points will be obtained in the same way. So generally we can writeh
y ( xi +h )= y i + s(7)
2
This process continues until solution at desired point is obtained. Here two slopes has been
used thats why it is known as 2nd order method.

MATLAB program for Eulers method to solve the ODE


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

dy
+ xy=0 ; y ( 0 )=1
dx

from

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
+ xy=0 ; y ( 0 )=1
dx

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 2 nd order
method gives better accuracy than Eulers method because two slopes are considered here
whereas only one slope is considered in Eulers 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