© Sulaymon Eshkabilov 2019
S. EshkabilovBeginning MATLAB and Simulinkhttps://doi.org/10.1007/978-1-4842-5061-7_8

8. Ordinary Differential Equations

Sulaymon Eshkabilov1 
(1)
Fargo, United States
 
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:
$$ frac{dy}{dx}=fleft(y,x
ight) $$
(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 $$ =frac{dp}{dt}=frac{mdv}{dt}=Fleft(t,v
ight) $$(Equation 8.2)

Where F(t, v) is force, which is a function of time (t) and velocity (v); $$ frac{dv}{dt} $$ 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 $$ frac{dp}{dt} $$ is its derivative. This formulation of Newton’s Second Law can be also rewritten the following way.
$$ frac{md}{dt}left(frac{dx}{dt}
ight)=kern0.5em frac{m{d}^2x}{d{t}^2}=Fleft(t,x,frac{dx}{dt} 
ight) $$
(Equation 8.3)
Where the derivative $$ left(frac{dx}{dt}
ight) $$ 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.
../images/471979_1_En_8_Chapter/471979_1_En_8_Fig1_HTML.jpg
Figure 8-1

Flowchart expressing motion and exerted force of a moving object

Classifying ODEs

There are two classifications of ODE-related problems:
  • Initial Value Problems (IVPs). $$ ddot{x}= xt-3dot{x} $$ with initial conditions $$ x(0)=3,kern0.5em dot{x}(0)=1 $$

  • Boundary Value Problems (BVPs). $$ ddot{x}= xt-3dot{x} $$ 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: $$ left{egin{array}{c}dot{x}=-0.04x+{10}^4 yz\ {}dot{y}=0.04x-{10}^4 yz-3ast {10}^7{y}^2\ {}dot{z}=3ast {10}^7{y}^2end{array}
ight. $$ t ∈ [0, 40]

  • Non-stiff ODEs: $$ dot{y}+2y=2t $$, $$ ddot{w}+dot{w}=5 $$

  • Linear ODEs: $$ frac{dv}{dt}=9.81-0.198v,kern0.5em ddot{x}+3dot{x}+5x=0 $$

  • Non-linear ODEs: $$ frac{dv}{dt}=9.81-0.198{v}^2, $$ $$ ddot{x}+3xdot{x}+5{x}^2=0 $$

  • Homogeneous ODEs:$$ dot{y}+2y=0,ddot{x}+3dot{x}+5x=0 $$

  • Non-homogeneous ODEs: $$ dot{y}+2y=sin (x),ddot{x}+3dot{x}+5x={e}^{2t} $$

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:

$$ frac{dy}{dt}=mu y $$ 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:

$$ frac{dy}{dt}=-mu y $$ 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 .
$$ frac{m{d}^2y}{d{t}^2}= mg-frac{gamma dy}{dt} $$
This is a second order ODE that has a solution in the form of:
$$ y(t)={C}_1{e}^{-left(frac{gamma t}{m}
ight)}-frac{mleft( mg- ggamma t
ight)}{gamma^2}+{C}_2 $$

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—$$ frac{d^2y}{d{t}^2} $$ (acceleration) and $$ frac{dy}{dt} $$ (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:
Solution = dsolve(equation)
Solution = dsolve(equation, conditions)
Solution = dsolve(equation, conditions, Name, Value)
[y1,...,yN] = dsolve(equations)
[y1,...,yN] = dsolve(equations, conditions)
[y1,...,yN] = dsolve(equations, conditions, Name, Value)

Example 1: Using dsolve

Given an ODE $$ dot{y}+2t{y}^2=0 $$, 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 ODE$$ dot{y}+2t{y}^2=0 $$with 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:
>> syms y(t); Solution=dsolve(diff(y) == -2*y^2*t, y(0)==0.5)
Solution =
 1/(t^2 + 2)
The analytical solution is in a symbolic formulation and thus can be plotted with ezplot. (See Figure 8-2.)
>> ezplot(Solution)
../images/471979_1_En_8_Chapter/471979_1_En_8_Fig2_HTML.png
Figure 8-2

Analytical solution of $$ dot{y}+2t{y}^2=0,y(0)=0.5 $$

Numerical values of the analytical solution (the equation) can be computed by vectorizing (parameterizing) the symbolic formulation (the solution):
>> ysol=vectorize(solution)
ysol =
1./(t.^2 + 2)
>> t=(-5:.1:5); ysol_values=eval(ysol);

Example 3: Adding an Unspecified Parameter

Given $$ dot{y}+ kt{y}^2=0,y(0)=0.5 $$, note that this exercise has one unspecified parameter, k.
>> syms k
>> solution=dsolve('Dy=-k*y^2*t', 'y(0)=0.5')
solution =
1/((k*t^2)/2 + 2)
Here is the alternative command syntax:
>> syms y(t) k; solution=dsolve(diff(y) == -k*y^2*t, y(0)==0.5)
solution =
1/((k*t^2)/2 + 2)

Note

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 example:
solution=dsolve('Dy=-k*y^2*t', 'y(0)=0.5', ... 'IgnoreAnalyticConstraints', 'none')

Note

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.

For example:
solution=dsolve(diff(y)==-k*y^2*t, y(0)==0.5, ... 'IgnoreAnalyticConstraints', false)

Second-Order ODEs and a System of ODEs

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).
$$ ddot{y}+p(x)dot{y}+q(x)y=0 $$
(Equation 8.4)
$$ ddot{y}+p(x)dot{y}+q(x)y=g(x) $$
(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.

Example 1: dsolve with a Second-Order ODE

Given an ODE $$ frac{d^2u}{dt^2}+100u=2.5sin (10t),kern0.5em u(0)=0,kern0.5em frac{du(0)}{dt}=0 $$:
usol=dsolve('D2u+100*u=2.5*sin(10*t)', 'u(0)=0', 'Du(0)=0');
pretty(usol)
%% Alternative syntax
syms u(t)
Du = diff(u);
u(t) = dsolve(diff(u, 2)==2.5*sin(10*t)-100*u, u(0)==0, Du(0) == 0);
pretty(u(t))
The output from executing the two short scripts/commands is:
3 sin(10 t)   sin(30 t)
  ----------- - --------- -
      320          320
               / t   sin(20 t)
     cos(10 t) | - - --------- |
                8      160    /

Example 2: System ODEs

Given a system of ODEs: $$ left{egin{array}{c}{y}_1^{prime }={y}_2\ {}{y}_2^{prime }=-{y}_1-0.125{y}_2end{array}
ight.,kern0.5em {y}_1(0)=1,{y}_2(0)=0 $$;

The given problem is a system of two first order ODEs. This problem can be solved directly with dsolve, similar to the previous examples:
%% System of two 1st order ODEs solved with dsolve
yt=dsolve('Dy1=y2', 'Dy2=-y1-0.125*y2','y1(0)=1', 'y2(0)=0');
pretty(yt.y1)
pretty(yt.y2)
%% Alternative syntax
syms y1(t) y2(t)
z=dsolve(diff(y1,1)==y2, diff(y2,1)==(-y1-0.125*y2), y1(0)==1, y2(0)==0);
pretty(z.y1), pretty(z.y2)
The computed analytical solutions of the problem displayed in the command window are:
     /    1/2                /    1/2   
     | 255    t |      1/2    | 255    t |
  cos| -------- |   255    sin| -------- |
         16    /                 16    /
  --------------- + ----------------------
          1/16                    1/16
    exp(t)              255 exp(t)
                 /    1/2   
          1/2    | 255    t |
    16 255    sin| -------- |
                     16    /
  - -------------------------
                   1/16
         255 exp(t)
The computed analytical solutions are:
$$ {y}_1(t)=frac{mathit{cos}left(frac{sqrt{255}t}{16}
ight)}{sqrt[16]{e^t}}+frac{sqrt{255}mathit{sin}left(frac{sqrt{255}t}{16}
ight)}{255sqrt[16]{e^t}} $$
$$ {y}_2(t)=-frac{16sqrt{255}mathit{sin}left(frac{sqrt{255}t}{16}
ight)}{255sqrt[16]{e^t}} $$

Example 3: Unsolvable Solutions Using dsolve

Given a second order ODE: $$ 2ddot{y}+3{dot{y}}^3-left|y
ight|cos (100t)=2 $$, y(0) = 0 and $$ dot{y}(0)=0. $$
>> Solution_dsolve=dsolve('2*D2y+3*(Dy^3)-cos(100*t)*abs(y)-2=0', ... 'y(0)=0','Dy(0) =0');
Warning: Explicit solution could not be found.
> In dsolve at 194

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

$$ dot{y}-left|y
ight|{e}^t=2,y(0)=2. $$ Figure 8-3 shows the commands used to compute an analytical solution of the given exercise.
../images/471979_1_En_8_Chapter/471979_1_En_8_Fig3_HTML.jpg
Figure 8-3

MuPAD commands to solve $$ dot{y}-left|y
ight|{e}^t=2,y(0)=2 $$ analytically

Example 5: An Interesting ODE

Let’s consider a second order ODE, as follows: $$ ddot{y}+dot{y}=sin tmathrm{with} y(0)=1,kern0.5em dot{y}(0)=2. $$

../images/471979_1_En_8_Chapter/471979_1_En_8_Fig4_HTML.jpg
Figure 8-4

MuPAD tools used to solve a second order ODE

Example 6: Responding to No Analytical Solution

Given $$ ddot{y}+3{dot{y}}^3-left|y
ight|mathrm{y}=2 $$, y(0) = 0 and $$ dot{y}(0)=0 $$, this is a second order non-homogeneous and non-linear ODE.
[Y:=ode({y"(t)+3*(y'(t)^3)-y(t)*abs(y(t))=2,y(0)=0,y'(0)=0},y(t))
../images/471979_1_En_8_Chapter/471979_1_En_8_Figa_HTML.jpg
[Ysolution:=solve(Y)
../images/471979_1_En_8_Chapter/471979_1_En_8_Figb_HTML.jpg

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:
$$ mathcal{L}left{frac{d^ny}{d{t}^n}=f(t)
ight}=>{s}^nY(s)-{s}^{n-1}{y}^{n-1}(0)-{s}^{n-2}{y}^{n-2}(0)- $$
$$ -s{y}^{prime }(0)-y(0)=F(s) $$
(Equation 8.6)
Or
$$ {s}^nY(s)-{sum}_{i=1}^n{s}^{n-1}{y}^{i-1}(0)=F(s) $$
(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:
$$ mathcal{L}left{frac{d^ny}{d{t}^n}=f(t)
ight}=>{s}^nY(s)-{s}^{n-1}{a}_0-{s}^{n-2}{a}_1-{s}^{n-3}{a}_2-dots -s {a}_{n-2}-{a}_{n-1}=F(s) $$
(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. 1.

    Take the Laplace transforms from both sides of the given equation.

     
  2. 2.

    Solve for Y(s) in terms of F(s) and other unknowns.

     
  3. 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.
../images/471979_1_En_8_Chapter/471979_1_En_8_Fig5_HTML.jpg
Figure 8-5

Flowchart of solving ODE with Laplace transform and its inverse

Example 1: First Laplace Transform

Let’s consider a second order non-homogeneous differential equation: $$ frac{d^2y}{d{t}^2}+Afrac{dy}{dt}+C={e}^{nt} $$, 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.
$$ mathcal{L}left{frac{d^2y}{d{t}^2}+Afrac{dy}{dt}+C={e}^{nt} 
ight}=>Lleft{frac{d^2y}{d{t}^2}
ight}+mathcal{L}left{Afrac{dy}{dt}
ight}+mathcal{L}left{C
ight}=mathcal{L}left{{e}^{nt}
ight} $$
(Equation 8.9)
$$ mathcal{L}left{{e}^{nt}
ight}=frac{1}{s-n} $$
(Equation 8.10)
So,
$$ mathcal{L}left{frac{d^2y}{d{t}^2}
ight}={s}^2Y(s)- dy(0)-sast y(0)={s}^2Y(s)-k-sast m $$
(Equation 8.11)
$$ mathcal{L}left{Afrac{dy}{dt}
ight}=Aast mathcal{L}left{frac{dy}{dt}
ight}=Aast left(sast Y(s)-y(0)
ight)=Aast sast Y(s)-m $$
(Equation 8.12)
$$ mathcal{L}left{C
ight}=C $$
(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:
$$ {s}^2Y(s)-k- sm+ AsY(s)-m+C=frac{1}{s-n} $$
(Equation 8.14)
We solve Equation 8.5 for Y(s) to determine the following:
$$ Y(s)=frac{1+left( sm+left(k+m-C
ight)
ight)left(s-n
ight)}{left(s-n
ight)left({s}^2+ As
ight)}=-frac{Cn- Cs- kn- mn+ ks+ ms+m{s}^2}{sleft(A+s
ight)left(n-s
ight)} $$
(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:
$$ y(t)=frac{e^{nt}}{An+{n}^2}-frac{Cn- kn- mn+1}{An}+frac{Cn- kn- mn-Aleft(k- mn-C
ight)+1}{A{e}^{At}left(A+n
ight)} $$
(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

$$ y(x)=a{x}^3+b. $$
>> 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 $$ dot{y}+2y=0,y(0)=0.5 $$ 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
LT_A=subs(LT_A,{'laplace(y(t),t, s)','y(0)'},{Y,0.5});
% Step #4. Solve for Y (unknown)
Y=solve(LT_A, Y);
display('Laplace Transforms of the given ODE with ICs'); disp(Y)
% Step #5. Evaluate Inverse Laplace Transform
Solution_Laplace=ilaplace(Y);
display('Solution found using Laplace Transforms: ')
pretty(Solution_Laplace)
% Step #6. Compute numerical values and plot them
t=0:.01:2.5; LTsol=eval(vectorize(Solution_Laplace));
figure, semilogx(t, LTsol, 'ro-')
xlabel('t'), ylabel('solution values')
title('laplace/ilaplace vs dsolve ')
grid on; hold on
% Compare with dsolve solution method
Y_d=dsolve('Dy=-2*y', 'y(0)=0.5'); display('Solution with dsolve')
pretty(Y_d); Y_sol=eval(vectorize(Y_d));
plot(t,Y_sol, 'b-', 'linewidth', 2), grid minor
legend('laplace+ilaplace', 'dsolve'), hold off; axis tight
After executing the ODE_Laplace.m script, the following output is obtained.
Laplace Transforms of the given ODE with ICs
1/(2*s + 4)
Solution found using Laplace Transforms:
  exp(-2 t)
  ---------
      2
Solution with dsolve
  exp(-2 t)
  ---------
      2
The plot of solutions shown in Figure 8-6 clearly displays perfect convergence of solutions found with laplace/ilaplace and dsolve .
../images/471979_1_En_8_Chapter/471979_1_En_8_Fig6_HTML.png
Figure 8-6

The problem $$ dot{y}+2y=0,y(0)=0.5 $$ solved with laplace/ilaplace and dsolve

Example 5: Convergent Answers

Given $$ ddot{y}+dot{y}=sin t,y(0)=1,dot{y}(0)=2 $$, here is the solution script (Laplace_vs_Dsolve.m) of this second-order non-homogeneous ODE with laplace, ilaplace, and dsolve.
% Laplace_vs_Dsolve.m
% %%   LAPLACE TRANSFORMS
% Given: y"+y'=sin(t) with ICs: [1, 2] for y(0) & y'(0).
clearvars, clc, close all
syms t s Y
ODE2nd='D(D(y))(t)+D(y)(t)-sin(t)';
% Step 1. Laplace Transforms
LT_A=laplace(ODE2nd, t, s);
% Step 2. Substitute ICs and initiate an unknown Y
LT_A=subs(LT_A,{'laplace(y(t),t, s)','y(0)','D(y)(0)'},{Y,1,2});
% Step 3.  Solve for Y unknown
Y=solve(LT_A, Y);
display('Laplace Transforms of the given ODE with ICs'); disp(Y)
Solution_Laplace=ilaplace(Y);
display('Solution found using Laplace Transforms: ')
pretty(Solution_Laplace); t=0:.01:13;
LTsol=eval(vectorize(Solution_Laplace));
figure, plot(t, LTsol, 'ro-'); xlabel('t'),
ylabel('solution values')
title('laplace/ilaplace vs. dsolve: ddy+dy=sin(t)'); hold on
% dsolve solution method
Y=dsolve('D2y+Dy=sin(t)', 'y(0)=1, Dy(0)=2', 't');
display('Solution with dsolve:  ');
pretty(Y); Y_sol=eval(vectorize(Y));
plot(t,Y_sol, 'b-', 'linewidth', 2); grid minor
legend('laplace+ilaplace', 'dsolve'); hold off
The computed analytical solutions are as follows:
Laplace Transforms of the given ODE with ICs
(s + 1/(s^2 + 1) + 3)/(s^2 + s)
Solution found using Laplace Transforms:
      cos(t)   sin(t)   5 exp(-t)
  4 - ------ - ------ - ---------
        2        2          2
Solution with dsolve:
      cos(t)   sin(t)   5 exp(-t)
  4 - ------ - ------ - ---------
        2        2          2
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.
../images/471979_1_En_8_Chapter/471979_1_En_8_Fig7_HTML.png
Figure 8-7

Numerical solutions of $$ ddot{y}+dot{y}=mathit{sin}(t),y(0)=1,kern0.5em dot{y}(0)=2 $$

Example 6: No Analytical Solution

Given a second order non-homogeneous and non-linear ODE $$ 2ddot{y}+3{dot{y}}^3-left|y
ight|cos (t)=2 $$, y(0) = 0 and $$ dot{y}(0)=0. $$ Here is the solution script (Lap_inv_Lap.m) with the Laplace and inverse Laplace transforms:
% Lap_inv_Lap.m
clearvars, clc, close all
syms t s Y
ODE2nd='2*D(D(y))(t)+3*(D(y)(t))^3-cos(100*t)*abs(y(t))-2';
% Step 1. Laplace Transforms
LT_A=laplace(ODE2nd, t, s);
% Step 2. Substitute ICs and initiate an unknown Y
LT_A=subs(LT_A,{'laplace(y(t),t, s)','y(0)','D(y)(0)'},{Y,0,0});
% Step 3.  Solve for Y unknown
Y=solve(LT_A, Y);
display('Laplace Transforms of the given ODE with ICs'); disp(Y)
Solution_Laplace=ilaplace(Y);
display('Solution found using Laplace Transforms: ')
pretty(Solution_Laplace)
The Lap_inv_Lap.m script produces the following output in the command window:
Laplace Transforms of the given ODE with ICs
(4 + s*laplace(abs(y(t)), t, s - 100*i) + s*laplace(abs(y(t)), t, s + 100*i) - 6*s*laplace(D(y)(t)^3, t, s))/(4*s^3)
Solution found using Laplace Transforms:
                 /                3              
                 | laplace(D(y)(t) , t, s)       |
       3 ilaplace| -----------------------, s, t |
   2             |            2                  |
  t                         s                   /
  -- - ------------------------------------------- +
  2                         2
             / laplace(|y(t)|, t, s - 100 i)       
     ilaplace| -----------------------------, s, t |
             |               2                     |
                           s                      /
     ----------------------------------------------- +
                            4
             / laplace(|y(t)|, t, s + 100 i)       
     ilaplace| -----------------------------, s, t |
             |               2                     |
                           s                      /
     -----------------------------------------------
                            4

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 $$ 2ddot{y}+3dot{y}-2y=g(t) $$, y(0) = 0 and $$ dot{y}(0)=0 $$, where g(t) is a forcing function that is discontinuous and defined by
$$ g(t)={u}_2(t)-{u}_{10}(t)=left{egin{array}{c}5,kern7.75em 2le t<10,\ {}0,kern3em 0le t<2kern0.5em mathrm{and} tge 10.kern0.5em end{array}
ight. $$
The Laplace transform of the given equation is:
$$ mathcal{L}left{2ddot{y}+3dot{y}-2y
ight}=mathcal{L}left{{u}_2(t)-{u}_{10}(t)
ight} $$
$$ 2{s}^2Y(s)-2sast y(0)-2ast dot{y}(0)+3 sY(s)-y(0)-2Y(s)=frac{5left({e}^{-2s}-{e}^{-10s}
ight)}{s} $$
$$ Y(s)=frac{5left({e}^{-2s}-{e}^{-10s}
ight)}{sleft(2{s}^2+3s-2
ight)} $$

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: $$ {mathcal{L}}^{-1}left{Y(s)
ight}=y(t) $$. 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.
% Lap_4_non_homog.m
syms t s
F=5*(exp(-2*s)-exp(-10*s))/s; Y=2*s^2+s+2;
TF=F/Y; TFt=ilaplace(TF); pretty(TFt); Sol=vectorize(TFt);
t=linspace(0, 20, 400); S=eval(Sol); plot(t, S, 'bo-');
grid minor
title('Differential Equation with Discontinuous Forcing Fcn')
grid on, xlabel('time'), ylabel('y(t) solution'), shg
After executing the script, the next solution plot is obtained along with the solution formulation, as shown in Figure 8-8.
../images/471979_1_En_8_Chapter/471979_1_En_8_Fig8_HTML.png
Figure 8-8

Simulation of the second order non-homogeneous ODE subject to the discontinuous forcing function

>> pretty(TFt)
                      /                /             1/2                
                      |    /       t |           15    sin(#1) |       |
                      | exp| 5/2 - - | | cos(#1) + ------------- |       |
                      |           4 /                 15       /       |
  5 heaviside(t - 10) | ------------------------------------------ - 1/2 |
                                           2                            /
                        /                /             1/2                
                        |    /       t |           15    sin(#2) |       |
                        | exp| 1/2 - - | | cos(#2) + ------------- |       |
                        |           4 /                 15       /       |
     5 heaviside(t - 2) | ------------------------------------------ - 1/2 |
                                             2                            /
  where
             1/2
           15    (t - 10)
     #1 == --------------
                 4
             1/2
           15    (t - 2)
     #2 == -------------
                 4

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.

References

  1. [1]
     
  2. [2]

    Gear, C.W., Numerical Initial-Value problems in Ordinary Differential Equations, Prentice-Hall, Englewood Cliffs, N.J. (1971).

     
  3. [3]

    Potter M. C., Goldberg J.L., Aboufadel E.F., Advanced Engineering Mathematics, 3rd Edition, Oxford University Press, (2005).

     
  4. [4]

    Boyce W.E., Diprima R.C., Elementary Differential Equations and Boundary Value Problems, 7th Edition, John Wiley & Son, Inc, (2003).

     
  5. [5]

    Hairer E., Norsett S. P., Wanner G., Solving ordinary differential equations I: Non-stiff problems, 2nd Edition, Berlin: Springer Verlag, ISBN 978-3-540-56670-0, (1993).

     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.5