Advantages and Disadvantages of Using MATLAB/ode45 For Solving Differential Equations in Engineering Applications
Advantages and Disadvantages of Using MATLAB/ode45 For Solving Differential Equations in Engineering Applications
Ahmed
Abstract
The present paper demonstrates the route used for solving differential equations for the
engineering applications at UAEU. Usually students at the Engineering Requirements Unit (ERU)
stage of the Faculty of Engineering at the UAEU must enroll in a course of Differential Equations
and Engineering Applications (MATH 2210) as a prerequisite for the subsequent stages of their
study. Mainly, one of the objectives of this course is that the students practice MATLAB software
package during the course. The aim of using this software is to solve differential equations
individually and as a system of equation in parallel with analytical mathematics trends. In general,
mathematical models of the engineering systems like mechanical, thermal, electrical, chemical
and civil are modeled and solve to predict the behavior of the system under different conditions.
The paper presents the technique which is used to solve DE using MATLAB. The main code that
utilized and presented is MATLAB/ode45 to enable the students solving initial value DE and
experience the response of the engineering systems for different applied conditions. Moreover,
both advantages and disadvantages are presented especially the student mostly face in solving
system of DE using ode45 code
Keywords: Advantages, DE, Disadvantages, Engineering, MATLAB, ode45.
1. INTRODUCTION
Engineering software is one of the most important tools in engineering education and they can be
very useful in teaching the working principles of various engineering instruments and devices [1].
Usually differential equations can be used for solving different applications of engineering fields
like electrical, mechanical, civil and chemical besides to the petroleum engineering. Normally, at
the early stages of their study in the faculty of engineering at UAEU, a considerable amount of
time and efforts are needed by the students to solve the differential equations of the mathematical
models of different engineering systems especially the dynamic systems [2]. In general, students
experience applied differential equations through the engineering applications whereas involved
with the classical math solution through the mathematic lecture. One of the objectives of the
course MATH 2210 is to let students practice solving applied differential equations of the
engineering cases [3]. Generally, the mathematical solution of these equations does not readily
which provides the student with a graphical image of the anticipated results, since they deal with
different style of the mathematical models depending on the case studied itself. As a result, the
students are uncomfortable with the entire process of solving these types of systems [4].
As a matter of fact, writing a program from first principles is not an easy task and needs
considerable programming skills [1]. Mainly programs are developed using a high-level
programming language such as FORTRAN, C++, PASCAL or BASIC to solve the differential
equations. The advantages and disadvantages of writing a simulation program can be
summarized as following:
25
Waleed K. Ahmed
26
Waleed K. Ahmed
&H (t ) =
1
CH
&L (t ) =
1
CL
1 &
L (t ) L (t )
qi (t )
RHL
( H (t ) L (t )) 1 ( L (t ) a )
RLa
RHL
(1)
(2)
&H (t ) 1
= CH RHL
1
& C R
L (t ) L HL
1
H (t ) qi (t )
CH RHL
+
1
1
a
(3)
= + ()
&H (t )
1
, A = C H RHL
x =
1
&
C L RHL
L (t )
(4)
C H RHL
1
1
C L RHL C L RLa
H (t )
qi (t )
, B =
x=
a
(t )
RLa
L
(5)
(6)
One of the features that ode45 solver requires is that the system of equations must be in first
order differential equations [8], and this is already generated by the thermal system mathematical
model. Therefore it will be straight forward to program the code necessary for the solution as
shown below without any complication, only needed is to form the mathematical model to be
accepted by the code. The program for the mathematical model is shown below.
Clear;clc
[t,theta] = ode45('ThermalEx3',[0 1000],[300 300]);
plot(t,theta(:,1),t,theta(:,2))
function dthetadt = ThermalEx3(t,theta)
RHL=1e-3; CH=20e3;
RLa=5e-3; CL=1e6;
thetaa=300;qi=2500;
eq1=[-(1/(RHL*CH))*theta(1)+(1/(RHL*CH))*theta(2)+(1/CH)*qi];
eq2=[-((1/(RHL*CL))+(1/(RLa*CL)))*theta(2)+(1/(RHL*CL))*theta(1)+(1/(RLa*CL))*thetaa];
dthetadt =[eq1;eq2];
27
Waleed K. Ahmed
3. SECOND
SYSTEM
ORDER
DIFFERENTIAL
EQUATIONS
OF
MECHANICAL
Usually the mathematical model of mechanical engineering systems is in the form of second
order differential equation, and that because of the Newtons second law. In this example, a
mass-spring-damper is modeled and then its behavior is simulated [11]. The system consists of a
mass (m), a spring (k), and a damper (C). An external force (f) is applied on the mass. The
system can easily be simulated and its response can be plotted as a function of time. Before the
system can be simulated it is necessary to derive its mathematical model. The mathematical
model of mechanical systems usually has the following form:
Mx+Cx+kx=f(t)
(7)
For the above system, simple there are two approaches can be easily used to solve the
mathematical model using MATLAB. In this paper both ODE45 code as the technique that the
students learn through the lectures. Unfortunately using ODE45 is not straight forward way to
solve a second order differential equation, so a modification must be done by the students to be
suitable for the code utilization and solution. By reorganizing a single second order differential to
a couple of first order differential equation by defining a new system of variables [12], the new
system will be quite fit for the ode45 code for the solution. In particular, if a new variables z1 and
z2 are introduced such that:
z1=x
and
z2=x
(8)
(9)
(10)
2
to get:
z2=1/m (f(t)-C.z2-k.z1)
(11)
Equations (3) and (4) constitute a state variable model corresponding to the reduced model. The
variables z1 and z2 are the state variables. The choice of the state variable is not unique, but the
choice must result in a set of first order differential equations, and a consequence the situation
will be more complicated when dealing with more than single equation, and this is one of the most
problems that the students face in this stage. The state variable equation can be written in matrix
form as follows:
0
1
0
1 + 1 ()
1 =
2
2
(12)
0
1
A=
0
1
=
= 1
2
(13)
28
Waleed K. Ahmed
The mechanical system can now be solved the using the MATLAB package. The MATLAB
function developed to simulate the system and plot the displacement and velocity of the system.
The component values can easily be changed and the system can be re-simulated. In this
example, the following component values were chosen for the simulation: m=1 kg, C=2 N.s/m,
k=1000 N/m, initial displacement (Xo)=0.5 m, initial velocity(Vo)=0.2 m/s, force(f)=50*sin(2t).
function dzdt = Mech(t,z)
m=1;
k=100;
C=2;
dzdt = [ z(2); (1/m)*(50*sin(2*t)-C*z(2)-k*z(1)) ];
[t, z] = ode45('Mech',[0,20],[0.5; 0.2]);
plot(t, z)
(14)
(15)
By assuming z1=x, the first step is to introduce a new variable that equals the first derivative of
the free variable in the second order equation z2=y and z3=y'. Taking the derivative of each side
yields the following:
z2'=y'=z3
z3' = y''
(16)
(17)
(18)
Combining (14), (16), and (18) yields three first order differential equations.
z1' = -y * exp(-t/5) + y' * exp(-t/5) + 1;
z2' = y'
z3' = -2*sin(t)
(19)
(20)
(21)
Since z3 = y', substitute z3 for y' in equation (19). Also, since MATLAB requires that all derivatives
are on the left hand side, rewrite equation (20). This produces the following set of equations:
z1' = -z2 * exp(-t/5) + z3 * exp(-t/5) + 1
z2' = z3
z3' = -2*sin(t)
(22)
(23)
(24)
0
z1
z2 = 0
z3
0
exp(t/5)
0
0
1
exp(t/5) z1
0
z2
1
2
sin(t)
z3
0
(23)
29
Waleed K. Ahmed
To evaluate this system of equations using ODE45 or another MATLAB ODE solver, create a
function that contains these differential equations. The function requires two inputs, the states
and time, and returns the state derivatives.
Following is the MATLAB code needed for the solution to evaluate the system of equations using
ODE45 or another MATLAB ODE solver. Define the start and stop times and the initial conditions
of the state vector:
function xprime = odetest(t,z)
eq1 = -z(2) * exp(-t/5) + z(3) * exp(-t/5) + 1;
eq2 = z(3);
eq3 = -2*sin(t);
xprime = [eq1;eq2;eq3];
clc
clear
t0 = 5;
tf = 20; x0 = [1 -1 3] % Initial conditions
[t,z] = ode45('odetest',[t0,tf],x0);
plot(t,z)
Unfortunately this case mostly can be faced in the electrical applications and this cause
complication especially if the mathematical model contain a system of equations and hence the
probability of the mistakes through the transformations stages is very high and will be reflected on
the final results and not forgetting the time consumed. Instead SIMULINK [14] can be used as an
alternate option for the engineering applications.
5. CONCLUSIONS
The present paper shows that dealing with differential equations of the mathematical models of
engineering systems mostly encounter difficulties in term of the MATLAB programming. As a
matter of fact this complication is due to restrictions of the ode45 which is mostly used to solve
such kind of differential equations. Few systems like thermal system are in the form of first order
differential equations which are quite fit with requirements of the ode45. The major restriction of
the MATLAB solve code is that the system of differential equations should be organized in the
form of the first order differential equations, and this frequently is a rare case, whereas the core
engineering application either in the form of second order of even mixed order. Therefore,
transformation of the system of differential equations is mandatory, and this can make mistakes
beside to the time spent.
6. REFERENCES
[1] D. Ibrahim. Engineering simulation with MATLAB: improving teaching and learning
effectiveness. Procedia Computer Science 3, 2011, pp. 853858.
[2] G.F. Simmons and S.G. Krantz. Differential Equations: Theory, Techniques, and Practice,
International Edition, New York: McGraw Hill, 2007.
[3] P.V. O'Neil. Advanced Engineering Mathematics, International Student Edition, Ontario:
Thomson, 1995.
[4] F. Pietryga. Solving Differential Equations Using MATLAB/Simulink, Proceedings of the
American Society for Engineering Education Annual Conference & Exposition, 2005.
[5] J. Walkenbach. Excel 2007 Bible, Indianapolis: John Wiley & Sons, 2007.
30
Waleed K. Ahmed
th
edition, MA:
nd
[9] D. Zill. A First Course in Differential Equations with Modeling and Applications, Brooks/Cole
Publishing Company, 6th ed, 1997.
[10] C. Close, D. Frederick, J. Newell. Modeling and Analysis of Dynamic Systems, John Wiley
and Sons, Inc., 3rd ed, 2002.
[11] K. Ogata. System Dynamics, 4th ed., New Jersy: Pearson Prentice Hall, 2004.
[12] W.J. Palm. System Dynamics, International 2
nd
[13] http://www.mathworks.com/support/tech-notes/1500/1510.html.
[14] W.Ahmed, K.Harib, MATLAB/SIMULINK to Solve Mathematical Models of Engineering
Systems: Class Activity, ICERI2011, Proceedings of the 4th International Conference of
Education, Research and Innovation, Madrid, Spain, 14-16 November, 2011. ISBN: 978-84-6153324-4.
31