0% found this document useful (0 votes)
12 views4 pages

Euler & RK

The document outlines a lab experiment for the Numerical Methods course at the Islamic University of Technology, focusing on Euler's method and Runge-Kutta methods for solving differential equations. It explains the theory behind these numerical techniques, provides a demonstration problem, and includes MATLAB code for implementation. Additionally, it discusses the limitations of Euler's method and the advantages of higher-order Runge-Kutta methods, along with tasks for the lab report.

Uploaded by

jubayerrocks462
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Euler & RK

The document outlines a lab experiment for the Numerical Methods course at the Islamic University of Technology, focusing on Euler's method and Runge-Kutta methods for solving differential equations. It explains the theory behind these numerical techniques, provides a demonstration problem, and includes MATLAB code for implementation. Additionally, it discusses the limitations of Euler's method and the advantages of higher-order Runge-Kutta methods, along with tasks for the lab report.

Uploaded by

jubayerrocks462
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Islamic University of Technology (IUT)

Organization of Islamic Cooperation (OIC)


Department of Electrical and Electronic Engineering (EEE)

Course No. : Math 4522


Course Name : Numerical Methods Lab.
Experiment No. : 12
Experiment Name : Euler's method, Runge-Kutta 2nd order method and Runge-Kutta
4th order method.

Objective
To get familiarized with the different types of numerical integration technique.

Theory:
For many of the differential equations we need to solve in the real world, there is no "nice" algebraic
solution. That is, we can't solve it using the techniques using separation of variables, integrable
combinations, or using an integrating factor, or other similar means.

As a result, we need to resort to using numerical methods for solving such DEs. The concept is
similar to the numerical approaches we saw in an earlier lab Trapezoidal Rule and Simpson's Rule.

Even if we can solve some differential equations algebraically, the solutions may be quite
complicated and so are not very useful. In such cases, a numerical approach gives us a good
approximate solution.

We are trying to solve problems that are presented in the following way:
dy
= f (x, y) ; and y(a) [which is the initial value] is known.
dx
Where f(x, y) is some function of the variables x, and y that are involved in the problem.
Euler's Method assumes our solution is written in the form of a Taylor's [Link] is, we'll have
a function of the form:
h 2 y '' ( x) h3 y ''' ( x) h3 y '''' ( x)
y ( x + h) ≈ y ( x) + hy ' ( x) + + + + ......
2! 3! 4!
This gives us a reasonably good approximation if we take plenty of terms, and if the value of h is
reasonably small.

For Euler's Method, we just take the first 2 terms only.


y ( x + h) ≈ y ( x) + hy ' ( x)
dy
The last term is just h times the expression, so we can write Euler's Method as follows:
dx
y ( x + h) ≈ y ( x) + hf (x, y)

Demonstration
Example 25.1 from the book.

Problem statement
Use Euler’s method to numerically integrate
dy
= −2 x 3 + 12 x 2 − 20 x + 8.5 from x = 0 to x=4 with a step size of 0.5. The initial condition
dx
at x = 0 is y = 1.
Here y=
−0.5 x 4 + 4 x3 − 10 x 2 + 8.5 x + 1

function E = Euler (f,a,b,y0,N)

h = (b-a)/N;
y = zeros (1, N+1);
t = a: h: b;
y(1) = y0;
for n = 1:N
y(n+1) = y(n) + h * f(t(n));
end

E = [t' y'];
end

% f = @(x) ((-2) * x^3 + 12*x^2-20*x+8.5)


% a = 0;
% b = 4;
% y0 = 1;
% N = 8;
% Y = Euler(f,a,b,y0,N)

The problem with Euler's Method is that you have to use a small interval size to get a reasonably
accurate result. That is, it's not very efficient.

The Runge-Kutta Method produces a better result in fewer steps.

Verification
n h I E t (%)

2 0.4 1.0688 34.85


3 0.267 1.3696 16.52
4 0.200 1.4848 9.49
5 0.16 1.5399 6.14
10 0.08 1.615 1.55

Runge-Kutta 4th order method


Runge-Kutta 4th order method is a numerical technique used to solve ordinary differential equation
of the form
dy
= f (x, y), y(0)=y 0
dx

In summary, we can describe the formula in the following way:


xn +=
1 xn + h
1
yn +1 =yn + (k1 + 2 k 2 + 2 k 3 + k 4 )
6
where,
k1 = h f ( xn , yn )
k1
k 2 = h f ( xn + h , yn + )
2 2
k
k 3 = h f ( xn + h , yn + 2 )
2 2
k 4 = h f ( xn + h, yn + k3 )

Demonstration
Example 25.7 from the book.

Problem statement
Use the classical 4th order RK method to integrate f (x, y) = −2 x + 12 x − 20 x + 8.5
3 2

using a step size of h = 0.5 and an initial condition of y = 1 at x = 0


clc
format long g
syms x y

n = input ('Please enter the iteration number = ');


ab = input ('Please input interval [a, b] = ');
y0 = input ('Please input initial values y(x0) = ');
fxy = input ('Please input the function f(x,y) = ');

h = (ab(2) - ab(1))/n; % step size;


Y = zeros (n, 1);
ff = char (fxy);
f = inline (ff, 'x', 'y');

% Assigning the values of x


X = [ab(1): h : ab(2)]';
Y(1,1) = y0;

for i=2:n+1
k1=h*f(X(i-1,1),Y(i-1,1));
k2=h*f(X(i-1,1)+h/2,Y(i-1,1)+k1/2);
k3=h*f(X(i-1,1)+h/2,Y(i-1,1)+k2/2);
k4=h*f(X(i-1,1)+h,Y(i-1,1)+k3);
Y(i,1)=Y(i-1,1)+ (1/6)*(k1+2*k2+2*k3+k4);
end

disp(' Xn Yn')
disp('---------------------------------------------------------------')
disp([X Y])

Task for the lab report


 Why higher order Runge-Kutta methods are used?
 Write a Matlab program to implement 5th order RK method. Then use it to solve
=
f (x, y) 4e0.8 x − 0.5 y
with y(0) =2 from x = 0 to x = 4, with various step sizes.

You might also like