You are on page 1of 5

Reza Katebi, EE908 53

10. Integration Methods: Euler Method


The purpose of this tutorial is to describe how an initial value problem can be solved using Euler's
method, with implementation in Matlab. Many students find it easiest to learn to use Matlab through
examples rather than flipping through a manual to learn the syntax, command names, etc., so the
Matlab code for simple examples will be given.
10.1 Euler's Method
Consider the initial value problem

(1)

Suppose we write the Taylor exansion of the solution:

(2)

Truncating and using (1), we obtain the formula for Euler's method for the numerical solution of
differential equations:

(3)
Of course, there is nothing special about , so, letting , , we obtain

(4)


By iterating, we find an approximation to the solution ( ) y t of (1). Here is known as the
stepsize.

10.2 An Example
As an example, suppose we want to solve the one-dimensional ordinary differential equation

(5)

where and are constants. This can be in fact be solved exactly because it is a separable equation,
and we find that

Reza Katebi, EE908 54

(6)
where . (This can be checked by plugging (6) into (5)).
To view the exact solution in Matlab, we create a file called ``yexact.m'' with the following lines of
text:
function r = yexact(t,y0,K,s)
r = y0*exp(K*t) + s*(1 - exp(K*t));
Note that this function takes four arguments, the time t, the initial condition y0, and the constants K
and s from (5). Suppose that we want the solution for y0=100, K=1, and s=20. First, in Matlab we
type:
t = 0:0.01:5;
which creates a vector t = (0,0.01,0.02,...,4.98,4.99,5), then
plot(t,yexact(t,100,1,20))
which plots yexact vs. t at the times given in the vector t.
10.3 Numerically Solving the Example with Euler's Method
Although we know the exact solution for equation (5), it is instructive to consider its numerical
solution using Euler's method. This is implemented in Matlab with the following series of statements
(note that we compare to the exact solution, so to run this program you must have the file ``yexact.m''
as described on the last page):

% Example: Euler's method for dy/dt = K*(y-s)
K = 1;
s = 20;
y0 = 100;
npoints = 50;
dt = 0.1;

Reza Katebi, EE908 55
y = zeros(npoints,1); % this initializes the vector y to being all zeros
t = zeros(npoints,1);
y(1) = y0; % the initial condition
t(1) = 0.0;
for step=1:npoints-1 % loop over the timesteps
y(step+1) = y(step) + dt*K*(y(step)-s);
t(step+1) = t(step) + dt;
end
plot(t,y,'r'); %plots the numerical solution in red
hold on; %keep the previously plotted lines
plot(t,yexact(t,y0,K,s)); %plots the exact solution (default plot is in blue, solid line)
10.4 Conditional Statements
It is often of interest to determine when the solution satisfies a certain property. For example, suppose
that we want to know when the solution obtained with Euler's method to equation (5) with crosses.
This can be accomplished with the following Matlab program:
K = 1;
s = 20;
y0 = 100;
y=1000;
npoints = 50;
dt = 0.1;
y = zeros(npoints,1);
t = zeros(npoints,1);
y(1) = y0;
t(1) = 0.0;
for step=1:npoints-1
y(step+1) = y(step) + dt*K*(y(step)-s);
t(step+1) = t(step) + dt;

Reza Katebi, EE908 56
if ((y(step) < 1000) & (y(step+1) > 1000)) % conditional statement
fprintf('y crosses 1000 at t= %14.7f',t(step+1)); % output result to the screen
end
end






Exercise 10.5
The van der Pol equation is an ordinary differential equation that models self-
sustaining oscillations in which energy is fed into small oscillations and removed
from large oscillations. This equation arises in the study of circuits containing vacuum
tubes and is given by

y + (1! y
2
) y + y = u
assume

x
1
= y
x
2
= y

(1) Write the state space model for the system.
(2) Use Euler method to simulate the system
(3) Plot

x
1
against x
2
for u=1, = 0.2 and u=1, = 1

You might also like