Practical MATLAB
Modeling with Simulink
Programming and Simulating
Ordinary and Partial Differential
Equations
Sulaymon L. Eshkabilov
Practical MATLAB Modeling with Simulink: Programming and Simulating Ordinary
and Partial Differential Equations
Sulaymon L. Eshkabilov
Ag & Biosystems Engineering Department, North Dakota State University, Fargo, USA
ISBN-13 (pbk): 978-1-4842-5798-2 ISBN-13 (electronic): 978-1-4842-5799-9
[Link]
Copyright © 2020 by Sulaymon L. Eshkabilov
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Coordinating Editor: Mark Powers
Cover designed by eStudioCalamar
Cover image from [Link]
Distributed to the book trade worldwide by Springer Science+Business Media, 1 New York Plaza, New York,
NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@[Link], or visit
[Link]. Apress Media, LLC is a California LLC and the sole member (owner) is Springer
Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail editorial@[Link]; for reprint, paperback, or audio rights,
please e-mail bookpermissions@[Link].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at [Link]/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at [Link]/9781484257982. For more
detailed information, please visit [Link]/source-code.
Printed on acid-free paper
To the memory of my father.
To my mother.
To my wife, Nigora, after 25 wonderful years together.
Table of Contents
About the Author ................................................................................................... xiii
About the Technical Reviewer .................................................................................xv
Acknowledgments .................................................................................................xvii
Introduction ............................................................................................................xix
Part I: Ordinary Differential Equations ............................................................ 1
Chapter 1: Analytical Solutions for ODEs.................................................................. 3
Classifying ODEs ........................................................................................................................... 4
Example 1 ..................................................................................................................................... 5
Example 2 ..................................................................................................................................... 6
Example 3 ..................................................................................................................................... 6
Analytical Solutions of ODEs ......................................................................................................... 8
dsolve( ) ................................................................................................................................... 8
Example 4 ..................................................................................................................................... 8
Example 5 ..................................................................................................................................... 9
Example 6 ................................................................................................................................... 11
Example 7 ................................................................................................................................... 11
Second-Order ODEs and a System of ODEs ................................................................................ 13
Example 8 ................................................................................................................................... 13
Example 9 ................................................................................................................................... 14
Example 10 ................................................................................................................................. 15
Example 11 ................................................................................................................................. 16
Example 12 ................................................................................................................................. 16
Example 13 ................................................................................................................................. 17
Laplace Transforms ..................................................................................................................... 22
v
TABLE OF CONTENTS
Example 14 ................................................................................................................................. 24
laplace/ilaplace ..................................................................................................................... 25
Example 15 ................................................................................................................................. 25
Example 16 ................................................................................................................................. 26
Example 17 ................................................................................................................................. 26
Example 18 ................................................................................................................................. 26
Example 19 ................................................................................................................................. 30
Example 20 ................................................................................................................................. 34
Example 21 ................................................................................................................................. 36
References .................................................................................................................................. 40
Chapter 2: Numerical Methods for First-Order ODEs .............................................. 41
Euler Method ............................................................................................................................... 41
Example 1 ................................................................................................................................... 42
Improved Euler Method ............................................................................................................... 44
Backward Euler Method .............................................................................................................. 45
Example 2 ................................................................................................................................... 47
Midpoint Rule Method ................................................................................................................. 50
Example 3 ................................................................................................................................... 51
Ralston Method ........................................................................................................................... 55
Runge-Kutta Method ................................................................................................................... 56
Example 4 ................................................................................................................................... 57
Runge-Kutta-Gill Method ............................................................................................................ 60
Runge-Kutta-Fehlberg Method ................................................................................................... 63
Adams-Bashforth Method ........................................................................................................... 66
Example 5 ................................................................................................................................... 66
Milne Method .............................................................................................................................. 72
Example 6 ................................................................................................................................... 73
Taylor Series Method .................................................................................................................. 75
Example 7 ................................................................................................................................... 75
Adams-Moulton Method.............................................................................................................. 78
vi
TABLE OF CONTENTS
Example 8 ................................................................................................................................... 80
MATLAB’s Built-in ODE Solvers ................................................................................................... 85
Example 9 ................................................................................................................................... 87
The OPTIONS, ODESET, and ODEPLOT Tools of Solvers .......................................................... 93
Example 10 ................................................................................................................................. 95
Example 11 ................................................................................................................................. 98
Simulink Modeling .................................................................................................................... 103
Example 12 ............................................................................................................................... 103
SIMSET ...................................................................................................................................... 111
References ................................................................................................................................ 112
Chapter 3: Numerical Methods for Second-Order ODEs ....................................... 113
Euler Method ............................................................................................................................. 114
Example 1 ................................................................................................................................. 114
Example 2 ................................................................................................................................. 118
Example 3 ................................................................................................................................. 120
Example 4 ................................................................................................................................. 122
Example 5 ................................................................................................................................. 124
Runge-Kutta Method ................................................................................................................. 128
Example 6 ................................................................................................................................. 128
Example 7 ................................................................................................................................. 131
Example 8 ................................................................................................................................. 134
Example 9 ................................................................................................................................. 136
Example 10 ............................................................................................................................... 138
Adams-Moulton Method............................................................................................................ 141
Example 11 ............................................................................................................................... 141
Example 12 ............................................................................................................................... 146
Simulink Modeling .................................................................................................................... 151
Example 13 ............................................................................................................................... 151
Example 14 ............................................................................................................................... 153
Example 15 ............................................................................................................................... 156
vii
TABLE OF CONTENTS
Example 16 ............................................................................................................................... 157
Nonzero Starting Initial Conditions ........................................................................................... 158
Example 17 ............................................................................................................................... 159
ODEx Solvers ............................................................................................................................. 162
Example 18 ............................................................................................................................... 163
Example 19 ............................................................................................................................... 165
Example 20 ............................................................................................................................... 166
Example 21 ............................................................................................................................... 169
diff( ) .......................................................................................................................................... 172
Example 22 ............................................................................................................................... 172
Chapter 4: Stiff ODEs ............................................................................................ 177
Example 1 ................................................................................................................................. 177
Example 2 ................................................................................................................................. 179
Example 3 ................................................................................................................................. 180
Example 4 ................................................................................................................................. 183
Jacobian Matrix ........................................................................................................................ 185
Example 5 ................................................................................................................................. 186
Example 6 ................................................................................................................................. 189
Chapter 5: Higher-Order and Coupled ODEs.......................................................... 193
Fourth-Order ODE Problem ....................................................................................................... 193
Robertson Problem ................................................................................................................... 197
Akzo-Nobel Problem ................................................................................................................. 199
HIRES Problem .......................................................................................................................... 206
Reference .................................................................................................................................. 211
Chapter 6: Implicit ODEs ....................................................................................... 213
Example 1 ................................................................................................................................. 214
Example 2 ................................................................................................................................. 218
Example 3 ................................................................................................................................. 220
Example 4 ................................................................................................................................. 223
viii
TABLE OF CONTENTS
Example 5 ................................................................................................................................. 226
Example 6 ................................................................................................................................. 229
References ................................................................................................................................ 232
Chapter 7: Comparative Analysis of ODE Solution Methods ................................. 233
Example 1 ................................................................................................................................. 234
Drill Exercises ........................................................................................................................... 251
Exercise 1 ................................................................................................................................. 251
Exercise 2 ................................................................................................................................. 252
Exercise 3 ................................................................................................................................. 252
Exercise 4 ................................................................................................................................. 253
Exercise 5 ................................................................................................................................. 253
Exercise 6 ................................................................................................................................. 254
Exercise 7 ................................................................................................................................. 255
Exercise 8 ................................................................................................................................. 256
Exercise 9 ................................................................................................................................. 257
Exercise 10 ............................................................................................................................... 258
Exercise 11 ............................................................................................................................... 258
Exercise 12 ............................................................................................................................... 259
Exercise 13 ............................................................................................................................... 259
Part II: Boundary Value Problems in Ordinary Differential Equations ........ 261
Chapter 8: Boundary Value Problems ................................................................... 263
Dirichlet Boundary Condition Problem ...................................................................................... 267
Example 1 ................................................................................................................................. 267
Example 2 ................................................................................................................................. 271
Robin Boundary Condition Problem .......................................................................................... 274
Example 3 ................................................................................................................................. 274
Sturm-Liouville Boundary Value Problem ................................................................................. 279
Example 4 ................................................................................................................................. 280
Stiff Boundary Value Problem ................................................................................................... 285
ix
TABLE OF CONTENTS
Example 5 ................................................................................................................................. 285
References ................................................................................................................................ 289
Drill Exercises ........................................................................................................................... 289
Exercise 1 ................................................................................................................................. 290
Exercise 2 ................................................................................................................................. 290
Exercise 3 ................................................................................................................................. 290
Exercise 4 ................................................................................................................................. 290
Exercise 5 ................................................................................................................................. 290
Exercise 6 ................................................................................................................................. 291
Exercise 7 ................................................................................................................................. 291
Exercise 8 ................................................................................................................................. 291
Exercise 9 ................................................................................................................................. 291
Exercise 10 ............................................................................................................................... 291
Exercise 11 ............................................................................................................................... 292
Exercise 12 ............................................................................................................................... 292
Exercise 13 ............................................................................................................................... 292
Part III: Applications of Ordinary Differential Equations............................. 293
Chapter 9: Spring-Mass-Damper Systems ........................................................... 295
Single Degree of Freedom System............................................................................................ 295
Case 1: Free Vibration (Motion) ................................................................................................. 295
Case 2: Forced Vibration (Motion) ............................................................................................. 309
Two Degrees of Freedom System ............................................................................................. 320
Three Degrees of Freedom System........................................................................................... 328
Matrix Approach for n-Degree of Freedom System .................................................................. 336
References ................................................................................................................................ 343
Chapter 10: Electromechanical and Mechanical Systems.................................... 345
Modeling a DC Motor ................................................................................................................ 345
Modeling a DC Motor with Flexible Load .................................................................................. 350
Modeling a Microphone ............................................................................................................ 356
x
TABLE OF CONTENTS
Modeling Motor: Pump Gear Box............................................................................................... 361
Modeling Double Pendulum................................................................................................. 371
References ................................................................................................................................ 384
Chapter 11: Trajectory Problems .......................................................................... 385
Falling Object ............................................................................................................................ 385
Thrown Ball Trajectories ........................................................................................................... 388
References ................................................................................................................................ 400
Chapter 12: Simulation Problems ......................................................................... 401
Lorenz System .......................................................................................................................... 401
Lotka-Voltera Problem .............................................................................................................. 407
References ................................................................................................................................ 411
Drill Exercises ........................................................................................................................... 412
Exercise 1 ................................................................................................................................. 412
Exercise 2 ................................................................................................................................. 413
Exercise 3 ................................................................................................................................. 413
Exercise 4 ................................................................................................................................. 413
Exercise 5 ................................................................................................................................. 414
Exercise 6 ................................................................................................................................. 414
Exercise 7 ................................................................................................................................. 415
Exercise 8 ................................................................................................................................. 415
Exercise 9 ................................................................................................................................. 416
Exercise 10 ............................................................................................................................... 416
Exercise 11 ............................................................................................................................... 416
Exercise 12 ............................................................................................................................... 417
Exercise 13 ............................................................................................................................... 417
Exercise 14 ............................................................................................................................... 418
Exercise 15 ............................................................................................................................... 419
Exercise 16 ............................................................................................................................... 420
Exercise 17 ............................................................................................................................... 422
Exercise 18 ............................................................................................................................... 423
xi
TABLE OF CONTENTS
Part IV: Partial Differential Equations ......................................................... 427
Chapter 13: Solving Partial Differential Equations ............................................... 429
pdepe() ...................................................................................................................................... 430
One-Dimensional Heat Transfer Problem .................................................................................. 431
Example 1 ................................................................................................................................. 432
Two-Dimensional Heat Transfer: Solving an Elliptic PDE with the Gauss-Seidel Method ......... 436
Example 2 ................................................................................................................................. 438
del2(): Laplace Operator ............................................................................................................ 443
Example 3 ................................................................................................................................. 444
Wave Equation .......................................................................................................................... 447
Solving a One-Dimensional Wave Equation .............................................................................. 448
Example 4 ................................................................................................................................. 451
Solving a Two-Dimensional Wave Equation .............................................................................. 455
Example 5 ................................................................................................................................. 456
References ................................................................................................................................ 459
Drill Exercises ........................................................................................................................... 460
Exercise 1 ................................................................................................................................. 460
Exercise 2 ................................................................................................................................. 460
Exercise 3 ................................................................................................................................. 460
Exercise 4 ................................................................................................................................. 461
Exercise 5 ................................................................................................................................. 461
Exercise 6 ................................................................................................................................. 461
Exercise 7 ................................................................................................................................. 461
Exercise 8 ................................................................................................................................. 461
Exercise 9 ................................................................................................................................. 462
Exercise 10 ............................................................................................................................... 462
Exercise 11 ............................................................................................................................... 462
Exercise 12 ............................................................................................................................... 463
Exercise 13 ............................................................................................................................... 463
Index ..................................................................................................................... 465
xii
About the Author
Dr. Sulaymon L. Eshkabilov is currently a visiting
professor in the Department of Agriculture and Biosystems
Engineering at North Dakota State University. He obtained
his ME diploma from Tashkent Automobile Road Institute
in 1994, his MSc from Rochester Institute of Technology,
USA in 2004, and his PhD from Academy Sciences of
Uzbekistan in 2005. He was an associate professor at
Tashkent Automobile Road Institute from December 2006 to
January 2017. He also held visiting professor and researcher
positions at Ohio University from 2010 to 2011 and Johannes
Kepler University, Austria, from January to September 2017. He has taught the following
courses: “MATLAB/Simulink Applications for Mechanical Engineering and Numerical
Analysis” and “Modeling of Engineering Systems” for undergraduate students, an
“Advanced MATLAB/Mechatronics” seminar/class, and “Control Applications,” “System
Identification,” “Experimentation and Testing with Analog and Digital Devices” for
graduate students.
His research areas are mechanical vibrations, control, mechatronics, system
dynamics, image processing, and microstructure analysis of materials. He is the author
of more than 30 research papers and 5 books. Three of the five books are devoted to
MATLAB/Simulink applications for mechanical engineering students and numerical
analysis. From 2009 to 2020, he was an external academic expert for the European
Commission, assessing academic projects.
xiii
About the Technical Reviewer
Karpur Shukla is a research fellow at the Centre for
Mathematical Modelling at FLAME University in Pune,
India. His current research interests focus on topological
quantum computation, nonequilibrium and finite-
temperature aspects of topological quantum field theories,
and applications of quantum materials effects for reversible
computing. He received an MSc in physics from Carnegie
Mellon University, with a background in theoretical analysis
of materials for spintronics applications as well as Monte Carlo simulations for the
renormalization group of finite-temperature spin lattice systems.
xv
Acknowledgments
I express my special gratitude to the technical reviewers, proofreaders, and editors of
Apress for their very thorough work while reviewing the content and code in this book.
Without their critical insights and corrections at many points of the book, I would not
have been able to complete it with this quality. In addition, I would like to express my
special gratitude to Mark Powers for his on-time and well-planned correspondence
throughout this book project.
My cordial gratitude goes to my mother for her limitless support and love. Up until
the very last point of this book, she was always checking in about my progress.
I would like to thank my wife, Nigora, because without her great support, I would not
have been able to take up the challenging task of writing this book. I have spent many
weekends in my office writing and editing the book content and the MATLAB/Simulink
scripts and models. In addition, I would like to thank our children, Anbara, Durdona,
and Dovud, for being such delightful people and being the inspiration for my efforts
while writing this book.
xvii
Introduction
This book covers the most essential and hands-on tools and functions of the MATLAB
and Simulink packages and the Symbolic Math Toolbox (MuPAD notes) to solve, model,
and simulate ordinary differential equations (ODEs) and partial differential equations
(PDEs). It explains how to solve ODEs and PDEs symbolically and numerically via
interactive examples and case studies. The main principle of the book is “learn by doing,”
moving from the simple to the complex. This book contains dozens of solved problems
and simulation models embedded in MATLAB scripts and Simulink models, which
will help you to master programming and modeling essentials, as well as learn how to
program and model more difficult and complex problems that involve ODEs and PDEs.
Practical MATLAB Modeling with Simulink explains various practical issues
of programming and modeling in parallel by comparing the programming tools of
MATLAB to the modeling tools of Simulink. By studying this book, you’ll be proficient
at using the MATLAB/Simulink packages and at using the source codes and models
from the book’s examples as templates for your own projects to solve modeling and
simulation, or engineering problems with ODEs and PDEs.
What You Will Learn
By the end of the book, you’ll have learned how to do the following:
• Model complex problems using MATLAB and Simulink
• Use MATLAB and Simulink to solve ODEs and PDEs
• Use numerical methods to solve first-, second-, and higher-order and
coupled ODEs
• Solve stiff and implicit ODEs
• Employ numerical methods to solve first- and second-order linear
PDEs
xix
INTRODUCTION
• Solve ODEs symbolically with the Symbolic Math Toolbox of
MATLAB
• Understand the applications and modeling aspects of differential
equations in solving various simulation problems
This book is aimed at engineers, programmers, data scientists, and students
majoring in engineering, applied/industrial math, data science, and scientific
computing. This book continues where Apress’ Beginning MATLAB and Simulink leaves
off.
The book is composed of three parts. Part 1 consists of the following chapters:
• Chapter 1 is dedicated to general formulations and solving ODEs
symbolically using The Symbolic Math Toolbox functions and
MuPAD note commands.
• Chapter 2 covers the most essential programming aspects to solve
first-order ODEs numerically by writing scripts based on the Euler,
Runge-Kutta, Milne, Adams-Bashforth, Ralston, and Adams-Moulton
methods. You’ll write the scripts using MATLAB’s built-in ODE
solvers, such as ode23, ode23t, ode45, ode15s, ode113, odeset, etc.,
and Simulink modeling.
• Chapter 3 is dedicated to solving second-order ODEs symbolically
and numerically using the Euler, Runge-Kutta, Milne, Adams-
Bashforth, Ralston, and Adams-Moulton methods. You’ll do this
using MATLAB’s built-in ODE solvers, such as ode23, ode23t, ode45,
ode15s, ode113, odeset, etc., and Simulink modeling.
• Chapter 4 addresses the issues of how to solve stiff ODEs by adjusting
a step size, specifying a solver setting, selecting an appropriate solver
type, and so forth.
• Chapter 5 is dedicated to solving higher-order and coupled ODEs.
• Chapter 6 is devoted to solving implicitly defined IVPs and
differential algebraic equations using MATLAB’s ode15i and Simulink
modeling.
xx
INTRODUCTION
• Chapter 7 is dedicated to a comparative analysis of solving ODEs
with MATLAB’s ODE solvers, Simulink modeling, script writing, and
computing analytical solutions with the Symbolic MATH Toolbox
(MuPAD notes).
Part 2 consists of the following chapter:
• Chapter 8 covers most essential tools of solving boundary value
problems of ODEs numerically by using MATLAB solvers such as
bvp4c, bvp5c, bvpinit, deval, etc.
Part 3 consists of the following chapters:
• Chapter 9 is devoted to applications of ODEs, specifically, modeling
and simulations of spring-mass-damper systems.
• Chapter 10 is devoted to applications of ODEs, specifically, modeling
and simulations of mechanical and electromechanical systems.
• Chapter 11 is devoted to applications of ODEs, specifically, modeling
and simulations of trajectory problems.
• Chapter 12 is devoted to applications of ODEs, specifically, modeling
and simulations of several simulation problems, such as the Lotka-
Voltera and Lorenz systems.
Part 4 consists of the following chapter:
• Chapter 13 is devoted to solving partial differential equations using
PDE toolbox functions, such as pdepe() and the Laplacian operator
del2(), and writing scripts based on Gauss-Seidel and finite
difference methods.
How to Access the Source Code
All of the source code (such as M/MN files, Simulink models, and SLX/MDL files)
discussed in the book is available to readers via the Download Source Code button at
[Link]/9781484257982.
xxi
INTRODUCTION
A Note to Users
The given scripts in the context of the book may not be the best solutions to the given
problems, which was done intentionally in some cases to emphasize methods used to
improve them; in other cases, the given scripts are the most appropriate solutions to the
best knowledge of the author. Should I spot any better alternative solutions to exercises
given in the context of the book, I intend to publish them via the MathWorks MATLAB
Central User Community’s file exchange via my file exchange link there—under my
name.
No matter how hard we have worked to proofread the content of the book, it is
inevitable that there might be some typographical errors that have slipped through and
will appear in print. My apologies.
Sulaymon L. Eshkabilov
January 2020
xxii