MATLAB Simulink Applications in Solving
MATLAB Simulink Applications in Solving
net/publication/320531603
CITATIONS READS
10 6,841
1 author:
Sulaymon Eshkabilov
North Dakota State University
52 PUBLICATIONS 183 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
Comfort ride studies; road roughness studies; modeling and system identification of heat flow View project
All content following this page was uploaded by Sulaymon Eshkabilov on 20 October 2017.
Abstract. This paper presents essential points with applications of MATLAB/Simulink tools in solving initial value problems
(IVP) of ordinary differential equations (ODEs) analytically and numerically. There are seven approaches of solving IVP of
ODEs in MATLAB/Simulink are discussed via two different examples one which is simulation of thrown ball trajectories and the
other is a non-homogeneous second order ODE. Comparative analyses of seven approaches are discussed by plotting their
numerical results.
1. Introduction
Differential equations (DEs) are very important not only for mathematics or physics but also for broad areas of engineering and
many other scientific fields and there are myriad of engineering and scientific problems formulated and simulated using
differential equations. For instance, falling or flying object trajectories and equations of motion, radioactive decay, dynamics of
processes, logistics problems, wave propagation equations, control problems, just to name a few, are formulated with differential
equations. There are many definitions of differential equations and one of the simplest one is “A differential equation is any
equation which contains derivatives, either ordinary derivatives or partial derivatives” by (Internet Source: Paul’s Online Math
Notes).
Despite the fact that modeling the behavior of a system or process or phenomenon with DEs can be relatively easy, but solving
many DEs analytically are not plausible and only possibility is then to seek their numerical solutions. There are a number of
methods to solve DEs analytically and numerically. For example, to define analytical solution(s) of the DEs separation of
variables or introduction of new variables or by expressing them as linear equations or multiplying by some scalars type of
approaches (Boyce and DiPrima, 2003), (Internet sources: SOS MATH and wikiHOW) are used depending on the type of the
DEs, e.g. homogeneous or non-homogeneous, or linear or non-linear. To find numerical solutions of the DEs, there a number of
methods viz. Euler, Runge-Kutta, Runge-Kutta-Gill, Adams, Adams-Bashforth method, Adams-Moulton method, Heun, Ralton,
Milne, mid-point rule, Taylor series, and trapezoidal rule methods, developed. Nowadays, to find numerical solutions of
differential equations computers and computer programs are mostly employed, and there are many commercial and open ware
software packages to find numerical solutions of differential equations. One of such powerful software packages is
MATLAB/Simulink that contains many easy to use tools and built-in functions to solve or simulate differential equations. We
have studied a few tools and functions of the package in order to show how to employ it in solving initial value problems (IVP)
of ordinary differential equations (ODEs). There are two relatively simple examples are taken to demonstrate our developed
methodologies and approaches in solving IVP. One of our examples is to simulate thrown ball trajectories and the other is to find
analytical and numerical solutions of a second order non-homogeneous ODE.
2. Method
2.1. Example 1. Modeling Thrown Ball Trajectories
There are several factors influencing on flight trajectory of a hit or thrown ball. This can be taken as one of the good examples for
applications of differential equations for educational purposes.
All good tennis players know how to hit the ball to make it fly according to a certain angle to get over the net and at just the right
spot of the court on the opponent’s side. Professional hits are the result of a few years of hard practice. If a new player hits the
ball 1% faster than a professional tennis player does, it will land considerably far away (maybe about 0.5 m) beyond the baseline.
The ball trajectory is a well formulated and explained with the theory of the ball trajectory that is all depicted hereafter.
z
From the figure, we can see that there are three forces acting upon the ball flying in air. They are:
1. Gravitational force
(1)
Where is vector of the gravitational acceleration.
3. Magnus force
(3)
The magnitudes of the drag force and the Magnus force are usually supposed to have a form given by the theory of
ideal fluids (Richardson, 1961):
(4)
(5)
Where is air density. The coefficients and are dependent in real fluids, for instance, air on the velocity , the ball
revolution and the ball surface material. These coefficients are found experimentally and taken from the experimental results of
the source (Stepanek, 1988), the tennis ball moving with velocity of and with a ball revolution (spin) of
, the coefficients of and depend on the ratio of only, where in some cases the
projection of the equatorial velocity of the spinning ball onto the velocity vector . The following expressions for the
coefficients were obtained:
(6)
(7)
For a tennis ball we can neglect the deceleration of the ball revolution, so is constant. The trajectory vector of the ball can be
defined via Newton’s Second Law equations for the position vector
(8)
and
The equation is a nonlinear system of three differential equations and no analytical solution exists for it, so only way is to solve it
numerically. Let us select the x and z axes in x-z plane and then derive the final formulation of the equation (8) by plugging in it
and .
(9a)
(9b)
Where and The parameter describes direction of rotation, for instance, for topspin As
initial conditions for , the following initial values are taken
(10)
Where, is the magnitude of the initial velocity vector and is an angle between and x - axis.
For our simulations we take two case-conditions which are: 1) ball flying in vacuum and 2) ball flying in air.
1) For the case of ball flying in vacuum, there is one force acting upon the ball is gravitational force and thus, the
equations (9a, 9b) will gain a very simple form (11) :
(11)
2) For the case of ball flying in air, in order to find numerical solutions of the problem we rewrite the equations (9a, 9b)
of the second order ODEs via a system of the first order ODEs shown in (12) by introducing new variable names.
(12)
diameter of the tennis all d m its mass and air density Also, for initial
conditions we choose
and for the spin of the ball we take for top spin.
1) Case # 1. Ball is hit in the vacuum. This case is very simple and its analytical solution can be easily found with
MATLAB Sym olic MATH tool ox’s function dsolve(). Here is a script – MATLAB simulation model.
By executing the above script, in the MATLAB command window, the next outputs – analytical solutions (13) of the ball
trajectory in vacuum are found.
, (13)
Here is a complete simulation model implemented via MATLAB (with Sym olic MATH tool ox’s function dsolve( )) script for
the case when a tennis ball is hit in vacuum.
2) Case #2. The numerical simulation models of the tennis ball trajectory are developed in MATLAB and Simulink.
Simulations of the ball trajectory in the air under two sub-cases are considered when a ball is hit with topspin
and without spin, and also, the numerical simulation model of the case # 1, when a ball is hit in the vacuum, is
developed for comparison purposes. The first way, the MATLAB simulation model is created as an M-file with
ODE solvers ode23, ode45 and ode113, that is given below.
% Ball Trajectory simulation model with ODE23, ODE45 and ODE113 solvers of MATLAB.
h =1; % [m];
v0=25 ; % [m*sec^-1];
theta=15; % [deg];
ro=1.29; % [kg*m^-3];
g=9.81; % [kg*m]
d=.063; % [m];
m=.05; % [kg];
w=20; % [m*sec^-1]; angular velocity of a spinning ball
eta=1; % describes direction(+-) and presence of rotation; eta=1 is for topspin.
alfa = ro*pi*d^2/(8*m);
u = @(t,x)sqrt(x(3).^2+x(4).^2);
CD = @(t,x)(0.508+(1./(22.053+4.196*(u(t,x)./w).^(5/2))).^(2/5));
CM = @(t,x)(1/(2.022+.981*(u(t,x)./w)));
vacuum = @(t,x)([x(3); x(4); 0; -g]);
nospin = @(t,x)([x(3); x(4); (-1)*CD(t,x)*alfa*u(t,x)*x(3); (-1)*g-CD(t,x)*alfa*u(t,x)*x(4)]);
topspin = @(t,x)([x(3); x(4);
(-1)*CD(t,x)*alfa*u(t,x)*x(3)+eta*CM(t,x)*alfa*u(t,x)*x(4);
(-1)*g-CD(t,x)*alfa*u(t,x)*x(4)-eta*CM(t,x)*alfa*u(t,x)*x(3)]);
time=0:.01:1.5;
ICs=[0, 1, v0*cos(theta*pi/180), v0*sin(theta*pi/180)];
[tvac, XZvac] = ode23(vacuum, time, ICs, []);
[tns, XZns] = ode45(nospin, time, ICs, []);
[tts, XZts] = ode113(topspin, time, ICs, []);
% It is also important to find when the ball hits the ground (along x-axis) and after how many seconds in: the vacuum & in the
% air with no-spin and top-spin.
% Case #1. Vacuum
z_i=find(abs(XZvac(:,2))<=min(abs(XZvac(:,2)))); t_gr=XZvac(z_i,1); t_t = time(z_i);
% Case #2.a. In the air: No-spin
z1_i =find(abs(XZns(:,2))<=min(abs(XZns(:,2)))); t_gr1=XZns(z1_i,1); t_t1 = time(z1_i);
% Case #2.b. In the air: Top-spin
z2_i =find(abs(XZts(:,2))<=min(abs(XZts(:,2)))); t_gr2=XZts(z2_i,1); t_t2 = time(z2_i);
Another approach in modeling of the ball trajectory is the Simulink model shown in Figure 4 that consists of three parts:
In Vacuum
Top Spin in Air (Subsystem: With TopSpin (eta=1))
Without Spin in Air (Subsystem: Without Spin in Air)
Note in building a Simulink model (Ball.mdl), we have employed a function block for algebraic expressions to compute
. Besides, all initial conditions: are specified via
Integrator blocks as internal initial condition sources. The vacuum part of the main Simulink model (Figure 2) of the problem is
composed of constant input, integrator and scope blocks, and the air part with top-spin (Figure 3) and no-spin (Figure 4) sub-
cases are modeled under two sub-systems, viz. With TopSpin (eta=1) and Without Spin in Air.
Fig. 2. The main Simulink model (Ball.mdl) of the tennis ball trajectory.
Fig. 3. A Top-spin model is a subsystem (with Top-Spin (eta=1)) of the model (Ball.mdl) of the ball trajectory.
To find analytical and numerical solutions of this problem seven different MATLAB/Simulink models are built using seven
different approaches, viz.
1) Sym olic MATH tool ox’s function dsolve
2) Sym olic MATH tool ox’s Laplace Transform functions laplace, ilaplace
3) MuPAD tool ox’s ode::solve tools
4) MATLAB’s numerical ODE solvers: ode2 , ode45, ode113
5) Simulink model
6) Scripts using Euler’s Method
7) Scripts using 5-step Adams-Moulton method.
1) A first way is to find an analytical solution of the problem using Symbolic MATH toolbox (dsolve) of MATLAB .
%% Solving IVP ODEs using Symbolic MATH "dsolve", Laplace Transforms and
close all
clear
%% 1st Solution Approach: DSOLVE
Solution_dsolve=dsolve('D2y+Dy+3*y-sin(5*t)=0', 'y(0)=3', 'Dy(0)+2=0');
Solution_dsolve=simplify(Solution_dsolve);
disp('Symbolic MATH dsolve function output is: ')
disp(Solution_dsolve)
An analytical (particular) solution of the problem is rather complicated equation expressed with exponential and trigonometric
functions, but with some simplifications the next formulation is obtained.
2) A second way is also to evaluate an analytical solution of the problem using Symbolic MATH toolbox (laplace,
ilaplace) of MATLAB.
Solution_Laplace=ilaplace(Y);
Solution_Laplace=simplify(Solution_Laplace);
display('Solution found using Laplace Transforms ')
disp(Solution_Laplace)
The Laplace transformation approach without any simplifications produces the same formulation of an analytical solution of the
problem as obtained with dsolve().
3) A third way is also to find an analytical solution of the problem with MuPAD toolbox of MATLAB.
MuPAD has also produced exactly the same analytical solution equation alike the previous two ways.
To find numerical solutions we need to re-write the given second order ODE as two first order ODEs as we did in equation (12)
of the example #1 by introducing new variables.
4) A fourth way is to find numerical solutions of the problem using ode45 and ode113 built-in solvers of MATLAB. Note
in this simulation model anonymous function is used to re-write the given second order ODE as two first order ODEs.
5) A fifth way is the Simulink model (shown in Figure below) of the problem and obtaining numerical solutions. Note that
two initial condition values are inserted via two integrator blocks (Integrator1 and Integrator) internally.
The Simulink model, called ODEsols_COMPARE.mdl that is given in Figure 5, is simulated from an M-file with the following
script.
%% 5th Solution Approach: Simulink model simulation
open('ODEsols_COMPARE.mdl')
set_param('ODEsols_COMPARE','Solver','ode8','StopTime','2*pi');
[tout, SIMout]=sim('ODEsols_COMPARE.mdl');
6) A sixth way is another numerical solution search using Euler’s method. Note in this case, the given problem equation is
written via two first order ODEs.
%% 6th Solution Approach: via scripts using Euler's Method
t0=0; % Start time
tend=2*pi; % End time
h=pi/100; % Time step
t=t0:h:tend; % Time space
steps=length(t); % # of steps
y(1,:)=[3, -2]; % ICs: u0 at t0
for k=2:steps
f=[y(k-1,2), sin(5*t(k-1))-3*y(k-1,1)-y(k-1,2)];
y(k,:)=y(k-1,:)+h*f;
end
7) A seventh way is also a numerical solution search with 5-step Adams-Moulton method that is a predictor-corrector
method to solve ODEs numerically. In this method, an anonymous function is used to express the given second order
ODE as two first order ODEs.
%% 7th- Solution Approach: via scripts using 5-step Adams-Moulton Method
To display numerical values of the thrown ball trajectories in vacuum and air obtained from the simulation models developed in
MATLAB – m-files and Simulink model (Ball.mdl) the following plot and data display tools of MATLAB are employed.
figure(1)
plot(XZvac(:,1), XZvac(:,2), 'bo', 'linewidth', 1), grid on; hold on
plot(XZns(:,1), XZns(:,2), 'm', 'linewidth', 2)
plot(XZts(:,1), XZts(:,2), 'ko-', 'linewidth', 1.0)
legend('In Vacuum','No-Spin in Air','Top-Spin in Air',0), ylim([0, 3.35])
title 'Trajectory of a Tennis Ball hit under \theta=15^0, h=1 m'
xlabel 'Length, [m]', ylabel 'Height, [m]'
tt1=['VACUUM: Ball hits the ground (x-axis):' num2str(t_gr) '[m]']; gtext(tt1);
tt1a=['VACUUM: Ball hits the ground after: ' num2str(t_t) ' [sec]']; gtext(tt1a);
tt2=['Nospin: Ball hits the ground: ' num2str(t_gr1) '[m]']; gtext(tt2);
tt2a=['Nospin: Ball hits the ground: ' num2str(t_t1) '[sec]']; gtext(tt2a);
tt3=['Topspin: Ball hits the ground: ' num2str(t_gr2) '[m]']; gtext(tt3);
tt3a=['Topspin: Ball hits the ground: ' num2str(t_t2) 'sec]']; gtext(tt3a); hold off
After executing the above shown simulation models and plot scripts, trajectories of the ball in vacuum and air with top-spin and
different spin values, and without any spin, ball covered distances and flying time results, displayed in Figure 6 and 7, are
obtained.
Fig. 6. Tennis ball trajectories in the vacuum and air with top-spin and no-spin.
Fig.7. Trajectory of a Ball with different spin values.
From the results of flying ball trajectory simulations shown in Figure 6 and 7, it is clear if ball is hit in such a way by providing
no spin to a ball, then it flies much longer distance than a ball is hit with top-spin.
To display the results from seven solution methods of the given second order ODE, the following script of plot tools and
commands is executed.
t=0:pi/100:2*pi;
SDsol=eval(vectorize(Solution_dsolve)); LTsol=eval(vectorize(Solution_Laplace));
figure(1);
plot(t, SDsol, 'bd-', 'markerfacecolor', 'c'), hold on
plot(t, LTsol, 'ro-')
plot(t, y_ODE45(:,1), 'm+-')
plot(t, y_ODE113(:,1), 'k<-')
plot(tout, SIMout(:,1), 'gd-')
plot(t, y(:,1), 'linewidth', 2), grid minor
title('Numerical solutions of the problem: ddy+dy+y=sin(5*t); y(0)=3, dy(0)=-2')
xlabel('t'), ylabel('Solution, y(t)')
legend('dsolve', 'Laplace Transforms','ODE45','ODE113','Simulink', 'Euler', 'Adams-Moulton', 0)
xlim([0, 2*pi]), hold off
Fig. 8. Numerical solutions of the second order ODE: with dsolve, Laplace Transforms, ode45, ode113, Simulink, Euler and
Adams-Moulton methods.
In overall views, all the results of seven approaches converge very closely but with different error margins that can be seen in the
above plot figure of residuals. From the residuals, it is clear that Euler and Adams-Moulton produce largest errors that tend to
decrease over simulation time. Results of other numerical methods are so well converged with the analytical solution that error
margins smaller than that can be reduced significantly by taking smaller time steps. On the other hand, when we need to
solve larger problems with crude error tolerances and with least computation time, then it is more reasonable to develop a model
with Euler or 1/2nd order Runge-Kutta method.
4. Summary
With two relatively simple examples of explicitly defined ODEs, we have demonstrated a few essential tools of
MATLAB/Simulink package how to apply the package’s capacity in modeling and simulating IVP of ODEs. The demonstrated
M-files and Simulink models are generic to solve a similar class of problems demonstrated in our two problems. Besides,
MATLAB/Simulink tools and built-in functions for building simulation models and evaluating analytical and numerical solutions
are easy to apply. For instance, dsolve and laplace/ilaplace of Symbolic MATH toolbox and ode::solve of MuPAD toolbox are
very handy to find analytical solutions of IVP of ODEs only if analytical solution(s) of a given problem exists and not very
complex. ODE solvers, ode23, ode45, ode113 and others of MATLAB and Simulink tools are relatively simple to build
simulation models of systems with ODEs.
5. References
Boyce W.E., and DiPrima R.C., (2003). Elementary Differential Equations and Boundary Value Problems. John Wiley & Sons,
Inc. 745 p.
Stepanek, A., (1988). The Aerodynamics of tennis Balls – the Topspin Lob. American Journal of Physics, 56, pp. 138-142.