NM Lab 12 Manual - EulerMethod
NM Lab 12 Manual - EulerMethod
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:
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
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:
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
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.