# Euler’s Method Or ode45 for solving ODE for control systems

The dominant approach for solving ODE in control systems books is ode45 since the majority of these books use Matlab. I'm not acquainted with how the ode45 works but lately I started reading about Euler's method in this book Numerical Methods for Engineers. If the step size is very small, then the results are satisfactory. For simulation, one can actually set the step size to be very small value. I've used ode45 in here for regulation and tracking problems. I faced some difficulties for using ode45 for tracking problem since the step size is not fixed. Now for the same experiment, I've used the Euler's method with step size 0.001 sec. The results are amazing and so friendly in comparison with ode45. This is a snapshot from the result And this is the code

clear all;
clc;

dt = 0.001;
t = 0;

% initial values of the system
a = 0; % angular displacement
da = 0; % angular velocity

% PID tuning
Kp = 50;
Kd = 18.0;
Ki = 0.08;

error  = 0;

%System Parameters:
m = 0.5;       % mass (Kg)
d = 0.0023e-6; % viscous friction coefficient
L = 1;         % arm length (m)
I = 1/3*m*L^2; % inertia seen at the rotation axis. (Kg.m^2)
g = 9.81;      % acceleration due to gravity m/s^2

% Generate Desired Trajectory
y = 0:dt:(3*pi)/2;
AngDes = y; % Ang: angle , Des: desired
AngDesPrev = 0;

for i = 1:numel(y)

% get the first derviative of the desired angle using Euler method.
dAngDes  =  ( AngDes(i) - AngDesPrev )/ dt;
AngDesPrev = AngDes(i);

% torque input
u = Kp*( AngDes(i) - a ) + Kd*( dAngDes - da ) + Ki*error;
% accumulated error
error = error + ( AngDes(i) - a );

%store the erro
E(i) = ( a - AngDes(i) );
T(i) = t;

dda = 1/I*(u - d*da - m*g*L*sin(a));

% get the function and its first dervative
da = da + dda*dt;
a = a  +  da*dt;

%store data for furhter investigation
A(i) = a;
dA(i) = da;

t = t + dt;

end

plot(T, AngDes, 'b', T, A, 'g', 'LineWidth', 1.0)
h = legend('$\theta_{d}(t)$', '$\theta(t)$');
set(h, 'Interpreter','LaTex')


My question is why ode45 is preferred in many control books assuming the step size is very small.

• I suspect mainly because Euler is only second order method. This means it requires significant more simulation steps in order to get the same accuracy as ode45. Also ode45 has a variable stepsize, such that the local error is also roughly constant which also requires less simulation steps. You can test this yourself by looking at the computation time of both. Apr 12 '15 at 1:13
• @fibonatic: Euler method is second order accurate locally. Globally (accumulating errors over multiple time steps), it is much worse: only first order accuracy.
– Paul
Apr 12 '15 at 16:08
• This question is more appropriate for the SciComp SE site, since it has more to do with numerical methods than robotics. I recommend posting future questions of this type there.
– Paul
Apr 12 '15 at 20:49
• In a real control setting, that is with a digital controller and therefore not dealing with any simulation, you must work with constant sample time. In this regard, Tustin integration (bilinear transformation) has a wider application than forward or backward Euler formula. Apr 13 '15 at 18:20
• I think there's more than that behind. For a complete overview refer to wikipedia. For a comparison between Euler's and Tustin's formulas, refer to this page. Apr 14 '15 at 9:03