Many modeling problems with engineering applications can be formulated using ordinary differential equations (ODEs). There are a few different definitions of differential equations. One of the simplest is “A differential equation is any equation which contains derivatives, either ordinary derivatives or partial derivatives,” as given in source [1]. From this definition, we can derive two types of differential equations—ordinary differential equations (ODEs) and partial differential equations (PDEs). ODEs contain one type of derivative or one independent variable and PDEs, on the contrary, contain two or more derivatives or independent variables. For example, first order ODEs can be expressed by:
(Equation 8.1)
Where y(x) is a dependent variable whose values depend on values of the independent variable of x. Another good example of ODEs is Newton’s Second Law of Motion, formulated by:
ma (Equation 8.2)
Where F(t, v) is force, which is a function of time (t) and velocity (v); is a velocity change rate (acceleration) of a moving object; m is the mass of a moving object; a is an acceleration of a moving object; p is momentum; and is its derivative. This formulation of Newton’s Second Law can be also rewritten the following way.
(Equation 8.3)
Where the derivative of the displacement (x) of a moving object is the velocity (v). In other words, the velocity is the rate of change of the displacement x(t) of a moving object in time. This can be visualized with the flowchart displayed in Figure 8-1.
Classifying ODEs
There are two classifications of ODE-related problems:
Initial Value Problems (IVPs). with initial conditions
Boundary Value Problems (BVPs). with boundary conditions x(0) = 3, x(2) = 1.50
Initial Value Problems (IVPs) are defined with ODEs together with a specified value, called the initial condition, of the unknown function at a given point in the solution domain. In the IVP of ODEs, there can be a unique solution, no solution, or many solutions. By definition, the IVP of ODEs can be explicitly or implicitly defined. Most of the IVP are explicitly defined. We start with explicitly defined IVPs and then move to implicitly defined ones. Besides by solution type—how solution values change over the solution search space—the IVP are divided into stiff and non-stiff problems. Moreover, ODEs are grouped into two categories, linear and non-linear, and divided into two groups, homogeneous and non-homogeneous.
Here are some specific examples of different ODE types, categories, and groups.
Stiff ODEs: t ∈ [0, 40]
Non-stiff ODEs: ,
Linear ODEs:
Non-linear ODEs:
Homogeneous ODEs:
Non-homogeneous ODEs:
Here are several examples of ODEs and their application areas.
Example 1: Unconstrained Growth of Biological Organisms
This is an exponential growth problem that describes unconstrained growth of biological organisms (such as bacteria). This behavior can also describe real estate or investment values, membership increase of a popular networking site, growth in retail businesses, positive feedback of electrical systems, and generated chemical reactions. The problem is formulated by the following first order ODE:
has a solution: y(t) = y0eμt
Example 2: Radioactive Decay
This refers to exponential decay that describes many phenomena in nature and in engineering, such as radioactive decay, washout of chemicals in a reactor, discharge of a capacitor, and decomposition of material in a river. It’s expressed using this first order ODE:
has a solution: y(t) = y0e−μt
Examples 1 and 2 are two simple examples of first order ODEs.
Example 3: Newton’s Second Law
The motion of a falling object is expressed in the following equation using Newton’s Second Law.
This is a second order ODE that has a solution in the form of:
Where m is the mass of the falling object; g is gravitational acceleration; and γ is an air-drag coefficient of a falling object. Three parameters—m, g, and γ—are constant, and two parameters change over time— (acceleration) and (velocity). In the solution of a falling object, C1 and C2 are arbitrary numbers that are dependent on the initial conditions. In other words, they can be computed considering the initial condition of a falling object.
There are a few methods that evaluate analytical solutions of ODEs, including separation of variables, introduction of new variables, and others. We look at specific examples of these types of ODEs to see how to evaluate their analytical solutions and compute numerical solutions. We do this by employing different techniques in the MATLAB/Simulink environment and writing scripts and building models. In computing analytical solutions of ODEs, we explain using specific examples how to use built-in functions of the Symbolic Math Toolbox1 and the MuPAD2 Notebooks. We put more emphasis on the Symbolic Math Toolbox’s command syntax rather than the MuPAD Notebooks. In future releases of MATLAB, MuPAD Notebooks will be removed.
For obvious reasons, considerable effort is placed on numerical solution methods rather than analytical solution search tools. It is not always possible or is too costly to evaluate analytical solutions of ODEs. Therefore, a numerical solution search is often best. There are a number of numerical methods. They are Euler (forward, backward, modified), Heun, mid-point rule, Runge-Kutta, Runge-Kutta-Gill, Adams-Bashforth, Milne, Adams-Moulton, Taylor series, and trapezoidal rule methods. Some of these methods are explicit and others are implicit. In order to demonstrate how to employ these methods, we first describe their formulations and then work on their implementation algorithm for writing scripts (programs) explicitly. We do not attempt to derive any of the formulations used in these numerical methods. There are many literature sources [see 2, 3, 4, 5] that explain the theoretical aspects of these methods.
In solving the IVP using numerical methods, we start at an initial point (initial conditions) and then take a step (equal step-size or varying step-size) forward in time to compute the following numerical solution. Some of the previously named numerical methods (e.g. Euler’s methods) are single-step methods and others (Runge-Kutta, Adams-Bashforth, Milne, Adams-Moulton, and the Taylor series) are multi-step methods. Single-step methods refer to only one previous point and its derivative to determine the current value. Other methods, such as Runge-Kutta methods, take some intermediate steps to obtain a higher-order step and then drop off values before taking the next step. Unlike single-step methods, multi-step methods keep and use values from the previous steps instead of discarding them. This way, multi-step methods link a few previously obtained values (solutions) and derivative values. All of these methods, i.e., the single-step and multi-step methods, are assessed based on their accuracy and efficiency in terms of the computation time and resources (e.g., machine time) spent to compute numerical solutions for specific types of IVPs of ODEs. Nevertheless, it remains true that most solutions of the first, second, or higher order IVPs cannot be found by analytical means. Therefore, we need to employ various numerical methods.
Analytical Methods
The Symbolic Math Toolbox (or MuPAD Notebooks) has several functions that are capable of evaluating analytical solutions of many analytically solvable ODEs. There are two commands (built-in functions) by which analytical solutions of some ODEs can be evaluated—dsolve and ilaplace/laplace.
Note that in this subsection we demonstrate—via a few examples of first and second order ODEs and systems of coupled differential equations—how to compute analytical solutions of ODEs.
DSOLVE
A very first ODE solver tool for computing an analytical (or general) solution of any given ODE in MATLAB is dsolve. It can be used with the following general syntax:
Given an ODE , note that the initial and boundary conditions are not specified.
>> y_solution=dsolve('Dy=-2*y^2*t')
Y_solution=-1/(C3-t^2)
Note that C3 is defined from the initial or boundary conditions of the given ODE.
There is also an alternative command. In later versions of MATLAB (starting with MATLAB 2012), we can solve the given problem by using the following command syntax:
>>syms y(t); y_sol=dsolve(diff(y) == -2*y^2*t)
y_sol =
0
-1/(- t^2 + C3)
Example 2: Plotting from dsolve
Given an ODEwith the initial condition y(0) = 0.5:
>> Solution=dsolve('Dy=-2*y^2*t', 'y(0)=0.5')
Solution =
1/(t^2 + 2)
The alternative command syntax with later versions of MATLAB is as follows:
Options in dsolve need to be set appropriately depending on the problem type. In MATLAB 2008-2010 or earlier versions, we should set IgnoreAnalyticConstraints to none in order to obtain all possible solutions.
For MATLAB 2012 or later versions, we should set IgnoreAnalyticConstraints to false in order to get all possible correct answers for all the argument values. Otherwise, dsolve may output an incorrect answer due to its pre-algebraic simplifications.
There are a myriad of processes and phenomena that are expressed via second order differential equations. For example, simple harmonic motions of a spring-mass system, motions of objects with some acceleration (Newton’s Second Law), damped vibrations, current flows in resistor-capacitor-inductance circuits, and so forth. In general, second order ODEs are expressed in two different forms—homogeneous (Equation 8.4) and non-homogeneous (Equation 8.5).
(Equation 8.4)
(Equation 8.5)
Note that the homogeneous ODEs in Equation 8.4 always have one trivial solution, which is, y(x) = 0 and it satisfies the givens in Equation 8.4. With respect to the independent functions p(x), q(x), g(x), the ODEs can be linear or non-linear. In some cases, the independent functions p(x), q(x), g(x) can be constant values or non-constant values.
Let’s consider several examples of second order ODEs to see how to compute general and particular solutions with MATLAB’s Symbolic Math Toolbox.
This is a good approach to ODEs that cannot be solved analytically by using dsolve.
Let’s consider several examples of computing analytical solutions of ODEs in MuPAD Notebooks. We can open MuPAD by typing this command in the command window:
>> mupad
Example 4: Computing an Analytical Solution
Figure 8-3 shows the commands used to compute an analytical solution of the given exercise.
Example 5: An Interesting ODE
Let’s consider a second order ODE, as follows:
Example 6: Responding to No Analytical Solution
Given , y(0) = 0 and , this is a second order non-homogeneous and non-linear ODE.
No solution is computed in this exercise. When MuPAD cannot compute an analytical solution of a given ODE problem, it rewrites a given problem formulation and produces no analytical solution expression.
Laplace Transforms
Solutions of linear ordinary differential equations with constant coefficients can be evaluated by using the Laplace transformation. One of the most important features of the Laplace transforms in solving a differential equation is that the transformed equation is an algebraic equation. It will be used to define a solution to the given differential equation. In general, the Laplace transform application to solving differential equations can be formulated in the following way.
Let’s consider the nth order derivative of yn(x) = f(t). The Laplace transform of yn(x) is as follows:
(Equation 8.6)
Or
(Equation 8.7)
In Equations 8.6 or 8.7, if we substitute constant values of initial conditions at t = 0 given as y(0) = a0; y′(0) = a1; y"(0) = a2; …; yn − 2(0) = an − 2; yn − 1(0) = an − 1.
Now, we can rewrite the expression (Equation 8.6 or 8.7) as follows:
(Equation 8.8)
Subsequently, we first solve for Y(s), take the inverse Laplace transform from Y(s), and obtain the solution y(t) of the nth order differential equation.
The general procedure for applying the Laplace and the inverse Laplace transforms to determine the solution of differential equations with constant coefficients is as follows.
1.
Take the Laplace transforms from both sides of the given equation.
2.
Solve for Y(s) in terms of F(s) and other unknowns.
3.
Take the inverse Laplace transform of the found expression in order to obtain the final solution of the problem.
Note that in Step 3, we should also break the expression from Step 2 into partial fractions in order to use tables of the inverse Laplace transform correspondences. A schematic view of the Laplace and inverse Laplace transforms is given in the flowchart shown in Figure 8-5.
Example 1: First Laplace Transform
Let’s consider a second order non-homogeneous differential equation: , dy(0) = k, y(0) = m.
Now, by applying the steps depicted in the flowchart of the Laplace and inverse transforms from the flowchart, we write the Laplace transform of the given problem in explicit steps.
(Equation 8.9)
(Equation 8.10)
So,
(Equation 8.11)
(Equation 8.12)
(Equation 8.13)
By plugging Equations 8.10, 8.11, 8.12, and 8.13 back into Equation 8.9, we obtain the this expression:
(Equation 8.14)
We solve Equation 8.5 for Y(s) to determine the following:
(Equation 8.15)
From the expression of Y(s) in Equation 8.15, we can split this into partial fractions and take the inverse Laplace transform of both sides. We obtain Equation 8.16, which is the y(t) of the given differential equation:
(Equation 8.16)
The built-in laplace() function of the Symbolic Math Toolbox is used to evaluate the Laplace transform of any algebraic expression or differential equation. Likewise, the ilaplace() function of the Symbolic Math Toolbox is used to compute the inverse of the evaluated Laplace transformed s domain expression. These two functions handle all transformations by breaking up the partial fraction procedures automatically, and then compute an analytical solution of a given ODE exercise.
LAPLACE/ILAPLACE
As mentioned, laplace/ilaplace are based on the Laplace and inverse Laplace transforms, which are built-in function tools of the Symbolic Math Toolbox. The general syntax of laplace/ilaplace is as follows:
F=laplace(f)
F=laplace(f, t)
F=laplace(f, var1, var2)
and
f=ilaplace(F)
f=ilaplace(F, s)
f=ilaplace(F, var1, var2)
Example 2: Using LAPLACE
Given x(t) = sin (2t), the Laplace transform of x(t) is computed with the following command syntax.
>> syms t
>> xt=sin(2*t); Xs=laplace(xt)
Xs =
2/(s^2 + 4)
Given: y(t) = sin (Kt)
>> syms t K
>> yt=sin(K*t); Ys=laplace(yt)
Ys =
K/(K^2 + s^2)
Example 3: A Final LAPLACE
>> syms x a b
>> yx=a*x^3+b; Ys=laplace(yx)
Ys =
(6*a)/s^4 + b/s
We can also obtain the t variable domain instead of s.
>> yx=a*x^3+b; Yt=laplace(yx, x, t)
Yt =
(6*a)/t^4 + b/t
The ilaplace() function syntax and implementation are exactly the same for laplace. Let’s look at several ODE exercises to see how to use laplace/ilaplace and compare their evaluated solutions to the ones obtained with dsolve.
Example 4: Comparing LAPLACE/ILAPLACE with DSOLVE
Let’s solve with laplace/ilaplace and dsolve. The following script (ODE_Laplace.m) shows the solution.
% ODE_Laplace.m
clearvars; clc; close all
% Step #1. Define symbolic variables' names
syms t s Y
ODE1='D(y)(t)=-2*y(t)';
% Step #2. Laplace Transforms
LT_A=laplace(ODE1, t, s);
% Step #3. Substitute ICs and initiate an unknown Y
From the plot displayed in Figure 8-7, it is clear that the solutions found via the Laplace transforms (laplace/ilaplace) and the dsolve functions converge perfectly well. Both functions evaluate the same analytical solution of a given ODE.
Example 6: No Analytical Solution
Given a second order non-homogeneous and non-linear ODE , y(0) = 0 and Here is the solution script (Lap_inv_Lap.m) with the Laplace and inverse Laplace transforms:
This output means that no analytical solution is computed explicitly with laplace/ilaplace, just like with the dsolve function tools.
Example 7: Demonstrating Efficiency and Effortlessness
Given a second order non-homogeneous ODE , y(0) = 0 and , where g(t) is a forcing function that is discontinuous and defined by
The Laplace transform of the given equation is:
Note that e−2s, e−10s are explained with time delays in the system output signals; in other words, -2 and -10 mean 2 and 10 seconds of time delays. 5 is the magnitude of the Heaviside (step) function.
The formulation Y(s) is the solution of the differential equation in the s domain, but we need it in the time domain. Thus, we need to compute its inverse Laplace transform: . By employing ilaplace(), the next short script (Lap_4_non_homog.m) is created. It solves the given problem and computes its analytical and numerical solutions.
From the last exercise, the following points can be drawn. Using the Laplace transforms (laplace/ilaplace) to compute analytical solutions of non-homogeneous ODEs subject to external forcing functions, which are discontinuous, is relatively easy, fast, and effortless. Such exercises are found very often in particular within control engineering problems. Moreover, it must be noted that the Laplace and inverse Laplace transforms (laplace/ilaplace) are straightforward to implement in solving ODEs. The solutions of ODEs found with them match the ones found by dsolve() perfectly well. As mentioned, many ODEs cannot be solved analytically with the laplace/ilaplace and dsolve functions. Thus, numerical methods are often the only option.