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.