0% found this document useful (0 votes)
23 views6 pages

NM Lab 12 Manual - EulerMethod

The document describes implementing Euler's method to solve ordinary differential equations numerically using MATLAB. It provides an introduction to Euler's method, an example problem, and MATLAB code to implement the method. Students are assigned exercises to solve initial value problems analytically and using Euler's method with different step sizes.

Uploaded by

naeemhuzaifah0
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)
23 views6 pages

NM Lab 12 Manual - EulerMethod

The document describes implementing Euler's method to solve ordinary differential equations numerically using MATLAB. It provides an introduction to Euler's method, an example problem, and MATLAB code to implement the method. Students are assigned exercises to solve initial value problems analytically and using Euler's method with different step sizes.

Uploaded by

naeemhuzaifah0
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
You are on page 1/ 6

International Islamic University, Islamabad

Faculty of Engineering and Technology


Department of Electrical and Computer Engineering

Numerical Methods Lab

EXPERIMENT # 12: ODE Solution by Euler’s Method using MATLAB

Name of Student: …………………………………..


Roll No.: ……………………………………………
Date of Experiment: ………………………………..
Lab 12: ODE Solution by Euler’s Method using MATLAB

Lab Experiment 12: ODE Solution by Euler’s Method using MATLAB

Objective:
• To introduce you to solving initial-value problems for ODEs (ordinary differential
equations).
• Knowing how to implement the following Euler’s methods for a single ODE.
• Knowing how to implement the following Euler’s methods for systems of ODEs:

Introduction to Euler’s Method:


To solve ordinary differential equations of the form
𝑑𝑦
= 𝑓 (𝑡 , 𝑦 ) (1)
𝑑𝑡

Recall that the method was of the general form:


New value = old value + slope × step size
or, in mathematical terms:
𝑦𝑖+1 = 𝑦𝑖 + 𝜙ℎ (2)
where the slope 𝜙 is called an increment function. According to this equation, the slope estimate
of 𝜙 is used to extrapolate from an old value 𝑦𝑖 to a new value 𝑦𝑖+1 over a distance h. This formula
can be applied step by step to trace out the trajectory of the solution into the future. Such
approaches are called one-step methods because the value of the increment function is based on
information at a single point 𝑖.
All one-step methods can be expressed in the general form of Eq. (22.4), with the only difference
being the manner in which the slope is estimated. The simplest approach is to use the differential
equation to estimate the slope in the form of the first derivative at ti . In other words, the slope at
the beginning of the interval is taken as an approximation of the average slope over the whole
interval. This approach, called Euler’s method.
The first derivative provides a direct estimate of the slope at 𝑡𝑖 (Fig. 1):
𝜙 = 𝑓 (𝑡𝑖 , 𝑦𝑖 ) (3)
where 𝑓 (𝑡𝑖 , 𝑦𝑖 ) is the differential equation evaluated at 𝑡𝑖 and 𝑦𝑖 . This estimate can be substituted
into Eq. (2):
𝑦𝑖+1 = 𝑦𝑖 + 𝑓 (𝑡𝑖 , 𝑦𝑖 )ℎ (4)
This formula is referred to as Euler.s method (or the Euler-Cauchy or point-slope method). A new
value of y is predicted using the slope (equal to the first derivative at the original value of t) to
extrapolate linearly over the step size h (Fig.1).

Numerical Methods Lab (CS 203-L) Page 2


Lab 12: ODE Solution by Euler’s Method using MATLAB

Figure 1: Euler's Method

Example (Problem Statement)


Use Euler’s method to integrate 𝑦 ′ = 4𝑒 0.8𝑡 − 0.5𝑦 from 𝑡 = 0 to 4 with a step size of 1. The
initial condition at 𝑡 = 0 is 𝑦 = 2. Note that the exact solution can be determined analytically as:
4
𝑦= (𝑒 08𝑡 − 𝑒 −0.5𝑡 ) + 2𝑒 −0.5𝑡
1.3

Solution:
Equation (4) can be used to inplement Euler’s method:
𝑦(1) = 𝑦(0) + 𝑓(0,2)(1)
Where 𝑦(0) = 2 and the slope estimate at 𝑡 = 0 is
𝑓(0,2) = 4𝑒 0 − 0.5(2) = 3
Therefore,
𝑦(1) = 2 + 3(1) = 5
The true solution at 𝑡 = 1 is
4
𝑦= (𝑒 0.8(1) − 𝑒 −0.5(1) ) + 2𝑒 −0.5(1) = 6.19463
1.3
Thus, the percent relative error is
6.19463 − 5
𝜀𝑡 = | | × 100% = 19.28%
6.19463
For the second step:
𝑦(2) = 𝑦(1) + 𝑓(1,5)(1)
= 5 + [4𝑒 0.8(1) − 0.5(5)] = 11.40216

Numerical Methods Lab (CS 203-L) Page 3


Lab 12: ODE Solution by Euler’s Method using MATLAB

Table 1: Comparison of true and numerical values of the integral of 𝑦 ′ = 4𝑒 0.8𝑡 − 0.5𝑦, with the initial
condition that y = 2 at t = 0. The numerical values were computed using Euler’s method with a step size of 1

Figure 2: Comparison of the true solution with numerical solution using Euler’s methodfor the integral of y ′ =
4e0.8t − 0.5y, from 𝑡 = 0 𝑡𝑜 4 with the step size of 1.0. The initial condition that y = 2 at t = 0.

The true solution at 𝑡 = 2.0 is 14.84392 and, therefore, the true percent relative error is 23.19%.
The computation is repeated, and the results compiled in Table I and Fig. 2. Note that although the
computation captures the general trend of the true solution, the error is considerable. As discussed
in the next section, this error can be reduced by using a smaller step size.
The error associated with Euler’s method is given below:
𝑓 ′ (𝑡𝑖 , 𝑦𝑖 ) 2
𝐸𝑎 = ℎ (5)
2!
MATLAB Implementation M-file: eulode
M-file that uses Euler’s method to compute values of the dependent variable 𝑦 over a range of
values of the independent variable 𝑡. The name of the function holding the right-hand side of the
differential equation is passed into the function as the variable dydt. The initial and final values
of the desired range of the independent variable is passed as a vector tspan. The initial value and
the desired step size are passed as y0 and h, respectively.
The function first generates a vector t over the desired range of the dependent variable using an
increment of h. In the event that the step size is not evenly divisible into the range, the last value
will fall short of the final value of the range. If this occurs, the final value is added to t so that
the series spans the complete range. The length of the t vector is determined as n. In addition, a
vector of the dependent variable y is pre-allocated with n values of the initial condition to
improve efficiency. At this point, Euler’s method (Eq. 3) is implemented by a simple loop:

Numerical Methods Lab (CS 203-L) Page 4


Lab 12: ODE Solution by Euler’s Method using MATLAB

for i = 1:n-1
y(i+1) = y(i) + dydt(t(i),y(i),varargin{:})*(t(i+1)- t(i));
end
Notice how a function is used to generate a value for the derivative at the appropriate values of the
independent and dependent variables. Also notice how the time step is automatically calculated
based on the difference between adjacent values in the vector t.
The ODE being solved can be set up in several ways. First, the differential equation can be defined
as an anonymous function object. For example, for the ODE from Example 1:
>> dydt=@(t,y) 4*exp(0.8*t) - 0.5*y;
The solution can then be generated as
>> [t,y] = eulode(dydt,[0 4],2,1);
>> disp([t,y])
with the result (compare with Table 22.1):
0 2.0000
1.0000 5.0000
2.0000 11.4022
3.0000 25.5132
4.0000 56.8493
Although using an anonymous function is feasible for the present case, there will be more complex
problems where the definition of the ODE requires several lines of code. In such instances, creating
a separate M-file is the only option.
function [t,y] = eulode(dydt,tspan,y0,h,varargin)
% eulode: Euler ODE solver
% [t,y] = eulode(dydt,tspan,y0,h,p1,p2,...):
% uses Euler's method to integrate an ODE
% input:
% dydt = name of the M-file that evaluates the ODE
% tspan = [ti, tf] where ti and tf = initial and
% final values of independent variable
% y0 = initial value of dependent variable
% h = step size
% p1,p2,... = additional parameters used by dydt
% output:
% t = vector of independent variable
% y = vector of solution for dependent variable
if nargin<4,error('at least 4 input arguments required'),end
ti = tspan(1);tf = tspan(2);
if ~(tf>ti),error('upper limit must be greater than lower'),end
t = (ti:h:tf)'; n = length(t);
% if necessary, add an additional value of t
% so that range goes from t = ti to tf
if t(n)<tf
t(n+1) = tf;
n = n+1;
end
y = y0*ones(n,1); %preallocate y to improve efficiency
for i = 1:n-1 %implement Euler's method
y(i+1) = y(i) + dydt(t(i),y(i),varargin{:})*(t(i+1)-t(i));
end

Numerical Methods Lab (CS 203-L) Page 5


Lab 12: ODE Solution by Euler’s Method using MATLAB

Lab Tasks:

Exercise 1:
Solve the following initial value problem over the interval from t = 0 to 2 where y(0) = 1. Display
all your results on the same graph.
𝑑𝑦
= 𝑦𝑡 3 − 1.5𝑦
𝑑𝑡

(a) Analytically.
(b) Using Euler’s method with ℎ = 0.5 and 0.25.

Exercise 2:
Solve the following problem over the interval from x = 0 to 1 using a step size of 0.25 where
y(0) = 1. Display all your results on the same graph.
𝑑𝑦
= (1 + 4𝑥)√𝑦
𝑑𝑥

(a) Analytically.
(b) Using Euler’s method.

Numerical Methods Lab (CS 203-L) Page 6

You might also like