Introduction To Modeling and Simulation With Matlab and Python - Compress
Introduction To Modeling and Simulation With Matlab and Python - Compress
Horst Simon
Deputy Director
Lawrence Berkeley National Laboratory
Berkeley, California, U.S.A.
PUBLISHED TITLES
DATA-INTENSIVE SCIENCE
Edited by Terence Critchlow and Kerstin Kleese van Dam
INTRODUCTION TO SCHEDULING
Yves Robert and Frédéric Vivien
Steven I. Gordon
Brian Guilfoos
MATLAB® and Simulink® are trademarks of the MathWorks, Inc. and are used with permission. The
MathWorks does not warrant the accuracy of the text or exercises in this book. This book’s use or
discussion of MATLAB® and Simulink® software or related products does not constitute endorsement
or sponsorship by the MathWorks of a particular pedagogical approach or particular use of the
MATLAB® and Simulink® software.
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
This book contains information obtained from authentic and highly regarded sources. Reasonable
efforts have been made to publish reliable data and information, but the author and publisher cannot
assume responsibility for the validity of all materials or the consequences of their use. The authors and
publishers have attempted to trace the copyright holders of all material reproduced in this publication
and apologize to copyright holders if permission to publish in this form has not been obtained. If any
copyright material has not been acknowledged please write and let us know so we may rectify in any
future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced,
transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or
hereafter invented, including photocopying, microfilming, and recording, or in any information storage
or retrieval system, without written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access
www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc.
(CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization
that provides licenses and registration for a variety of users. For organizations that have been granted a
photocopy license by the CCC, a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are
used only for identification and explanation without intent to infringe.
Preface
Authors
CHAPTER 5 ■ Plotting
5.1 PLOTTING IN MATLAB®
5.2 PLOTTING IN PYTHON
EXERCISES
CHAPTER 12 ■ Functions
12.1 MATLAB® FUNCTIONS
12.2 PYTHON FUNCTIONS
12.2.1 Functions Syntax in Python
12.2.2 Python Modules
EXERCISES
INDEX
Preface
Sources: Bell, G., Supercomputers: The amazing race (a history of supercomputing, 1960–2020),
2015, http://research.microsoft.com/en-us/um/people/gbell/MSR-TR-2015-
2_Supercomputers-The_Amazing_Race_Bell.pdf (accessed December 15, 2016).
Bell, T., Supercomputer timeline, 2016, https://mason.gmu.edu/~tbell5/page2.html (accessed
December 15, 2016).
Esterbrook, S., Timeline of climate modeling, 2015, https://prezi.com/pakaaiek3nol/timeline-
of-climate-modeling/ (accessed December 15, 2016).
Sources: Bartlett, B.N., The contributions of J.H. Wilkinson to numerical analysis. In S.G. Nash, (Ed.),
A History of Scientific Computing, ACM Press, New York, pp. 17–30, 1990.
Computer History Museum, Timeline of computer history, software and languages, 2017,
http://www.computerhistory.org/timeline/software-languages/ (accessed January 2, 2017).
Dorzolamide, 2016, https://en.wikipedia.org/wiki/Dorzolamide (accessed December 15,
2016).
Jameson, A., Computational fluid dynamics, past, present, and future, 2016, http://aero-
comlab.stanford.edu/Papers/NASA_Presentation_20121030.pdf (accessed December 15,
2016).
Prat-Resina, X., A brief history of theoretical chemistry, 2016,
https://sites.google.com/a/r.umn.edu/prat-resina/divertimenti/a-brief-history-of-theoretical-
chemistry (accessed December 15, 2016).
Vassberg, J.C., A brief history of FLO22, http://dept.ku.edu/~cfdku/JRV/Vassberg.pdf
(accessed December 15, 2016).
Tracing weather and climate modeling from von Neumann’s first model on
ENIAC, we can see that the computational power has allowed scientists to
make rapid progress in the representation of weather and climate. In 1959,
Lorenz laid the foundation for the mathematics behind weather events. By
1965, further advances in computing power and scientific knowledge
provided the basis for the first global climate models. These have grown in
scope to the present day to earth system models that couple atmospheric and
ocean circulation that provide for the basis for the climate change forecasts of
the international community.
Table 1.2 documents similar developments in computational chemistry,
aeronautics and structures, and selected achievements in software and
algorithms. The scientific advances were made possible not only by
improvements in the hardware but also by the invention of programming
languages, compilers, and the algorithms that are used to make the
mathematical calculations underlying the models. As with weather modeling,
one can trace the advancement of computational chemistry from the first
simulation of molecules to the screening of drugs by modeling their binding
to biomolecules. In aeronautics, the simulation of airflow over a wing in two
dimensions has advanced to the three-dimensional simulation of a full
airplane to create a final design. Similar timelines could be developed for
every field of science and engineering from various aspects of physics and
astronomy to earth and environmental science, to every aspect of engineering,
and to economics and sociological modeling.
For those just getting introduced to these concepts, the terminology is
daunting. The lesson at this point is to understand that computation has
become an essential part of the design and discovery process across a wide
range of scientific fields. Thus, it is essential that everyone understands the
basic principles used in modeling and simulation, the mathematics underlying
modeling efforts, and the tools of modeling along with their pitfalls.
Modeling and simulation has also become a key part of the process and
designing, testing, and producing products and services. Where the building
of physical prototypes or the completion of laboratory experiments may take
weeks or months and cost millions of dollars, industry is instead creating
virtual experiments that can be completed in a short time at greatly reduced
costs. Proctor and Gamble uses computer modeling to improve many of its
products. One example is the use of molecular modeling to test the
interactions among surfactants in their cleaning products with a goal of
producing products that are environmentally friendly and continue to perform
as desired (Council on Competiveness, 2009).
Automobile manufacturers have substituted modeling for the building of
physical prototypes of their cars to save time and money. The building of
physical prototypes called mules is expensive, costing approximately
$500,000 for each vehicle with 60 prototypes required before going into
production (Mayne, 2005). The design of the 2005 Toyota Avalon required
no mules at all—using computer modeling to design and test the car.
Similarly, all of the automobile manufacturers are using modeling to reduce
costs and get new products to market faster (Mayne, 2005).
These examples should illustrate the benefits of using modeling and
simulation as part of the research, development, and design processes for
scientists and engineers. Of course, students new to modeling and simulation
cannot be expected to effectively use complex, large-scale simulation models
on supercomputers at the outset of their modeling efforts. They must first
understand the basic principles for creating, testing, and using models as well
as some of the approaches to approximating physical reality in computer
code. We begin to define those principles in Section 1.3 and continue through
subsequent chapters.
FIGURE 1.3 Partial concept map of model to calculate travel time using Cmap.
FIGURE 1.4 Partial mind map of model to calculate travel time using mind map maker.
The average speed across a road segment is slowed by parked cars and
traffic control devices while wider lanes and higher speed limits take less
time. The total time for a trip would need to add the average times associated
with traversing each road segment. Thus, data on each segment will be
needed as input to the model. Simple versions of such estimates are provided
by global positioning satellite (GPS) equipment or the Internet mapping
services that are available online. There are many other conditions that would
impact this system. Modeling traffic conditions are a topic of one of the
exercises at the end of the chapter.
Going back to Figure 1.2, one must choose which simplifying assumptions
can be made in a model. This, in turn, leads to a selection of the data that
would be needed, the variables that will drive the model, and the equations
and mathematical functions that will comprise the model.
Once these items have been defined, a computer version of the model can
be created and tested. The results must be verified to ascertain that the code is
working properly. If the model is giving unexpected results with the code
working properly, there may be a need to reexamine the simplifying
assumptions and to reformulate the model. Thus, one may go through several
iterations until the model is providing sufficiently accurate results. This can
be validated against available experimental or field data to provide a
quantitative assessment of model accuracy. Finally, the model can be used to
undertake more detailed analysis and the results reported. As time goes on,
the model must be maintained and may be improved by relaxing more of the
assumptions and/or improving the input data. It should be noted that the
judgment of whether a model is giving reasonable results is sometimes as
much an art as a science. Confidence in that judgment is a function of the
experience of the modeler and the breadth and depth of the previous research
about the system under study. Of course the best validation of modeling
results comes from comparisons with real data gathered from observations or
experiments.
REFERENCES
Bartlett, B. N. 1990. The contributions of J.H. Wilkinson to numerical analysis. In A History of
Scientific Computing, ed. S. G. Nash, pp. 17–30. New York: ACM Press.
Bell, G. 2015. Supercomputers: The amazing race. (A History of Supercomputing, 1960–2020).
http://research.microsoft.com/en-us/um/people/gbell/MSR-TR-2015-2_Supercomputers-
The_Amazing_Race_Bell.pdf (accessed December 15, 2016).
Bell, T. 2016. Supercomputer timeline, 2016. https://mason.gmu.edu/~tbell5/page2.html (accessed
December 15, 2016).
Biesiada, J., A. Porollo, and J. Meller. 2012. On setting up and assessing docking simulations for
virtual screening. In Rational Drug Design: Methods and Protocols, Methods in Molecular
Biology, ed. Yi Zheng, pp. 1–16. New York: Springer Science and Business Media.
Cmap website. http://cmap.ihmc.us/ (accessed February 22, 2016).
Computer History Museum. 2017. Timeline of computer history, software and languages.
http://www.computerhistory.org/timeline/software-languages/ (accessed January 2, 2017).
Council on Competitiveness. 2004. First Annual High Performance Computing Users Conference.
http://www.compete.org/storage/images/uploads/File/PDF%20Files/2004%20HPC%2004%20Users%20Conferenc
Council on Competitiveness. 2009. Procter & gamble’s story of suds, soaps, simulations and
supercomputers. http://www.compete.org/publications/all/1279 (accessed January 2, 2017).
Dorzolamide. 2016. https://en.wikipedia.org/wiki/Dorzolamide (accessed December 15, 2016).
Esterbrook, S. 2015. Timeline of climate modeling. https://prezi.com/pakaaiek3nol/timeline-of-climate-
modeling/ (accessed December 15, 2016).
Fatherree, B. H. 2006. U.S. Army corps of engineers, Chapter 5 hydraulics research giant, 1949–1963,
Part I: River modeling, potamology, and hydraulic structures. In The First 75 Years: History of
Hydraulics Engineering at the Waterways Experiment Station,
http://chl.erdc.usace.army.mil/Media/8/5/5/Chap5.htm. Vicksburg, MS: U.S. Army Engineer
Research and Development Center (accessed October 15, 2016).
Goldstine, H. 1990. Remembrance of things past. In A History of Scientific Computing, ed. S. G. Nash,
pp. 5–16. New York: ACM Press.
Gordon, S. I., K. Carey, and I. Vakalis. 2008. A shared, interinstitutional undergraduate minor program
in computational science. Computing in Science and Engineering, 10(5): 12–16.
HPC University website. http://hpcuniversity.org/educators/competencies/ (accessed January 15, 2016).
International Panel on Climate Change. 2013. Climate change 2013—The physical science basis
contribution of working Group I to the fifth assessment report of the IPCC, New York: Cambridge
University Press. http://www.climatechange2013.org/images/report/WG1AR5_ALL_FINAL.pdf
(accessed December 15, 2016).
Jameson, A. 2016. Computational fluid dynamics, past, present, and future. http://aero-
comlab.stanford.edu/Papers/NASA_Presentation_20121030.pdf (accessed December 15, 2016).
Joseph, E., A. Snell, and C. Willard. 2004. Study of U.S. industrial HPC users. Council on
Competitiveness. http://www.compete.org/publications/all/394 (accessed December 15, 2016).
Lax, P. D. 1982. Report of the panel on large scale computing in science and engineering. Report
prepared under the sponsorship of the Department of Defense and the National Science
Foundation. Washington, D.C.: National Science Foundation.
Lynch, P. and O. Lynch. 2008. Forecasts by PHONIAC. Weather, 63(11): 324–326.
Mayne, E. Automakers trade mules for computers, Detroit News, January 30, 2005,
http://www.jamaicans.com/forums/showthread.php?1877-Automakers-Trade-Mules-For-
Computers (accessed January 25, 2016).
McCartney, S. 1999. ENIAC. New York: Walker and Company.
Mindmapmaker website. http://mindmapmaker.org/ (accessed February 22, 2016).
National Science Foundation. 2006. Simulation-based engineering science: Report of the NSF blue
ribbon panel on simulation-based engineering science.
http://www.nsf.gov/pubs/reports/sbes_final_report.pdf.
Nick, T. 2014. A modern smartphone or a vintage supercomputer: Which is more powerful?
http://www.phonearena.com/news/A-modern-smartphone-or-a-vintage-supercomputer-which-is-
more-powerful_id57149 (accessed January 15, 2016).
Pachauri, R. K. and L. A. Meyer (ed.). 2014. Climate change 2014: Synthesis report. Contribution of
working groups I, II and III to the fifth assessment report of the intergovernmental panel on
climate change. http://www.ipcc.ch/report/ar5/syr/.
Prat-Resina, X. 2016. A brief history of theoretical chemistry.
https://sites.google.com/a/r.umn.edu/prat-resina/divertimenti/a-brief-history-of-theoretical-
chemistry (accessed December 15, 2016).
Reed, D. 2005. Computational science: America’s competitive challenge. President’s information
technology advisory committee subcommittee on computational science.
http://www.itrd.gov/pitac/meetings/2005/20050414/20050414_reed.pdf.
Society of Industrial and Applied Mathematics (SIAM). 2001. Graduate education in computational
science and engineering. SIAM Review, 43(1): 163–177.
Vassberg, J. C. A brief history of FLO22. http://dept.ku.edu/~cfdku/JRV/Vassberg.pdf (accessed
December 15, 2016).
Vogelsberger, M., S. Genel, V. Springel, et al. 2014. Properties of galaxies reproduced by a
hydrodynamic simulation. Nature, 509(8): 177–182. doi:10.1038 /nature13316.
Yasar, O. and R. H. Landau. 2001. Elements of computational science and engineering education.
SIAM Review, 45(4): 787–805.
CHAPTER 2
The large pane in the middle is the Command Window. This pane provides
an interpreter and allows you to type MATLAB commands and see the
results immediately.
The leftmost pane displays the current working directory’s contents. You
can browse through your directory tree using the widget directly above the
three main panes. The current working directory is the first location where
MATLAB will look for files when attempting to execute, open, or close files.
There should be a helloworld.m file; if you double-click it the Editor will
open, showing you the contents of the file and making it possible to edit and
save the updated file. The Editor provides some rich tools, including syntax
coloring, debugging, and more. When the Editor is open, a context-
appropriate menu ribbon appears at the top, which includes debugging
controls. We will return to those later in this chapter.
The pane to the far right is the Workspace, which displays the variables
currently being used by MATLAB, and their value. Double-clicking on a
variable in this list will open the Variables pane, which allows fuller
inspection and editing of variables, including large matrices. Returning to the
menu ribbon at the top of the window, you should notice Import Data and
Save Workspace, which allow you to quickly import and export datasets to
and from your Workspace.
x = 2
five = 5
z = 3.14159
my_string = 'Hello World!'
Type these commands into the console. Once a variable is declared, it can be
recalled and used in an appropriate calculation. As an example enter this:
y=x*five;
You will note that we included a semicolon, which suppressed the output of
the command, unlike the previous examples. To see the result, type:
y
Now that you have some variable in memory, you can look at the Workspace
to see the list of variables and their current values.
Variables have something called scope, which defines where they are
visible. For example, variables defined in the Command Window are global;
they can be referenced, used, and modified from any other piece of code. For
example, if we define “x” in the Command Window, and then in my_script.m
if we add the command “x”, when we run that program, it will print the value
of “x”.
However, variables defined within a function are only visible within that
function. These are called local variables. We will come back to this idea
later when you begin to create full programs.
You can delete variables in two ways. The first is the clear command:
clear x
2.1.2.2 Keywords
MATLAB reserves certain words, called keywords, which cannot be used as
variable names. Note that these words will be colored in blue when they are
typed in the Editor or Command Window. They are listed in Table 2.3.
traffic=[200,150,350,235,450];
nox = zeros(1,365);
If you type in the first example, you will see that a traffic variable is created
with five items with the values indicated. In the second example (our first use
of a function), an array is created with 365 items initially set to a value of 0.
This could be, for example, space to hold the average daily nitrogen oxide
content in the air. Notice how they are represented in the Workspace.
We can use the index to operate on each of the items in the list in turn or
can operate on any individual item by using its index number. To see a single
value, we use the variable name with the index in parentheses. Try this and
see what happens:
traffic(2)
You should get the value 150. This is because MATLAB starts all lists and
arrays with the index number 1. So if you put traffic(1) in the console, you
should get 200. What happens if you put this in:
traffic
x=[1, 2, 3, 4];
y=[1, 2; 3, 4];
x=zeros(6)
y=ones(8)
You can also specify a custom step size, instead of being restricted to “1” as
the default, by adding a third parameter, located between the start and stop
points. For example, 3:.2:4 will return an array containing [3 3.2 3.4 3.6 3.8
4].
Another useful function for creating vectors containing regularly spaced
values is “linspace()”. Rather than specifying the size of the step, you specify
the number of elements you want in your array. For example, linspace(3,4,6)
will return an array containing [3. 3.2 3.4 3.6 3.8 4].
myarray=linspace(3,4,6)
matlab –r myprogram.m
We can also run these programs inside of the Command Window or via the
MATLAB graphical user interface, which provides some additional
debugging capabilities.
2.1.6 Debugging
MATLAB includes some debugging tools, which can be very useful in
discovering problems in your code.
If you set a breakpoint in your code, simply running the code will trigger
the debugger when the breakpoint is reached. Breakpoints allow you to target
a specific point in the code that you wish to stop and investigate. When the
debugger reaches a breakpoint, you can either run commands in the
Command Window to investigate the state of your program, or you can use
the Workspace and Variables panes to look at your data. You can set
breakpoints by clicking on the “-” in the gray space to the left of the line you
wish to stop execution at in the Editor.
The most common bugs you are likely to encounter when working on
modeling and simulation problems will be when your data do not contain the
values you expected when executing a certain block of code, and MATLAB’s
debugger is a useful tool for discovering this.
Once you wish to continue execution, you can Step through the program
one line at a time, Step In to the function of the current line, Step Out and run
until the current function ends, Continue until the debugger hits the next
breakpoint, or Quit Debugging. You will need to exit the debugger to return
the console to the normal mode. The exercises include an example where you
can try out the debugger.
The upper right region of the window shows the Help pane. It will
automatically show documentation for a function or object being instantiated
in the editor or console, or you can type a function name in the Object field at
the top of the window to pull up the corresponding documentation.
In the same pane as the Help is the Variable Explorer. Clicking on the
Variable Explorer button at the bottom of the pane will switch the view to the
Variable Explorer. This gives you a view into the global variables for the
current console. It will be empty until you enter commands to create or
modify variables in the console or run a script in the editor. Most common
variable types can be displayed, and variables can be edited in this interface.
You can do quick plots of arrays and show matrices as images.
You can also save sets of variables in a Spyder-specific format, MATLAB
data format, or HDF5 format by using the Disk icon to the right of the pane.
In addition, you can import data from a large variety of supported formats
using the Import Data icon (downward pointing arrow icon).
The left pane shows the Spyder editor. It enables editing of Python (and
other languages!) and includes features such as syntax coloring,
function/class/method browsing, code analysis, introspection, and more. At
the top of the Spyder window are debugging controls that allow you to
execute the Editor pane’s current file in the IPython console; we will cover
those in more detail later.
x = 2
five = 5
z = 3.14159
my_string = "Hello World!"
Type these commands into the console. Once a variable is declared, it can be
recalled and used in an appropriate calculation. As an example, enter this:
y=x*five
Now that you have some in memory, open the variable explorer to see the list
of variables and their current values.
Variables have something called scope, which defines where they are
visible. For example, variables defined in the console are global; they can be
referenced, used, and modified from any other piece of code. For example, if
we define “x” in the console, and then in temp.py if we add the command
“print(x)”, when we run that program, it will print the value of “x”.
However, variables defined within a code block (such as a function or an
indented section of code) are only visible within that code block, and any
dependent blocks (subblocks). These are called local variables. We will come
back to this idea later when you begin to create full programs.
You can delete variables in two ways. The first is the del command:
del x
2.2.3.2 Keywords
Python reserves certain words, called keywords, which cannot be used as
variable names. Note that Spyder will color these words in magenta when
they are typed in the Editor or console. They are listed in Table 2.7.
traffic=[200,150,350,235,450]
nox = [0.]*365
If you type in the first example, you will see that a traffic variable is created
with five items with the values indicated. In the second example, a list is
created with 365 items initially set to a value of 0. This could be, for
example, space to hold the average daily nitrogen oxide content in the air.
Notice how they are represented in the Variable Explorer.
We can use the index to operate on each of the items in the list in turn or
can operate on any individual item by using its index number. To see a single
value, we use the variable name with the index in brackets. Try this and see
what happens:
traffic[1]
You should get the value 150. This is because Python starts all lists and
arrays with the index number 0. So if you put traffic[0] in the console, you
should get 200. What happens if you put this in:
traffic
For the rest of this textbook, when using functions or objects provided by
NumPy, we will assume that it has been imported as np, which is consistent
with the documentation for NumPy. Please note that renaming a package
upon import does not rename the package in the Help.
The basic interface for creating vectors and matrices is NumPy’s “array()”
function, and it can accept a list object as input.
x=np.array([1, 2, 3, 4])
y=np.array([[1, 2], [3, 4]])
We can access (and modify) individual elements in arrays in Python. Use the
linspace() example above to create the array “myarray”. We can look at the
second element of myarray by typing “myarray[1]” in the console.
Remember that previously we explained that Python is zero-indexed,
meaning myspace[0] refers to the first element, and myspace[1] refers to the
second. We can change the value of the second element in “myarray” simply
by using it on the left side of an assignment expression.
myarray[1] = 2
This will turn “myarray” into [3., 2., 3.4, 3.6, 3.8, 4.].
We can also extract portions of arrays called slices. For example, we could
create a slice of “myarray” containing the second and third elements with the
command “myarray[1:3]”. The argument is in the form “start:stop:step”, and
it does not include the stop element. The “step” argument is option (and
assumed to be 1 unless specified), whereas start and stop, if not specified, are
assumed to be the beginning and end of the array. “myarray[:3]” will return
[3., 2., 3.4], whereas “myarray[3:]” will return [3.6, 3.8, 4.]. “myarray[::2]”
will select every other element in the array, starting with position zero: [3.,
3.4, 3.8].
Now, we can execute functions from the math module. For example, we can
find the square root of a value:
math.sqrt(9)
We can also run these programs inside of the Spyder environment, which
provides some additional debugging capabilities.
2.2.8 Debugging
One advantage to Spyder over testing code directly in the interpreter as
described above is that Spyder includes some debugging tools, which can be
very useful in discovering problems in your code.
To launch the debugger, click on the “Debug File” button, which shows a
play and pause button next to each other. (Hovering your mouse over any
button in the Spyder toolbar will display a tip in the lower left of the window
explaining what the button does.) Breakpoints allow you to target a specific
point in the code that you wish to stop and investigate. When the debugger
reaches a breakpoint, you can either run commands in the console to
investigate the state of your program, or you can use the Variable Explorer to
look at your data. You can set breakpoints by double-clicking in the gray
space to the left of the line you wish to stop execution at; the debugger will
run to that line by default or will stop at the very first line otherwise.
The most common bugs you are likely to encounter when working on
modeling and simulation problems will be when your data does not contain
the values you expected when executing a certain block of code, and
Spyder’s debugger is a useful tool for discovering this.
Once you wish to continue execution, you can (examining the debugging
buttons from left to right) step through the program one line at a time, step
into the function of the current line, run until the current function ends, run
until the debugger hits the next breakpoint, or exit. You will need to exit the
debugger to return the console to the normal mode. The exercises include an
example where you can try out the debugger.
EXERCISES
1. Basic Arithmetic
Convert the following equations into valid MATLAB or Python
commands, and submit both the input and output of the command from
the interpreter.
a.
b.
c.
c.
d.
For the following problems, create the matrices as described, and
then edit as directed, and submit the editing command and the output
from the interpreter.
e. Create the array , and replace the third element with
the number 9.
f. Create the array , and replace the second element
with the value in that location cubed. That is, cube the value found in
that location and store it back in the same location.
The first pallet contains 40 boxes, leaving 3564 (99 pallets × 36 boxes) left
to load. The second and all subsequent loads contain 36 boxes, for a
difference in the remaining load of 36 fewer boxes each time. The pattern of
change associated with the loading of the truck can be characterized by
looking at the amount of change in the boxes to be loaded over time. After
the initial pallet is loaded, this can be observed to be a constant making this
relationship linear. The exact equation for this model is
(3.1)
where:
b is the number of boxes on the truck
k is the number of trips made by the forklift
The accumulated time for any particular trip will be 2k since it takes an
average of two minutes for each trip. We could use this linear equation to
create a simple, linear model of the loading process that would track the
number of loaded boxes over time. We can also use the relationships to
determine how long it will take to load the truck. How long will it take?
The general linear equation is written as follows:
(3.2)
FIGURE 3.1 Linear equation graph showing boxes loaded over time.
where:
F is the spring force (N)
k is the spring constant (N/mm or N/cm)
x is the deformation of the spring
where:
R is the resistance
ρ is the resistivity
l is the length of the sample
a is the cross-sectional area of the sample
(3.5)
Linear models can offer a good approximation of certain phenomena but need
to be used with special caution when making forecasts. All phenomena reach
limits as you approach the extremes of their distributions—whether at the low
end (zero or negative values for those that can go negative) or at the high end
(at values that exceed normal behavior or result in nonlinear behavior).
Extrapolating beyond the available data used to create or validate a model is
prone to major errors, whether the phenomenon we are modeling is linear or
nonlinear in nature. For systems which we approximated with a linear
representation, extrapolation may be doubly dangerous as there are very few
circumstances where something will increase without reaching some limits
that cause the system behavior to change.
For example, if we think about the truck loading example, we could
forecast adding additional forklifts to speed the process. However, at some
point, the average amount of time it takes for a forklift to complete a cycle of
pickup and delivery will slow as additional forklifts will need to wait in line
to get into or out of the truck or avoid each other inside the warehouse. If we
did not think about this, we could easily add too many forklifts and forecast a
directly proportional, linear reduction in loading times. Since forklifts and
their operators are expensive, we could potentially raise costs without the
requisite benefits of efficiency that we might desire.
EXERCISES
1. Examine the three datasets shown in Table 3.3. Which of these are
linear relationships?
2. A new fast food chain has studied its success in its initial metropolitan
market. They found that their net income from the opening and
operation of each site could be represented by this linear equation:
(3.8)
where I represents the net revenue in dollars over one year, b represents
the number of branches, and the constant represents the upfront costs of
creating a new location. They have tested this relationship in their
current market where they have 10 branches. If they are going to open a
new market in a different metropolitan region, what would their net
income be if they opened 4 branches, 8 branches, and 12 branches? Will
this model work for all of those situations? What are the uncertainties to
applying this model in this way?
3. We have implemented a model of the time it will take to get from our
home to work comparing two major potential routes: one using local
and major streets, and one that makes part of the trip on an Interstate
highway. Use the materials on the book website to download the
instructions and the partially completed code in either MATLAB or
Python. Complete the code and make the model runs required to answer
the questions about this model.
4. Water towers are used to store water and to release the treated water
under sufficient pressure to reach the buildings in its service area and
maintain pressure to keep the water from becoming contaminated.
Using the materials on the book website to download the instructions
and the partially completed code that predicts how tall the water tower
needs to be to achieve the minimum necessary pressure. Complete the
code in either MATLAB or Python and make the runs necessary to
answer the questions about this model.
REFERENCES
Chapra, S. C. 2008. Applied Numerical Methods with MALAB for Engineers and Scientists, 2nd ed.
Boston, MA: McGraw-Hill.
Landau, R. H., J. P. Manuel, and C. C. Bordeainu. 2015. Computational Physics Problem Solving with
Python, 3rd ed. Weinheim, Germany: Wiley.
Moaveni, S. 2014. Engineering Fundamentals, 5th ed. Boston, MA: Cengage Learning.
CHAPTER 4
Similarly, matrix addition and subtraction are also done element by element.
However, for the calculations to work correctly, the matrices must have the
same dimensions. Equations 4.2 through 4.4 illustrate the results of matrix
addition and subtraction:
(4.2)
(4.3)
(4.4)
where:
[A] is of the size m × n
[B] is of the size n × p
[C] is of the size m × p
Then
(4.6)
where i, j, and k are the row and column indices for the input and output
matrices.
This is illustrated by Equations 4.7 through 4.9:
(4.7)
(4.8)
(4.9)
There are also several types of special matrices that are built in to both
programming environments. One of these is the identity matrix. The identity
matrix is a matrix that has ones in the diagonal and zeros everywhere else.
The identity matrix is important in understanding the concept of the inverse
of a matrix. When the inverse of a matrix is multiplied by the original matrix,
that product is the identity matrix. In matrix notation, the inverse of a matrix
[A] is often represented as [A′]. Thus:
(4.10)
where I is the identity matrix. Only square matrices have an inverse.
There are several applications of these concepts but a full review is beyond
the scope of this section. However, for those with experience with linear and
matrix algebra, we provide an example exercise at the end of the chapter. The
remainder of the chapter reviews the matrix syntax of MATLAB and Python.
use the syntax np.array([[1], [2], [3]]). To create a matrix, such as the one in
Equation 4.1, you can combine the syntax and use np.array([[1,2,3], [4,5,6]])
to create
EXERCISES
Given matrices
, , , and ,
1.
2.
3.
4.
5.
6.
7.
8. For those with a background in linear algebra, download the file
simultaneous.pdf from the book website and follow the instructions for
MATLAB or Python solution of simultaneous linear equations.
CHAPTER 5
Plotting
year=[1988:1:1994];
sales=[127 130 136 145 158 178 211];
plot(year, sales, '--r*')
Plotting functions this way can be very sensitive to the number of points in
the “x” vector. For example, if we change the distance between points on the
range of −2 to 4 from 0.01 to 0.3, the plot looks like Figure 5.5. You should
take care to ensure the plot you get accurately represents the function you
wish to represent.
If you recall Figure 5.1, you should have noticed that we can put multiple
graphs on the same plot. The first mechanism for doing so is providing
multiple sets of data points to a single plot command. For example, to plot y
versus x, v versus u, and h versus t on the same plot, you can issue this plot
command:
plot(x, y, u, v, t, h)
By default, MATLAB will create each curve with a different color. You may
also control the lines by providing line specifiers to each pair. For example:
plot(x, y,'-b', u, v,'—r', t, h,'g:')
To demonstrate this, let us plot a function, its first and second derivatives (as
seen in Equations 5.2, 5.3, and 5.4), from −2 to 4 inclusive:
(5.2)
(5.3)
(5.4)
x=[-2:0.01:4];
y=3*x.^3 - 26*x+6;
yd=9*x.^2 - 26;
ydd=18*x;
plot(x, y,'-b', x, yd,'--r', x, ydd,':k')
An alternative method for drawing multiple curves on a single plot is the hold
command. You can issue a hold on to instruct MATLAB to keep the current
plot and all axis properties, and to draw all subsequent plot commands in the
existing plot window. Issuing a hold off command will return MATLAB to
the default mode where plot commands erase the previous plots and reset all
axis properties before drawing new plots.
FIGURE 5.6 Multiple curves in a single plot.
x=[10:0.1:22];
y=95000./x.^2;
x_data=[10:2:22];
y_data=[950 640 460 340 250 180 140];
plot(x, y,'-')
hold on
plot(x_data, y_data,'ro--')
hold off
This code generates Figure 5.7. A good start but a truly readable plot requires
us to add axis labels, a title, legend, and potentially manipulate the axis
limits. Let us learn how to do that now.
Once you have a plot, MATLAB provides multiple mechanisms for
manipulating various plot properties. We are going to focus on using
scriptable commands (typed in the Command Window) as knowing those
allows you to write programs that can generate complete plots; however, you
can do everything we mention interactively via the menus in the GUI if you
prefer.
To set a title, use the title function. It takes a single string as an argument.
xlabel and ylabel functions also take a single string as an argument, and set
the labels on the x and y axes, respectively. The axis function allows you to
set the minimum and maximum limits of the x-axis and y-axis by providing a
four element vector of the format To add a
legend, you use the legend function, providing a string argument with the
name of each curve, in the order the curves were added to the plot. Executing
the following code will turn Figure 5.7 into Figure 5.1:
xlabel('DISTANCE (cm)')
ylabel('INTENSITY (lux)')
title('Light Intensity as a Function of Distance')
axis([8 24 0 1200])
legend('Theory','Experiment')
Both vectors must have the same number of elements. The plot command
creates a single curve with the x values on the abscissa (horizontal axis) and
the y values on the ordinate (vertical axis). The curve is made from segments
of lines that connect the points that are defined by the x and y coordinates of
the elements in the two vectors. The more elements there are to plot, the
smoother the plot should look.
Given data in two vectors x and y, we can generate the plot in Figure 5.10
with the following code:
x=np.array([1,2,3,5,7,7.5,8,10])
y=np.array([2,6.5,7,7,5.5,4,6,8])
plt.plot(x, y)
For additional line markers and colors, please refer to the online
documentation for matplotlib.
Let us plot the data in the following table to explore line specifiers:
If you recall Figure 5.9, you should have noticed that we can put multiple
graphs on the same plot. The first mechanism for doing so is providing
multiple sets of data points to a single plot command. For example, to plot y
versus x, v versus u, and h versus t on the same plot, you can issue this plot
command:
plt.plot(x, y, u, v, t, h)
By default, matplotlib will create each curve with a different color. You may
also control the lines by providing line specifiers to each pair. For example:
plot(x, y,'-b', u, v,'—r', t, h,'g:')
To demonstrate this, let us plot a function, its first and second derivatives (as
seen in Equations 5.6 through 5.8), from −2 to 4 inclusive:
(5.6)
(5.7)
(5.8)
x=np.arange(-2,4.001,.01)
y=3*x**3 - 26*x+6
yd=9*x**2 - 26
ydd=18*x
plt.plot(x, y,'-b', x, yd,'--r', x, ydd,':k')
x=np.arange(10,22.01,0.1)
y=95000/x**2;
x_data=np.arange(10,22.01,2)
y_data=np.array([950,640,460,340,250,180,140])
plt.plot(x, y,'-', label='Theory')
plt.plot(x_data, y_data,'ro--', label='Experiment')
This code generates Figure 5.15. A good start but a truly readable plot
requires us to add axis labels, a title, legend, and potentially manipulate the
axis limits. Let us learn how to do that now.
To set a title, use the title function. It takes a single string as an argument.
xlabel and ylabel functions also take a single string as an argument, and set
the labels on the x and y axes, respectively. The axis function allows you to
set the minimum and maximum limits of the x-axis and y-axis by providing a
four-element vector of the format To add a
legend, you use the legend function, which will use the labels you provided
when generating the plot(s) earlier. Executing the following code will turn
Figure 5.15 into Figure 5.9:
FIGURE 5.15 Data plot from multiple plot commands.
plt.xlabel('DISTANCE (cm)')
plt.ylabel('INTENSITY (lux)')
plt.title('Light Intensity as a Function of Distance')
plt.axis([8,24,0,1200])
plt.legend()
EXERCISES
Make a plot of the arch. Draw a horizontal line at ground level (y = 0),
.
5. In astronomy, the relationship between the relative temperature
(temperature relative to the sun), relative luminosity
and relative radius a star is modeled by
Problem Solving
6.1 OVERVIEW
If time permits and the results are not what you expected, you may wish to go
back a few steps to improve the solution. What are the steps in detail?
Identify the problem: You should be able to create a clear written statement
of the problem to be solved. If you cannot do this, you will struggle to
come up with a good solution.
Execute the plan: Write the code. Keep track of what works, and what does
not (source control, such as git, is often helpful but outside the scope of
this book). It can be helpful to write and test small portions of the
solution independently to make sure that those parts work as expected.
This will allow you to build on smaller successes and to eat the elephant
one bite at a time.
Analyze the solution: Revise the plan and reexecute as needed. Keep the
good parts of the plan, and discard the not so good parts. We talk in
greater detail about verifying and validating models in Chapter 12.
Report/document the results: Let your team know how your idea worked in
writing. Good documentation and records of your development and
analysis will be helpful at this stage.
6.2 BOTTLE FILLING EXAMPLE
A specific example will be useful for making this more concrete. The
problem is simple: fill a bottle with stones. We need to document any
assumptions that may be required, and then write a step-by-step procedure for
solving the problem. Remember when doing these things, you need to think
like a machine. We want to avoid assumptions and steps in our procedure that
are too broad and vague.
First, what assumptions do we need to make? This is one possible list, but
there could be other assumptions as well.
• Bottle is present
• Stones are present
• There are enough stones to fill the bottle
• Bottle is empty (or at least not full)
• Some (or all) stones fit through the opening
What might an algorithm look like? Remember, this is partially an art, and
our algorithm here is not the only possible solution. There is not necessarily a
right answer, only answers that work, and ones that do not.
Now we have an algorithm for solving the bottle filling problem. We will
return to this example later in the chapter.
6.3 TOOLS FOR PROGRAM DEVELOPMENT
6.3.1 Pseudocode
Pseudocode is an artificial and informal programming language that helps
programmers to develop algorithms. Unlike MATLAB® or Python, there is
no standard grammar or syntax, and no interpreter to execute code. It is
simply writing steps in an algorithm in a code-like manner, rather than using
something like full English sentences. We will use examples later in this
chapter.
Twenty students in a class have taken their first quiz. The grades (in the
range of 0 to 100) are available. Determine the class average on the quiz.
What items are known? The grades, the possible range of legitimate grades,
and that the user of the program does not know MATLAB or Python are all
pieces of information we know. What is unknown? How the grades are
accessed by the program? So, we will list an assumption that the grades will
be input one by one, with a flag value (−99) to indicate when done.
The first step is to write the top-level statement of the program using
pseudocode.
1.0 Determine the class average for the quiz
While this statement accurately captures the program’s purpose, it does not
provide much insight into how to write the code. So the next step is to refine
this statement into more detailed statements.
1.0 Determine the class average for the quiz
1.1 Initialize variables
1.2 Input and sum the quiz grades
1.3 Calculate and print the class average
6.3.3 Flowcharts
Flowcharts—diagrams that describe the logic flow of the program—are very
powerful tools in program development. They are not a complete description
of the program, and not the only tool to use, but can be very helpful for
envisioning the various paths through the program required to solve a
problem. There are no formal standards for flowcharts, but there are some
common guidelines we will lay out.
We now know enough to generate a full flowchart for the bottle filling
example in Section 6.2. Figure 6.2 contains a flowchart for one possible
solution to the bottle filling problem. If you compare the flowchart to the
top–down example created in Section 6.2, you will note that the algorithms
are different.
If D > 0, the algorithm displays a message: The equation has two roots
and then displays the roots.
If D = 0, the algorithm displays a message: The equation has one root,
and then displays the root.
If D < 0, the algorithm displays a message: The equation has no real
roots.
Create a flowchart for your algorithm.
2. Using the top–down method, please describe an algorithm for a
program that calculates the cost of a car rental according to the
following price schedule:
Rental Period
Type of
1–6 days 7–27 days 28–60 days
Car
$162 for 7 days, +$25 for each $662 for 28 days, +$23 for each
Class B $27 per day
additional day additional day
$204 for 7 days, +$31 for each $810 for 28 days, +$28 for each
Class C $34 per day
additional day additional day
Class D cannot be rented for less than $276 for 7 days, +$43 for each $1136 for 28 days, +$38 for each
Class D
7 days additional day additional day
The algorithm asks the user to enter the rental period and type of car.
The algorithm should display the appropriate cost. If a period longer
than 60 days is entered, a message “Rental is not available for more
than 60 days” should be displayed. If a rental period of less than 6 days
is entered for Class D, a message “Class D cars cannot be rented for less
than 6 days” should be displayed. Create a flowchart for your
algorithm.
CHAPTER 7
Conditional Statements
Note: In MATLAB answers to these comparisons are returned as scalar values, 1 representing true
and 0 representing not true.
Python MATLAB
Description Example
Operator Operator
and & If both the operands are true, then the condition becomes true. (a and b) is true
or | If any of the two operands are true, then the condition becomes true. (a or b) is true
not ~ Used to reverse the logical state of its operand. not (a and b) is false
Note: In MATLAB, the operands are numeric, as is the output of the logical test. For example, any
nonzero number is evaluated as true, and the result of a logical test that is true is the value 1.
Not true logical tests return 0.
if conditional statement
command group
end
The second form is the if-else-end format. In this version, command group 1
is executed if the conditional statement is true, whereas command group 2 is
executed if the conditional statement is not true.
if conditional statement
command group 1
else
command group 2
end
if conditional statement 1
command group 1
elseif conditional statement 2
command group 2
...
elseif conditional statement #
command group #
...
else
command group n+1
end
This final form can be complicated, so a flowchart demonstrating how a if-
elseif-else-end block with three possible command groups works is shown in
Figure 7.1.
Let us use a more concrete example. Assume we wanted a program that
would calculate a tip based on the size of the bill such that the minimum tip
was $1.80, the program would tip 18% if the bill was less than $60, and it
would tip 20% if the bill was greater than or equal to $60. If we convert that
to a flowchart, we can see how it would look in Figure 7.2.
format bank
bill=input('Enter the amount of the bill (in dollars): ');
if (bill <= 10)
tip = 1.8;
elseif (bill > 10) & (bill <= 60)
tip = bill*0.18;
else
tip = bill*0.2;
end
disp('The tip is (in dollars):')
disp(tip)
n = 1;
while (n ~= 0)
x = input ('Resistance: ');
switch x
case 100
disp (x)
case 200
disp (x)
case 300
disp (x)
case 400
disp (x)
otherwise
n = 0;
end
end
7.3.2 Python
Python provides three forms of if conditional statements. The most basic is if.
In this form, a command group will be executed if the conditional statement
is true and not executed if the conditional statement is not true. Normal
sequential code execution resumes after the indentation returns to the level of
the if statement. (Python uses indentation to designate code blocks, and is
thus very sensitive to white space.)
if conditional statement:
command group
The second form is the if-else format. In this version, command group 1 is
executed if the conditional statement is true, whereas command group 2 is
executed if the conditional statement is not true.
if conditional statement:
command group 1
else:
command group 2
if conditional statement 1:
command group 1
elif conditional statement 2:
command group 2
. . .
elif conditional statement #:
command group #
. . .
else:
command group n+1
Here is the Python code to implement that flowchart. Note that we must
cast the input, which will be a string, to an integer type, and that we are
applying some formatting to the output to only display two decimal places.
EXERCISES
If D > 0, the algorithm displays a message: The equation has two roots
and then displays the roots.
If D = 0, the algorithm displays a message: The equation has one root,
and then displays the root.
If D < 0, the algorithm displays a message: The equation has no real
roots.
2. Expanding on Problem 2 from Chapter 6, implement your algorithm.
The problem description is restated as follows:
Write a program that calculates the cost of a car rental according to the
following price schedule:
Rental Period
Type of
1–6 days 7–27 days 28–60 days
Car
$162 for 7 days, +$25 for each $662 for 28 days, +$23 for each
Class B $27 per day
additional day additional day
$204 for 7 days, +$31 for each $810 for 28 days, +$28 for each
Class C $34 per day
additional day additional day
Class D cannot be rented for less than $276 for 7 days, +$43 for each $1136 for 28 days, +$38 for each
Class D
7 days additional day additional day
The program asks the user to enter the rental period and type of car. The
program then displays the appropriate cost. If a period longer than 60
days is entered, a message Rental is not available for more than 60 days
is displayed. If a rental period of less than 6 days is entered for Class D,
a message Class D cars cannot be rented for less than 6 days is
displayed.
CHAPTER 8
Iteration and Loops
for j=1:4
j
end
This loop will run four times, displaying 1, 2, 3, and 4 each time respectively,
as j is incremented by 1 each time through the loop, starting at 1 and exiting
when greater than 4. MATLAB is very quietly creating a vector to iterate
over, storing each element in the array in j one at a time as we repeat the
loop. This means that we can use MATLAB’s vector syntax to precisely
control the size that we increment j by each time through the loop. We will
commonly want to use the default increment of 1, in order to index in to
arrays or matrices, but we could for example step by 0.5.
for j=1:0.5:4
j
end
You can nest for loops, which is commonly used to iterate over a matrix.
for j=1:4
for k=1:4
a(j, k)=j*k;
end
end
a
for j in range(4):
print(j)
This loop will run four times, displaying 0, 1, 2, and 3 each time respectively.
Although you can use this mechanism to do some operation on every element
in an array, Python allows you to iterate directly over a list of items.
import numpy as np
a = np.array([1,2,3])
for j in a:
print(j)
This example will create the array [1 2 3], and then iterate over this array,
printing the first, second, and third items in the array on each run through the
loop respectively.
count = 0;
while (count < 4)
count
count = count + 1;
end
When run, it will output 0, 1, 2, and 3 on each iteration through the loop
respectively. When count is equal to 4, the test condition will become false,
and the loop will exit.
count = 0
while (count < 4):
print(count)
count = count + 1
When run, it will output 0, 1, 2, and 3 on each iteration through the loop
respectively. When count is equal to 4, the test condition will become false,
and the loop will exit.
Both MATLAB and Python support the use of two special control statements
that modify the behavior of a for or while loop.
8.3.1 Continue
The continue command will skip the rest of the commands in the current
iteration of a for or while loop, and will cause the code to jump to the next
iteration of the loop. In nested loops, it only applies to the loop it is executed
in. In other words, a continue inside the inner loop will jump to the next
iteration of the inner loop, and the outer loop will not be impacted. The
syntax for both languages is identical: just the keyword continue on its own
line.
8.3.2 Break
The break command will cause execution to leave the loop entirely, and the
program will jump to immediately after the loop. Just like the continue
command, in nested loops only the loop it is executed in is impacted. The
syntax in both languages is the keyword break on its own line.
EXERCISES
1. Write a for loop that prints all integers from 1 to n.
2. Write a for loop that prints all integers in reverse from n to 1.
3. Write a while loop that prints all even numbers from 1 to 100.
4. Write a while loop that prints all integers in reverse from n to 1.
5. Write a loop that calculates the factorial of n (n!—the product of all
positive integers less than or equal to n).
CHAPTER 9
Although linear models are very useful in analyzing some phenomena and
especially static systems, most real systems are much more complex as well
as dynamic. For many systems, the relationship between the causes and
effects in the model will be nonlinear in form. In addition, the state of the
system will change over time and/or space reflecting dynamic changes in the
forces underlying the system.
In this chapter, we will examine the components of dynamic systems,
extending our approach to creating a conceptual model to reflect that
dynamism. We will then review several examples of the mathematical
representations of nonlinear systems. Then we will illustrate several
mathematical approaches to modeling nonlinear dynamic systems. This
chapter then concludes with examples of dynamic systems and the computer
algorithms that can used to model those systems.
Simply stated, the rate of change in the variable y(dy) with respect to the
change in x(dx) is a function of x. For dynamic models, we are predicting the
change in the level with respect to the change in time. The nature of the
function f(x) will vary depending upon the phenomenon we are modeling.
Later in this chapter, we will look at a variety of examples and create
programs to model those changes.
Another way of thinking about rates of change over time is by dividing
time into small, discrete intervals. For each time interval, the level we
calculate at time t will be the level at the previous time period (t − 1) plus the
change in the level that occurred in that interval. The change could be
positive or negative, depending on the nature of the relationship. This
difference equation can be written as follows:
(9.2)
where:
level(t) is the level at the current time period
level(t − 1) is the level in the previous time period
Δlevel is the change in the level during that time period
For many systems dynamics problems, this is the strategy for modeling
systems with a computer. That is why we approximate the change in level
that varies depending on the nature of the underlying mathematical function
that best represents that particular system. We will review a number of those
examples in Sections 9.2.2 and 9.2.3.
where:
X represents the stock at the current time t and the previous time t − 1
R represents the rate of change
Δt is the time increment
where:
a is a scale factor
b is the base
k is the growth constant
where:
t is time
the independent variable x is a variable that changes as a function of time
x0 is the starting value of x
k is the growth constant
e is the natural logarithm base
where:
t is time
A/A0 is the ratio of the remaining 14C atoms to the amount in the
atmosphere
ln is the natural logarithm
where:
L is the curves maximum value or limit
k is the steepness of the curve
x0 is the value of x-value of the midpoint
e is the natural logarithm
where:
ΔP is the change in the population
P(t − Δt) is the net population change in the previous period
R is the birth rate
C is the carrying capacity
The first term represents the results of the births in the period added to the
previous period base. The second term uses the ratio between the resulting
population and the carrying capacity to modify the additional population
added in the time increment. When the ratio is small, the population will
grow at nearly the exponential rate. As the carrying capacity is approached,
there are increasing reductions in the population resulting in a leveling off of
the population growth. One of the exercises for this chapter is to program that
model and graph the resulting distribution.
Although this approach may mimic the form of the population changes, the
model does not explain the underlying causes of those changes. More
sophisticated models have explicit relationships across the growth and
constraint factors. In population models, these may include age-specific death
rates, the limitations of the food supply or habitat, the occurrence of disease,
predation, and other factors.
In the industrial dynamics models started by Forrester, constraints included
such factors as the availability of capital, the current production capacity,
market penetration, availability of labor, and management expertise. Similar
approaches have been used to forecast a variety of social and economic
phenomena including land use change, the growth of social movements, and
the housing market.
Estimating the values of all of the relevant parameters can be difficult. The
accuracy of the model will be influenced by the availability of data for the
system in question. However, even without exact measurements of all of the
relationships, models can be used to answer what if questions associated with
reasonable variations in the model parameters, providing important insights
into system behavior.
You should notice that the pattern that emerges is becoming increasingly
unstable, with larger peaks and valleys. Change the model parameters to
investigate how a more stable population might be achieved. Then discuss the
design of a more complex model including the incorporation of additional
components of the data that would be needed to implement it. Discuss the
findings of your model along with those concerning the design of the more
complex model.
The growth model examples illustrate how one implements a nonlinear model
given a set of governing equations that represent changes in the phenomenon
being modeled with respect to time. Models of physical phenomena use
scientific laws and theories to construct the equations governing their
behavior. Those theories have also been tested against experimental data that
have confirmed the efficacy and have led to modifications of the
representation that more closely match the experimental evidence.
For social phenomena, there are also underlying theories but human
behavior is not subject to the definition of scientific laws with the same level
of certainty. Instead, those models are most often based on empirical
evidence from primary or secondary data sources that are used to estimate
one or more governing equations. Making such estimates will be discussed
further in Chapter 10.
In either case, anyone modeling a system needs to understand its limits
may lie either in empirical terms from observations of data or from the
underlying scientific or engineering principles that have been developed from
accumulated knowledge. Following these general guidelines will help to
ensure that the models you formulate either account for those limits or
explicitly indicate that they are missing and need to be considered:
1. Define the purpose of your model and the accuracy required to make a
correct decision.
2. Conduct a literature search that focuses on models of the same
phenomena that have been developed by others. Full understanding of
more advanced models will often require a deeper background in
mathematics as models are likely to use linear algebra, differential
equations, and/or partial differential equations to express the underlying
relationships.
3. Create a conceptual model that accounts for the factors that are included
in those other models and the assumptions made in those models.
4. Identify how well the models estimated the observed results and the
potential sources of error indicated in the literature.
5. Identify the risks associated with your model giving a wrong answer
and how this relates to the known errors. For example, if you are
modeling a skydiver, an overestimate of how effective the parachute
will be at slowing down the decent once it is open could result in injury
or death. In such a case, we would prefer to err on the conservative side
and either open the parachute sooner or use a bigger parachute that
slows us down more to avoid that risk.
6. Honestly report the limitations of your model with respect to both
known and unknown causes of model variation.
If you take these steps, you will build an understanding of the system being
modeled and will build a model that provides the insights you seek.
(9.12)
where:
p is the position of the ball and p0 is its initial position
vi is the velocity and v0 is the initial velocity
g is the acceleration due to gravity
t is the time
These equations are used to solve for the position and velocity of the ball in
one of the programming exercises in this section.
The other force acting on a falling object is friction. For an object moving
through a fluid, the frictional force is called drag. In the case of the ball, a
bungee jumper, or skydiver, the objects are moving through air. The general
formula for the force of drag Fd is as follows:
(9.13)
where:
Cd is the drag coefficient
ρ is the density of the fluid; for air at sea level this is 1.29 kg/m3
v is the velocity of the object
A is the objects area in the direction of movement called the characteristic
frontal area
The drag coefficient is a dimensionless coefficient that takes into account the
object’s shape. Note that the drag increases with the square of velocity. The
drag force is in the opposite direction of the force of gravity. Thus, for a
skydiver, the parachute is designed to create sufficient drag so that the person
will hit the ground at a speed that will not cause any injury.
A more complete model of a skydiver will therefore need to account for
the forces in both directions and how they vary with respect to time in order
to track the velocity of the skydiver as they reach the ground. Creating such a
model is one of the capstone projects presented in Chapter 14.
REFERENCES
Bowman, S. 1990. Radiocarbon Dating. Berkeley, CA: University of California Press.
Forrester, J. 1961. Industrial Dynamics. Cambridge, MA: MIT Press. (Also available online at
https://babel.hathitrust.org/cgi/pt?id=mdp.39015002111774;view=1up;seq=9).
Libby, W. F. 1955. Radiocarbon Dating. 2nd ed. Chicago, IL: University of Chicago Press.
Meadows, D. H. 1972. The Limits to Growth: A Report for The Club of Rome’s Project on the
Predicament of Mankind. New York: Universe Books.
Systems Dynamics Society. 2016. Introduction to systems dynamics.
http://www.systemdynamics.org/what-is-s/ (accessed August 2, 2016).
CHAPTER 10
Caution must also be used because the experiments have been run under a
standard set of conditions and have not tested the relationships under all
possible conditions. For example, tests of the mechanical properties of a new
material may not have included all possible types of mechanical failure or not
tested for failure under all possible circumstances. Behaviors may change
under different pressure or temperature conditions. The empirical data
describes a limited set of relationships but does not fully explain the
relationships in the entire system. Thus, the limitations of the experiment
translate into limitations in the models created from the experimental data.
For datasets that include observations in open systems where standardized
controls are not possible, additional limitations arise. For example, samples
of aquatic biology and related physical and chemical measures of water
pollution are only taken intermittently in very discrete locations along a
stream. Sampling then has both spatial and temporal limitations that could
limit the ability to explain a decline in the diversity of the aquatic biota in
response to environmental stresses such as water pollutants. The response of
the biota to a stress may be due to an event in the same section of the stream
or an upstream event, which migrated downstream. Depending on the timing
of water sampling, the cause may or may not be captured in the dataset. In
addition, the cause could be a combination of several pollutants, some of
which might have been measured and others that have not been measured. An
empirical relationship in such a circumstance may capture one or more
independent variables that appear to be related to the decline in biota but
there may be other causes that covary with what has been measured. In
addition, the sample may not be representative of all possible conditions,
which will introduce other biases into the statistical analysis.
These kinds of problems can be even more complicated with
socioeconomic data gathered by the U.S. Census or through other surveys
and indices. Individual responses are generally not available to the public to
protect the privacy of individuals and businesses. Thus, they are reported as
summaries for larger geographic units that are not necessarily homogenous in
their measured characteristics. A census tract may encompass several
different neighborhoods with a different mix of population, housing, public
services, and businesses. In addition, many socioeconomic variables are
highly correlated with one another making it difficult to define what is the
real cause of a change. For example, a data analysis of migration within a
metropolitan area from the central city to the suburbs might show that both
income and education are the important determinants of the likelihood to
migrate. However, income and education are highly correlated. Do people
migrate because they have the economic means to move and desire to have
larger single family homes in the suburbs? Are they more educated and
therefore desire to move because they perceive that suburban schools are of
higher quality? Is there another circumstance that is the underlying cause of
their migration such as the perception of crime in the target neighborhoods
that is not represented in the data? All of these may play a role but sorting
them out from the empirical data may not be possible. One must carefully
think through the possible causes and effects and use one or more proxy
variables that are representative of the possible driving forces to forecast the
trends without placing too much confidence that the causes are understood.
Empirical models can be used to forecast the trends in circumstances but
do not necessarily fully capture the underlying causes of the changes in
the system.
Finally, the sampling associated with empirical models constrains the validity
of the projections if there are circumstances that would change the
relationships if the samples were taken at a different time or in different
places. Going back to our example of the diversity of biota in a stream, we
would need to ascertain whether the datasets are representative of a typical
year or if they contain instances of infrequent but cyclical events such as
droughts or floods. The stresses caused by these intermittent events can easily
create a bias in our empirical model. In a similar vein, the spatial distribution
of our sampling distribution may not capture unusual events in the watershed
such as a toxic spill or the runoff of a large amount of sediment from a
construction site upstream.
With socioeconomic data, we often use both current surveys and historical
data to construct models of a myriad of behaviors relating to markets, travel,
and social trends. These models assume that our past samples are
representative of what will happen in the future. However, there may actually
be changes in technology, economic circumstances, or social attitudes that
profoundly change what will happen in the future. If someone was modeling
the underlying causes of traffic fatalities, could they have anticipated the
change in technology that has caused a large number of accidents because of
the distractions of text messaging?
With all of these caveats in mind, it is still useful to build models from
empirical datasets. Such models can lead to a deeper understanding of the
system and guide future research, policy, and management decisions. The
relationships that are defined may also lead to a full explanation of system
behavior that can be incorporated into future models. The following sections
introduce methods to define and measure the efficacy of empirical models
and provide several examples.
The first step in creating a model from empirical data is to assess the nature
and strength of the relationship between the independent (causal) variable(s)
and dependent (effects) variables. This can be done in several ways. For a
simple two variable case, plotting the data points for one variable against the
other (often called an XY or scatter graph) may be good starting point.
As an example, download the file paintcr2.csv from the book website. The
file has two columns representing a sample of two water quality variables
from the Paint Creek watershed in Ohio. The first column is a numeric
indicator of the diversity of the fish population in a section of the creek called
the index of biotic integrity (IBI). The IBI ranges in value from 12 to 60
where values in the range from about 44 to 60 indicate watersheds that
exhibit the diversity of the fish population that would be expected in an
unpolluted, natural stream in Ohio. The second column represents the
maximum value of total suspended solids (TSS_MAX) found in the same
stream segments where the IBI was measured. Sediments are pollutants
brought into the stream via erosion that damage the aquatic habitat by
covering the bottom of the stream where fish may lay their eggs or find their
food sources. At the extreme, sediments make it difficult for fish to see their
prey, further hindering their success. Thus, we would hypothesize that as the
level of TSS goes up, the diversity of the fish population would go down. We
will use the data to test this hypothesis and create a statistical model of that
relationship. Use the following instructions for MATLAB® or Python to
import the dataset and make an XY plot of the relationships:
For those using MATLAB, from the home tab, choose Import Data. A
window will open that will allow you to go to the directory where you
have downloaded the file and choose the paintcr2.csv file. The table
should appear showing the nine rows representing the samples for IBI
and TSS_MAX. Relabel the columns by double-clicking and inserting
the correct variable names. Then click on the import selected button to
import the data. Use the code in the following box to create an XY
graph.
For Python, click variable explorer and choose the Import Data option
from the right side icons. Navigate to the paintcr2.csv file and import it
as data. It will appear in the variable explorer as a 9 by 2 matrix. Use the
Python code below to extract the two variables and make the XY plot.
MATLAB CODE
sz=45;%sets the size of circles in the plot
scatter (TSS_MAX, IBI, sz,'ko','filled');%x axis is TSS black
circles
PYTHON CODE
import math
import matplotlib.pyplot as plt
import numpy as np
IBI=np.zeros(9)
TSS_MAX=np.zeros(9)
IBI=paintcr2csv[0:9,0]
TSS_MAX=paintcr2csv[0:9,1]
x=TSS_MAX
y=IBI
plt.scatter(TSS_MAX, IBI)
FIGURE 10.1 X-Y plot of index of biotic integrity and maximum total suspended solids.
(10.1)
where:
x and y are the predictor and response variables respectively
a is the intercept of the line
b is the slope of the line
ε is the error or residual between the model and the observations
The approach used to establish the best fit line and calculate the error is to
minimize the sum of the squares of the residuals. This is illustrated
graphically in Figure 10.2. The residuals are measured as the vertical distance
between the line and the observations and several of which are shown in the
figure as dotted lines. For our example dataset, we provide an exercise at the
end of this chapter to derive the relevant regression equation.
We are assuming at least a basic understanding of the general principles of
descriptive and inferential statistics and will not repeat them here. In addition,
the underlying mathematics of the least squares function are illustrated in
great detail by several authors and will not be covered here (Draper and
Smith, 1998; Chapra, 2008). Please consult these references and any
introductory statistics sources for further information and more extensive
applications of regression techniques.
TABLE 10.1 Regression Results from CDC Data on Smoking and Heart
Disease Deaths by State
Linear Regression Model: Rate ~ 1 +
Model
CurrentSmoking
Standard
Estimated coefficients: Estimate tStat pValue
Error
(Intercept) 40.672 22.776 1.7857 0.080332
3.0752e-
CurrentSmoking 5.1244 0.97289 5.2672
06
We can use the output shown in Table 10.1 to construct our linear model
and judge its efficacy. The coefficients for the linear model are shown under
the estimate column in the table. If we accept the model the equation would
be as follows:
We can see that the pValue for the intercept is over 0.05, and the value for
CurrentSmoking is much less than 0.01. Thus, we might want to run the
model again forcing the intercept to be zero. We also see that the overall
model is significant with a very small pValue but we are only explaining part
of the variance in the distribution. There are two R-squared values that show
us this. The adjusted R-square is usually the one we use. It is adjusted relative
to the degrees of freedom associated with the model. It shows us that 34.8%
of the variance is explained.
Clearly, there are other risk factors for heart disease deaths that are not
captured by this empirical model. We could choose to use this model,
acknowledging that it underestimates the risk. Alternatively, we could search
for other risk factors that might create a better model.
Our first step is to create a new variable llight, which is the natural
logarithm of the predictor variable. To find the built-in functions for this, you
can use the help item in MATLAB to search for math functions. For the Math
and SciPy modules, you can find a list of the built-in math functions on the
websites cited at the end of this chapter. In both cases, this is the log()
function. Thus, we take the log of the response variable vector to create a new
vector called llight.
Now we can use one of the tools from MATLAB or Python to create a
linear regression of llight as a function of the original distance data. In both
cases, there are several options that can be used to fit a linear model. They
vary both in their syntax and the amount of information they give us about
the goodness of fit.
Tables 10.3 and 10.4 provide examples of the several ways that one can fit
a linear or nonlinear equation to a dataset in MATLAB and Python,
respectively. Here, we will provide one example of each but provide an
opportunity to try several others as part of the exercises at the end of this
chapter.
For MATLAB, we will use the built-in curve fitting application to fit the
transformed data to a linear equation. Go to the top menu in MATLAB with a
tab labeled as APPS and choose Curve Fit. Add a title and choose the input
values for x (distance) and y (llight). The standard linear model can also be
represented as a first degree polynomial. Thus, in the center of the curve fit
window, we choose polynomial, degree 1 as the fit and click on the Fit
button. You should get a screen that looks like Figure 10.4. The graph shows
the predicted line with a plot of the points around that line. The left side text
shows the regression results.
We can perform a similar function in Python using the Scipy curve fit
option shown in Table 10.4. For many of the Python and several of the
MATLAB options, we need to specify the function that will be used to fit the
data. As shown in Table 10.4, we define a linear function and then use this
function in the curve fit procedure. The function returns the coefficients in
the linear equations. We could then use the plotting functions to create a
graph similar to Figure 10.4.
10.2.3.3 Segmentation
In circumstances where the data distribution has two or more distinct shapes,
an approach called segmentation can be used to create separate linear or
nonlinear models for each segment. Such circumstances might arise when the
phenomenon being modeled changes behavior at distinct points. For physical
phenomena, that may be the case if there is a change in state due to changes
in conditions like temperature or the amount of stress. For social systems,
there may be a tipping point where a market changes behavior such as the
real estate values in a gentrifying neighborhood or rents in parts of cities with
high crime rates.
The same curve fitting approaches are followed for each of the distinct
segments. The resulting model then must check for the values of the
independent variables at the segment boundaries and apply the appropriate
predictive model for that segment.
EXERCISES
1. Use the Paint Creek dataset to find the coefficients for a linear model
using at least two of the different procedures in either MATLAB or
Python. For the procedures that do not produce a graph, create a graph
showing the regression line and the distribution of the original points
around that line. Create a report showing your results.
2. Use the light dataset to create both the transformed linear model and a
nonlinear, exponential model of the relationships in either MATLAB or
Python. For the linear model, calculate the predicted values from the
model and plot that as a line along with the scatter of points from the
original dataset. For the nonlinear model, create a similar graph.
Compare the two models in relation to the values of adjusted R2, the
significance of the coefficients, and the scatter of the original data
around the predicted curve. Which model do you think works better in
this instance? Create a report showing your results and analysis.
3. Find another dataset of interest to you where you hypothesize a cause
and effect relationship between one response variable and one or more
predictor variables. Plot the relationships and then choose one or more
curve fitting functions to test the strength of the relationships. Prepare a
report showing the statistical results and plots illustrating the model.
FURTHER READINGS
Math Functions. Scipy.org. https://docs.scipy.org/doc/numpy/reference/routines.math.html#exponents-
and-logarithms (accessed November 11, 2016).
Mathematical Functions. Python Software Foundation. https://docs.python.org/3/library/math.html
(accessed November 11, 2016).
REFERENCES
Chapra, S. C. 2008. Applied Numerical Methods with MATLAB for Engineers and Scientists. New
York: McGraw-Hill.
Draper, N. R. and H. Smith. 1998. Applied Regression Analysis. New York: Wiley.
Pandas Data Analysis Library. http://pandas.pydata.org/ (accessed November 11, 2016).
Scipy.stats Documentation. https://docs.scipy.org/doc/scipy/reference/stats.html (accessed November
11, 2016).
Statsmodels Documentation. http://statsmodels.sourceforge.net/stable/index.html (accessed November
11, 2016).
Thomas, H. 2016. An Introduction to Statistics with Python. Switzerland: Springer International
Publisher.
CHAPTER 11
Stochastic Models
11.1 INTRODUCTION
Thus far, we have been working with deterministic models. Those models
have used parameters that remain constant throughout the simulation, leading
to a singular result. We have used sensitivity testing to explore the impacts of
changes in selected parameters but always in the same deterministic
framework.
There are many situations in which the values of one or more of our model
components are uncertain. There is an element of chance associated with their
values. In such cases, the models are said to be probabilistic or stochastic.
For some of the simple systems we have used as examples earlier in the book,
we can imagine stochastic versions of the models to represent those systems.
For example, our traffic model, we could insert a random probability that we
encounter other cars or pedestrians at intersections that would add time to our
commute. We could add the impacts of weather events such as precipitation
that would slow traffic below normal speeds. In fact, in our community of
Columbus, Ohio, the mere prediction of snow slows down the traffic!
In each case, we would create a random variable based on a sample of data
from observations to represent the probabilities of different conditions. A
random number generator would then be inserted into our code to simulate
the random effects. We would then run the model many times and record the
distribution of the outcomes to provide further insights into the behavior of
the system.
In this chapter, we will discuss the methods used to create stochastic
models and how these are implemented in both MATLAB® and Python.
Using simple examples, we will illustrate how the models can be run and
interpreted. Some examples of stochastic models from a variety of fields will
be presented. Finally, we provide some exercises where you build a
stochastic model.
The initial steps for creating a stochastic model are the same as those
presented in Chapter 1. The model objectives are specified leading to a
selection of the variables and the governing equations. For a stochastic
model, the first additional step is to select the variables whose values will
vary randomly. For each of those variables, several additional decisions need
to be made:
A simple example should help to illustrate these steps. Recall that in the
traffic exercise, we assumed that traversing an intersection with a stop sign
would take 30 seconds on average. That would include the time to decelerate,
stop, and accelerate back to the maximum travel speed on that street segment.
However, we know from experience that it will take less time if there are no
conflicts with other cars or pedestrians at the intersection and more time if
such conflicts occur.
We can add a random variable to our model that varies the amount of time
at each stop sign depending upon the number and nature of conflicts that
occur at that intersection. To implement this, we could make observations of
the target intersections along our route during the time of day we would
normally commute to work. Let us say we divided the events of cars entering
these intersections to the four categories in Table 11.1. For those
observations, we record the time at the intersection and the number of times
that event occurred. The table is then compiled showing us the range of time
from 10 to 60 seconds along with the percentage of the time that each event
occurred. We can then use that distribution to alter our program to include
these random events.
MATLAB Commands
rand Uniformly distributed random numbers
randn Normally distributed random numbers
randi Uniformly distributed pseudorandom integers
rng Control random number generation
random(pd) Returns random number from a probability distribution
Python Commands
Random module (import random) rand Subcommands to generate uniform, normal, or other distributions of random numbers
random.random() Uniform random numbers
random.gauss(mu, sigma) Normal random numbers with mean mu and standard deviation sigma
numpy.random Module with many similar random number generators
Table 11.2 shows that MATLAB has three different random number
generators for uniform, normal, and integer random numbers (MathWorks,
2016). Each time MATLAB starts, it resets to the same initial seed for
random numbers. The rng command allows control over the seed as well as
the algorithm that is used in generating random numbers. The command can
be used to save a particular starting point, reset to a starting point, set a user
defined seed, and set the generator algorithm. There is also a function that
can draw a sample for any defined probability distribution.
In Python, there are at least two sets of random number functions. One
resides in the random module and has separate commands for a variety of
distributions (Python Documentation, 2016). If no seed is provided using a
random command, the default is to set the seed to the system time. NumPy
also has a variety of random number and sampling commands that operate in
a similar way (NumPy Documentation, 2016).
Let us create a simple stochastic model to represent the random toss of a coin.
We can select a uniform random number between 0.0 and 1.0. Assuming that
the coin is balanced, there should be an equal probability of getting a head or
tail on each toss. Thus, if the random number that is generated is less than
0.5, we can say that it simulated heads and if it is 0.5 or greater, it simulated
tails. The following MATLAB and Python codes show one approach to
implementing this program:
EXERCISES
Let us conduct a number of random walks to identify a search grid for the
lost hiker. Using the script, conduct 20 random walks. Record the coordinates
of the hiker’s final position and distance from the origin. Plot those results
and use them to recommend a search area for the lost hiker.
FURTHER READINGS
Python Software Foundation. Python random.py. https://docs.python.org/3/library/random.html
(accessed December 1, 2016).
SciPy.org. Random sampling. Numpy random.
https://docs.scipy.org/doc/numpy/reference/routines.random.html (accessed December 1, 2016).
REFERENCES
Bareiss, E. H. 1970. Computers and reactor design. In Computers and their Role in the Physical
Sciences, ed. S. Fernbach and A. Taub. pp. 337–384. New York: Gordon and Breach Science
Publishers.
Kalos, M. H. 1970. Monte Carlo methods. In Computers and their Role in the Physical Sciences, ed. S.
Fernbach and A. Taub. pp. 227–235. New York: Gordon and Breach Science Publishers.
Keane, R. E., R. A. Loehman, and L. M. Holsinger. 2011. The FireBGCv2 landscape fire and
succession model: A research simulation platform for exploring fire and vegetation dynamics.
General Technical Report RMRS-GTR-255. Fort Collins, CO: U.S. Department of Agriculture,
Forest Service, Rocky Mountain Research Station.
Lecca, P., I. Laurenzi, and J. Ferenc. 2013. Deterministic Versus Stochastic Modelling in Biochemistry
and Systems Biology, Woodhead Publishing Series in Biomedicine. Jordon Hill, Great Britain:
Woodhead Publishing. ProQuest ebrary. Web. 8 (accessed December 2016).
MathWorks. Random number generation. https://www.mathworks.com/help/matlab/random-number-
generation.html?requestedDomain=www.mathworks.com (accessed December 1, 2016).
Voinov, A. R., R. Soctanza, M. J. Roelof, T. M. Baoumans, and H. Voinov, 2004. Patuxent landscape
model: Integrated modeling of a watershed. In Landscape Simulation Modeling, ed. R. Costanza
and A. Voinov, 197–232. New York: Springer Verlag.
Wilkinson, D. J. 2011. Stochastic Modelling for Systems Biology, Chapman and Hall/CRC
Mathematical and Computational Biology 2nd ed. London: CRC Press. ProQuest ebrary. Web. 8
(accessed December 2016).
CHAPTER 12
Functions
This function will take the input argument and place it in the variable “x,”
check that x is a vector (and return with an error if it is not), and then
calculate the mean for the vector, store that in the variable mean, and then
return it. It is important to note that MATLAB handles input variables by
something commonly called pass by value, which means that any
modifications to the input variable that occur inside the function will not be
reflected to the variable outside the function. If you saved the file in your
MATLAB path (which includes the current working directory), you can test
this by calling the function in the Command Window, passing different
values and variables to it.
MATLAB functions also support multiple return values. We can modify
the function in stats.m to return the mean and standard deviation of the input
variable.
Now, when you call the function, you can get both the average and standard
deviation returned and stored in local variables, by calling your function with
the following syntax:
[avg, stdev] = stats(input_vector);
MATLAB functions are in scope and callable provided the file name matches
the function name, and the file is located in the MATLAB path. The path can
be modified by clicking Set Path in the environment section of the Home
ribbon, and can be inspected by looking at the path variable. You can create
what are called subfunctions—functions only available and in-scope inside a
particular function—by simply adding additional function definitions inside
your primary function. This can be useful if you have code you want to reuse
multiple times in a function, but that has no value outside of that function.
MATLAB also supports variable numbers of input and output arguments
by using special variables called varargin and varargout. These can be used
to create functions that have optional input or output variables. This will not
be commonly used, so we will not talk about it in any more detail here.
Variables defined inside of a function only have scope inside that function,
meaning that when the function exits, the variables cannot be accessed, and if
they have a name that conflicts with a name in the calling workspace, the
variable in the calling workspace is not affected.
Python allows the creation of local functions, which are only in scope within
the currently executed file, and the creation of code libraries via modules. We
will first look at the syntax around functions and then proceed to
demonstrating how to create custom modules.
import numpy as np
Now, when we call the stats() function, if we did not supply an argument for
my_array, the Python interpreter will use the array [1,2,3] instead of an error
stopping execution of our program.
Python also supports variable-length argument lists. These additional
arguments are nonnamed, cannot have default values, and are placed in a
tuple, a special type of list in Python that you can iterate over.
Variables inside a function in Python have local scope, which means that
they can only be accessed inside the function, and have no value outside of
the function. If a variable name inside a function is the same as a variable that
exists outside the function, the variable outside the function is not impacted
by anything that happens inside the function.
12.2.2 Python Modules
Code reusability is one of the biggest advantages of functions, and if we
could only use functions in the file they are defined in, our code would be
less reusable and become more difficult to maintain. Thankfully, Python
allows us to create our own user-defined modules to package up our custom
functions to make them easier to reuse.
Creating a module is very simple. If we want to package up our stats()
function into a module called mymodule, we would place the function
definition (along with any other functions we wish to put into mymodule)
inside mymodule.py. At this point, we can import the module and use the
functions defined in it just as we have imported other modules such as
NumPy.
import mymodule
a = mymodule.stats([1,2,3,4,5])
When the Python interpreter attempts to import modules, it first looks in the
local working directory and then looks in the environment variable
PYTHONPATH for a list of directories to search.
EXERCISES
1. Write a function that calculates the factorial for the input value.
2. Write a function that calculates the factorial for each value of an input
vector and that returns a vector of results.
3. Write a function that takes an input matrix and calculates the average
value of each row (or—if instructed by an optional argument—the
average value of each column) and that returns a vector containing the
results.
4. Package your functions from these exercises into a custom Python
module.
CHAPTER 13
13.1 INTRODUCTION
13.2 ERRORS
There are many possible sources of error in scientific research and modeling.
The data that are used to support the development of models may have errors.
Those errors can be associated with the accuracy of the instruments that are
used to measure what is happening. Errors may also be made in the
formulation of a model by making incorrect assumptions about the system
behavior, assumptions that oversimplify the system, decisions on the critical
components of the model, and the variability of model parameters.
The implementation of a model may also produce errors. Those can
include programming and logical errors that incorrectly implement the model
representation, calculation errors associated with the representation of
numbers by the computer, and errors in the algorithms used to approximate
the mathematical basis of the model.
Identifying and separating out the many sources of errors is a challenge.
The key to approaching this problem is a deeper understanding of the sources
of errors and the techniques that can be used to minimize their impact on the
model results.
The relative error is the absolute error divided by the correct value, assuming
the correct value is not zero. This can be calculated as a proportion or a
percentage:
(13.2)
or
(13.3)
13.2.2 Precision
Decimal numbers in a computer are represented as binary floating-point
numbers. Those numbers are represented in the computer using scientific
notation that includes a sign, a mantissa or significand representing the
numbers and an exponent, for example, +8.94357 × 102 to represent 894.357.
The number of significant digits in the mantissa is limited by the amount of
memory used in the computer to store that number. Numbers that are
represented in 32 bits are called single precision numbers. They can have six
to nine decimal digits. The range of numbers that can be represented in single
precision is from 1038 to 10−38. Double precision numbers can store up to 15–
17 decimal digits and represent numbers from 10308 to 10−308.
TRUNCATION CODES
MATLAB Code
%Truncation example
clear all;
format long
x=3.56e6;
y=2.2800e6;
a=single(x);
b=single(y);
z=x*y;
c=a*b;
fprintf('\nz = %2.8e c= %2.8e',z, c)
format short
z
c
Python Code
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 15 13:52:27 2016
Truncation example
"""
import numpy as np
x=3.56e15
y=2.2800e22
a=np.float32(x)
b=np.float32(y)
z=x*y
c=a*b
print ("z=: %2.8e, c=: %2.8e" % (z, c))
print (z, c)
In these codes, two very large numbers are input as x and y. A second
version is created in single precision as variables a and b. Both pairs are then
multiplied. The result is shown in a format, which shows the full value of
each. Although the numbers are close, the single precision number is slightly
smaller. In both cases, the program writes out both numbers in a more
standard decimal format. When this is done, rounding occurs and both values
appear to be the same.
The conversion between decimal and floating-point numbers often results
in these errors. This is an important tendency to keep in mind when writing a
program using floating-point arithmetic. It implies that we should never test
for the equality of two floating-point numbers because there is high
probability that they will be close but not equal.
One way of minimizing errors associated with rounding and truncation is
to perform the calculations with very small numbers first and then do the
calculations with larger numbers.
Rounding and truncation errors may produce errors that result in slightly
different results when the order of computations is changed. This will be
illustrated by one of the exercises.
where:
y′(t) is the estimate of the value of y given the time dependent function
f(t,y(t))
y(t0) = y0 is a known origin
To implement this algorithm, we choose a step size h and then step through
the calculation:
(13.6)
where:
P0 = 50
r = 0.05
Figure 13.1 shows the result of the simulation after 100 iterations using both
the Euler method and the analytical solution with the time step set to 1. The
figure shows that the Euler method creates a pretty accurate approximation
until iteration 50 when it starts to diverge from the analytical results. By the
time it reaches the 100th iteration, the difference is 797 or 12.7% error. We
can reduce the error by using a smaller time step of 0.25 at the cost of four
times the computational time. When this is done, the error is reduced to about
3.8%. For a small program such as this one, that computational burden is not
significant but for any large code, the trade-off between accuracy and
computational requirements can make a huge difference in the utility and
feasibility of the code.
The third increment k3 is also based on the midpoint but uses the value of
k2:
(13.12)
Then the fourth increment is based on the slope at the end of the interval
using y + hk3:
(13.13)
MATLAB
Functionsa
ode45 Listed as the first solver you should try
ode23 Listed as more efficient at problems with low tolerances
Used when ode45 fails and on problems with stringent error tolerances or when solving differential
ode15s
algebraic equations
Python Functions
scipy.integrate.odeint Integrate a system of ordinary differential equations
scipy.integrate.ode A generic interface class to numeric integrators
scikits.odes 2.2.1 Package that installs several other ODE solvers for Python
a Consult the MATLAB and Python documentation for other options and for solver syntax.
Given the previous discussion of the many sources of errors and the difficulty
in measuring those errors, it should be no surprise that model verification and
validation (V&V) is a challenge. In this section, we provide some historical
background to the process of V&V and the formal frameworks that have been
established by several professional organizations. We then go on to provide
some practical guidelines for conducting V&V that you can immediately
implement.
By following these guidelines, you should be able to verify that your code
correctly implements your conceptual model. You should also be able to
explicitly define the circumstances the model can be applied and have some
initial ideas of the accuracy of the model that can be tested when validating
the model.
where:
yi is the observed values
is the simulated values
n is the number of observations
For stochastic models that produce multiple output values that can be
summarized into either spatial or time based subsets, comparisons can be
made between observed and simulated mean values for each subset. Going
back to our air pollution model, we could make multiple runs to characterize
the possible outcomes during periods of air temperature inversions when air
pollutants are held near the ground. Those simulated averages could be
compared with real data from days where similar conditions existed. In
addition, the distributions around those means could be compared either by
calculating additional descriptive statistics such as the standard deviation and
the skewness or plotted to visualize their relationships. There are also
statistical tests of significance such as the difference of means test that can be
applied to those distributions to test whether the observed and simulated
values are significantly different.
We can also use regression analysis to provide a test of the accuracy of our
simulation. In that case, we would make the observed values our dependent
variable and the simulated results our explanatory variable. The resulting
coefficient of determination will then provide a measure of the amount of
variance in the observed data explained by the simulation model. The closer
the value is to 1.0, the better our model.
There are many other possible statistics that have been used in model
validation. A complete review of all of those methods is beyond the scope of
the current chapter. Hopefully, the examples here provide a solid base in
understanding the process of model validation.
EXERCISES
1. Download and run the glitches.m or glitches.py program file from the
book website. Run the code and observe what happens when the order
of operations change. Describe the results. If you were doing one of
these calculations in a program and wanted the program to branch when
the result of the calculation is zero, how would you compensate for
these truncation errors? Add that code to the program.
2. Write a program to simulate the exponential growth model described
earlier in this chapter. The initial population is 50 and the growth rate is
0.05. Create the model using the Euler method with a time increment of
1 and 100 iterations. Calculate the analytical solution to the equation in
the same program. Graph both distributions and calculate the root mean
squared error for the simulation.
3. Use one of the ode solvers in MATLAB or Python to generate a
solution to the exponential growth problem above. Compare that result
to the analytical solution and calculate the root mean squared error for
that comparison.
4. You are charged with validating a model of the level of dissolved
oxygen (DO) in a river. The oxygen level must remain about five parts
per million (ppm) in order to maintain a healthy environment. You
know that the level of DO can physically go from 0.0 to 14.6 ppm. The
model simulates the impact of a sewage treatment plant discharge. As it
flows downstream, the organic waste from the plant lowers the DO
level as bacteria use oxygen to decompose the waste. Download the file
DO_verify from the book website. The file has a series of model runs
using different assumptions about the amount of organic waste being
released and the efficiency of the environment in adding oxygen back to
the stream. Verify the model using these data. Describe any model
errors you can find and indicate what you would look for and/or add to
the model to fix those problems.
REFERENCES
American Society of Mechanical Engineers. 2006. Guide for Verification and Validation in
Computational Solid Mechanics. ASME Standard V&V 10. New York: American Society of
Mechanical Engineers.
Bush, B. M. 1996. The perils of floating point. http://www.lahey.com/float.htm (accessed December
15, 2016).
Department of Defense. 2009. DoD modeling and simulation (M&S) verification, validation, and
accreditation (VV&A). Department of Defense Instruction Number 5000.62.
https://www.msco.mil/vva.html (accessed December 15, 2016).
168Mathworks. 2016a. Solving ODEs in MATLAB.
https://www.mathworks.com/videos/series/solving-odes-in-matlab-117658.html (accessed
December 15, 2016).
Mathworks. 2016b. MathWorks products and services. https://www.mathworks.com/products.html?
s_tid=gn_ps (accessed December 15, 2016).
Murray-Smith, D. J. 2015. Testing and Validation of Computer Simulation Models. Cham, Switzerland:
Springer International.
Oberkampf, W. L. and C. J. Roy. 2010. Verification and Validation in Scientific Computing.
Cambridge, Great Britain: Cambridge University Press. ProQuest ebrary. Web. 15 (accessed
December 2016).
Schlesinger, S. 1979. Terminology for model credibility. Simulation 32 (3): 103–104.
doi:10.1177/003754977903200304.
Wikipedia. 2016. List of Python Software.
https://en.wikipedia.org/wiki/List_of_Python_software#Scientific_packages (accessed December
15, 2016).
CHAPTER 14
Capstone Projects
14.1 INTRODUCTION
The objective of the projects is to build and test a model of one of the
candidate systems and use that model to derive insights into the system
behavior with respect to a set of proposed problems. Individuals or teams
undertaking the projects will need to complete a number of activities:
Each of the projects is introduced in this section. The auxiliary materials are
available in the Projects folder on the book website. The optional starting
codes for MATLAB® and Python are available on the instructor’s website.
This project will implement a simplified model of the malaria disease cycle
applied to a village in a region with malaria. Assuming that there is no latent
period for infection, the population of humans in the village can be divided
into three groups: healthy villagers, sick villagers, and immune villagers. For
any given time, the change in the number of healthy villagers depends on the
numbers of births, deaths, infected, and recovered villagers. Change in the
number of sick villagers depends on the number of infected villagers,
recovered villagers, villagers who gained immunity, and deaths of sick
villagers. Change in immune villagers depends on the number of villagers
who gained immunity and the number of deaths in immune villagers.
The life cycle of the malaria parasite in mosquitos is little simpler since
infected mosquitos end with death, with no recovery or immunity. For any
time period, changes in the number of healthy mosquitoes depend on the
numbers of births, deaths, and infected mosquitoes, and change in the number
of infected mosquitoes depends on infected and death of infected mosquitoes
during that period of time. Review the references for this project to provide
additional background on the equations used in modeling malaria.
The detailed assignment provides a list of starting equations and
parameters for you to use in creating this model. You should start by
reviewing the references provided for a more detailed review of the life cycle
for malaria and the nature of efforts aimed at controlling its spread.
You should create the basic model using those instructions and then
prepare a report describing the nature of the findings for the basic model and
for any of the optional model additions you implement.
FIGURE 14.3 Comparison of predator prey with Yellowstone National Park. (From
http://www.isleroyalewolf.org/overview/overview/the_setting.html, 2014.)
(14.2)
where:
x is the number of prey
y is the number of predators
The equations represent the growth rate of the populations over time
t represents time
α, β, γ, and δ are constants representing the interactions of the population
where:
F is the force
m is the mass of a body
a is the acceleration
where:
D is the drag coefficient
P is the air density
A is the cross-sectional area of the object
v is the velocity
where:
y is the vertical position
v0 is the initial velocity
t is the time
g is the acceleration due to gravity (9.81 m/s2)
However, the drag is not constant over time. There will be some air resistance
of the skydiver based on how much they spread their limbs to catch the air.
That force will continue until the ripcord is pulled and the parachute
deployed. At that point, the parachute will introduce additional drag to slow
down the descent.
The initial parachute model should account for these two forces assuming a
constant air density. The model should then be used to experiment with the
time and location of free fall with the deployment of the parachute to
determine whether the skydiver will land safely. There are then other
components that can be added to remove some of the model assumptions.
The details for those optional simulations are provided in the downloads for
this project along with a list of references that can be used to choose an
appropriate set of parameter values for the simulation.
where:
D is the dissolved oxygen deficit over time
L is the concentration of organic matter requiring decomposition
k1 is the coefficient of deoxygenation
k2 is the coefficient of reaeration
FIGURE 14.5 Components of the oxygen sag curve.
Thinking about this equation with respect to each increment of time, it says
that the oxygen deficit for the current time period is first, dependent on the
deoxygenation coefficient (a constant for each section of the stream) times
the volume of waste remaining to be decomposed. The deficit is offset by the
reaeration rate times the oxygen left after the last time increment. That is
because the greater the difference between the concentration of oxygen in the
stream and the concentration in the air, the greater the rate of diffusion of
oxygen back into the stream. The constant for reaeration also varies by
stream segment as each segment may have a different amount of turbulence
and different streambed and therefore more or less mixing with the air.
This modeling project will have you build a DO model using a modified
Streeter–Phelps formulation and using that model to evaluate some policies
for changing a sewage treatment facility discharging into an example Ohio
stream. You will test the sensitivity of the model to changes in the parameters
and then undertake one or more optional modeling tasks to examine the
impacts of an increased waste load on the stream and validate the model
against some sample data from the same stream.
REFERENCE
Wikipedia, 2016. Lotka-Volterra Equations.
https://en.wikipedia.org/wiki/Lotka%E2%80%93Volterra_equations. As viewed on December 15,
2016.
Index
Note: Page numbers followed by f and t refer to figures and tables, respectively.
2D plotting,61
command, 61, 68
in MATLAB, 62f
in Python, 69f
tools and functions, 68
2005 Toyota Avalon, design, 9
A
abs() function, 28t, 39t
Absolute error, 152–153, 164
Acceleration (a), 113–114, 176
Algorithms and errors, 155–159
Euler’s method, 156–158
vs. analytic solution, 157f
RK4 method, 158–159
American Society of Mechanical Engineers (ASME), 161
Anaconda, programming language, 31, 68
Array, 26
lists and, 35–38
MATLAB® and Python, 55–60
arrays and matrices, 55–56
matrix mathematics, 56–58
one-dimensional, 27, 36
Python, 37
two-dimensional, 27, 36
array() function, 37
ASME (American Society of Mechanical Engineers), 161
Axis function, 68, 75
B
Ball toss exercise, 114–115
Biochemical oxygen demand (BOD), 178
BlenX, programming language, 141
Blood plasma, 171–172
Blue Waters, 7
BOD (biochemical oxygen demand), 178
Break command, 100
Breakpoints, 30, 41
Brownian motion, 141
Built-in functions, 127
MATLAB, 28t
Python, 39t
Business systems model, 102
C
Calculation verification, 161
Capstone projects, 169–181
descriptions, 171–181
drug dosage model, 171–172, 171f
heart disease risk factors, empirical model, 180
malaria model, 172–174, 173f
options, 181
population dynamics model, 174–176, 174f, 175f
sewage project, 178–179, 178f
skydiver project, 176–177
traffic, stochastic model, 180–181
goals, 170–171
overview, 169–170
Carbon dioxide, 107
Carrying capacity (C), 109
Centers for Disease Control and Prevention, 172, 180
Cmap, 12, 12f
Code verification, 161
Coefficient of determination, 124–125, 165
Coin toss simulation, 138
colon() function, 27
Command Window
MATLAB, 22
programs execution, 29
run command, 30
Spyder, 31
variable, 24
Computational modeling, 1–17
computational science importance, 1–3
modeling process, 9–17
mathematical modeling terminology, 14
and simulation terminology, 14–15
steps in, 11–14, 11f
in science and engineering, 3–9
Computational science, 1–3
variables in, 24, 34
Computer power and scientific modeling, 4t, 5t–6t
Concept map/concept-mapping, 12
drug dosage model, 171, 171f
Moose–Wolf population dynamics, 175f
tools, 12
Conceptual model, 12, 160
Conditional statements, 87–94
logical operators, 88
MATLAB®, 89–92
Python, 92–94
relational operators, 87–88
Constrained growth, 108–111
exercises, 110–111, 111t
Continue command, 100
Continuous model, 15
Control statements, 100
Cosmic rays, 107
Cray-1 supercomputer, 2
D
Def keyword, 147
Del command, 33
Demographer forecasting, 106
Deoxygenation rate, 178
Department of Defense (DoD), 161
Deterministic linear models, 45–52
linear interpolation, 49–51, 50t
linear models/linear equations, 46–49
limitations, 51–52
systems, 51
mathematical representation, 45–46
observe/experiment, 45
screening model, 45
Deterministic model, 14–16, 163
disp() function, 28t
Dissolved oxygen (DO), 178–179
divmod() function, 39t
DO (dissolved oxygen), 178–179
DoD (Department of Defense), 161
Drag coefficient (Cd), 114
Drug dosage model, 171–172
concept map, 171f
Drug screening, 8
Dynamic model, 15–16
nonlinear and, 101–115
E
Empirical data, estimating model, 117–131
build forecasting models, 117–120
limitations, 118–120
fitting mathematical function to data, 120–131
fitting linear model, 122–125
linear models with multiple predictors, 125–126
nonlinear model estimation, 126–131
ENIAC, computer, 3, 7
Errors, scientific research and modeling, 152–159
absolute and relative, 152–153
algorithms and, 155–159
Euler’s method, 156–158
RK4 method, 158–159
numeric associative and distributive properties, violation, 155
ODE Modules in MATLAB® and Python, 159
precision, 153
truncation and rounding, 153–155
Euler’s method, 156–158
vs. analytic solution, 157f
Exogenous parameters, 102
Exponential function, 104, 106
Extending model, 113–115
ball toss exercise, 114–115
eye() function, 27, 37
F
F distribution, 124
Figure function, 74
Fitted regression line and residuals, 123f
float() function, 39t
Flowchart(s), 83–84
bottle filling, 85f
if-elif-else, 93, 93f
if-elseif-else-end, 90, 90f
symbols, 84f
tipping, 91f, 94f
Force of drag (Fd), 114
For loop, 97–99
MATLAB®, 97–98
Python, 98–99
Forrester, Jay, 101, 108–109
Free and Open-Source Software (FOSS), 30
Frictional force, 114
Function(s), 145–149
2D plotting tools and, 68
abs(), 28t, 39t
array(), 37
axis, 68, 75
built-in, 127
MATLAB, 28t
Python, 39t
colon(), 27
curve_fit, 130
disp(), 28t
divmod(), 39t
exponential, 104, 106
eye(), 27, 37
figure, 74
float(), 39t
globals(), 39t
legend, 68, 76
linspace(), 27, 37
MATLAB®, 145–147
ones(), 27, 37
open(), 28t, 39t
plot/plotting, 63, 64f, 70, 73f, 74
print(), 39t
Python, 147–149
code reusability, 148
modules, 148–149
syntax, 147–148
variable-length argument lists, 148
title, 68, 75
G
Galaxy formation, 8
Gametocytes, 172–173
Generalized linear model, 130
globals() function, 39t
Guidelines, 162–166
validation, 163–166
graphical methods, 166
quantitative and statistical validation measures, 164–166
verification, 162–163
H
Healthy villagers, 173
Heart disease risk factors, empirical model, 180
Helloworld.m file, 22, 29
Hold command, 66, 67f
Household heating system, 102
Human-managed systems, 108
I
IBI (index of biotic integrity), 121, 122f
Identity matrix, 57–58
IDEs. See Integrated development environments (IDEs)
If-elif-else flowchart, 93, 93f
If-elseif-else-end flowchart, 89–90, 90f
If statements, 89
Immune villagers, 173
Index of biotic integrity (IBI), 121, 122f
Industrial Dynamics (book), 101
Integrated development environments (IDEs), 30
free, 30
Spyder, 31
Intersection conflict time delays and probabilities, 135t
int() function, 39t
iPhone 5s, 2
IPython, 31, 38, 40, 68, 69f
Isle Royale, 174
K
Keywords, 25, 34
MATLAB, 25–26, 25t
Python, 34–35, 35t
L
Legend function, 68, 76
len() function, 39t
Libby, Willard, 107
Light intensity, 61, 68, 126, 127f
The Limits to Growth (book), 108
Linear equation, 47, 48f, 49
Linear interpolation, 49–51, 50t
Linear model, 47–48
coefficients, 125
fitting, 122–125, 124t
generalized, 130
with multiple predictors, 125–126
spring, 48
standard, 129
Linear regression, 122, 124, 127
Linear transformation
limitations, 130
nonlinear data, 126t
Line specifiers, 63, 70
in MATLAB, 63, 64f
in Python, 70, 71f
linspace() function, 27, 37
Llight, variable, 127
Local variables, 24, 33
Logical operators, 88
Lotka–Volterra equation, 175
Low sampling resolution
in MATLAB, 65f
in Python, 73f
M
Malaria model, 172–174, 173f
Mantissa, 153
Mathematical model(ing), 10, 14, 172
MATLAB®/MATLAB, 23–24
2D plot, 62f
built-in functions, 28t
code, 121
coin toss simulation, 138
Command Window, 22
conditional statements, 89–92
curve_fit functions, 130
curve fitting app, 129f
functions, 145–147
hello world script, 29f
keywords, 25–26
reserved, 25t
linear/nonlinear model, procedures, 128t
line specifiers, 63, 64f
loops, 97–98
low sampling resolution, 65f
mathematic operators, 24t, 25t
matrix operations in, 58–60
plotting in, 61–68, 66f
programming environment, 21–30
basic syntax, 23–28, 24t, 25t
breakpoints, 30
built-in functions, 28t
Command Window, 22
debugging, 30
defined, 21, 23
interface, 21–22, 22f
program execution, 28–29
repeatable code creation, 29–30
reserved keywords, 25t
scalar operation in, 24–25
and Python
array mathematics, 55–60
ODE modules, 159, 159t
random number generators, 136–137, 137t
R2016a, 21
while loops, 99
Matplotlib, 68, 74
Matrix, 27, 36
algebra, 55
identity, 57–58
mathematics, 56–58
in MATLAB®, operations, 58–60
addition/subtraction, 58
multiplication, 57, 59–60
in Python, operations, 59–60
addition, 59
import numpy as np, 59
subtraction, 59
max() function, 28t, 39t
Mind Map Maker, 12, 13f
min() function, 28t, 39t
Mississippi River Basin Model, 10, 10f
Model(ing), 9–17
auto manufacturers, 9
business systems, 102
classification, 14–15
complex systems, 101
computational. See Computational modeling
computer power and scientific, 4t, 5t–6t
conceptual, 12, 160
continuous, 15
deterministic, 14–16, 163
linear. See Deterministic linear models
drug dosage, 171–172, 171f
dynamic, 15–16
nonlinear, 101–115
empirical data, estimating. See Empirical data, estimating model
generalized linear, 130
heart disease risk factors, empirical, 180
linear, 47–48
coefficients, 125
fitting, 122–125
generalized, 130
with multiple predictors, 125–126
spring, 48
standard, 129
malaria, 172–174, 173f
mathematical, 10, 14
Mississippi River Basin, 10, 10f
molecular, 9
multiple regression, 180
nonlinear and dynamic, 101–115
modeling complex systems, 101
physical and social phenomena, 111–115
systems dynamics, 101–111
one-compartment, 172
physical, 9–10
physical and social phenomena, 111–115
extending model, 113–115
tossed ball, model, 112–113
population dynamics, 174–176, 174f, 175f
predator–prey, 110, 174–175, 174f
initial parameters, 111t
probabilistic, 14
regression, 130
screening, 46
spatial, 102
steady-state, 15
steps in, 11–14, 11f
computer model creation, 13
conceptual model, 12
partial concept map, 12, 12f
partial mind map, 12f, 13
problem analyze and objective, 11–12
simplifying assumptions, 13
stochastic, 17, 133–141
creation, 134–136
definition, 133
larger scale, example of, 139–141
random number generators, 136–134
simple code example, 137–139
traffic, 180–181
Streeter–Phelps, 178
systems dynamics, 16–17
Modeling and simulation (M&S), 8, 161
application, 15–17
benefits, 9
terminology, 14–15
concepts and, 160f
mod() function, 28t
Modules, 38
Math and SciPy, 127
Python, 148–149
and MATLAB®, ODE, 159
Molecular modeling, 9
Monte Carlo modeling, 139–140
Moose–Wolf population dynamics, 175, 175f
M&S. See Modeling and simulation (M&S)
Mules, 9
Multiple regression, 126
model, 180
Municipal sewage treatment plants, 178
N
Newton’s second law of motion, 113–114, 176
Nonlinear model
and dynamic, 101–115
modeling complex systems, 101
physical and social phenomena, 111–115
systems dynamics, 101–111
estimation, 126–131, 126t
limitations with linear transformation, 130
nonlinear fitting/regression, 130–131
segmentation, 131
Numerical errors, 155
Numeric associative and distributive properties, 155
NumPy, library, 36–37, 137
O
Object-oriented programming, 145
ODE. See Ordinary differential equation (ODE)
One-compartment model, 172
One-dimensional array, 27, 36
ones() function, 27, 37
open() function, 28t, 39t
Operators
“:”, 27
logical, 88
MATLAB, 24–25, 24t
Python, 34
mathematic, 34t
relational, 87–88
variable and, 23–25, 32–34
Optional starting code, 170–171
Ordinary differential equation (ODE), 158
in MATLAB® and Python, 159
modules, 158
solvers in, 159t
Oxygen sag curve, 178, 179f
P
Partial concept/mind map, 12–13, 12f
Pass by reference, 147
Pass by value, 146
Physical and social phenomena, modeling, 111–115
extending model, 113–115
ball toss exercise, 114–115
tossed ball, model, 112–113
Plot function, 63, 70, 73f, 74
Plotting, 61–76
in MATLAB, 61–68, 62f
2D plot command, 61, 62f
function ploting, 64f
help plot command, 63
hold command, 66, 67f
line specifiers, 63, 64f
low sampling resolution, 65f
multiple curves in single plot, 66f
in Python, 68–76
2D plot, 69f
IPython graphics backend setting, 69f
line specifiers, 70, 71f
matplotlib, 68
multiple curves, 74f
multiple plot commands, 75f
simple plot, 71f
Population dynamics model, 174–176, 174f, 175f
Precision, 153
Predator–prey model, 110, 174
classic, 175
initial parameters, 111t
with Yellowstone National Park, 174f
print() function, 39t
Probabilistic model, 14–15
Problem solving, 79–85
bottle filling example, 80–81, 84–85, 85f
overview, 79–80
program development, tools, 81–84
flowchart, 83–84, 84f
pseudocode, 82
top–down design, 82–83
Pseudocode, 82
Python, 92–94, 137
array, 37. See also Array, MATLAB® and Python
code, 122, 138
conditional statements, 92–94
environment, 30–42
code libraries, 38
debugging, 41–42
defined, 30
keywords, 34–35
libraries, 38–39
lists and arrays, 35–39
mathematic operators, 34t
program execution, 40
recommendations and installation, 30–31
repeatable code creation, 40, 41f
reserved keywords, 35t
Spyder interface, 31–32, 31f
variables and operators, 32–34
functions, 147–149
built-in, 39t
code reusability, 148
modules, 148–149
syntax, 147–148
title, 75
variable-length argument lists, 148
hello world script, 41f
keywords, 34–35
reserved, 35t
line specifiers, 70, 71f
for loop, 98–99
low sampling resolution in, 73f
MATLAB®/MATLAB
ODE modules, 159, 159t
random number generators, 136–137, 137t
matrix operations in, 59–60
addition, 59
import numpy as np, 59
modules, 148–149
operators, 34, 34t
plotting in, 68–76
2D plot, 69f
IPython graphics backend setting, 69f
line specifiers, 70, 71f
matplotlib, 68
multiple curves, 74f
multiple plot commands, 75f
simple plot, 71f
procedures, 128t
variables, 32–33
while loops, 99
Q
Quantitative and statistical validation measures, 164–166
R
R2 (R squared), 124
Radiocarbon age, 107
range() function, 39t, 98
Regression model, 130, 166
Relational operators, 87–88
Relative error, 152–153, 164–165
Return keyword, 147
RK4 (Runge–Kutta 4) method, 158–159
RMSE (root mean square error), 165
Rng command, 137
Root mean square error (RMSE), 165
Rounding error, 153–154
R squared (R2), 124
Runge–Kutta 4 (RK4) method, 158–159
S
Scalar variable, 24
Scipy.integrate.odeint, 159, 159t
Scope, 24, 33
Screening model, 46
Segmentation, 131
Sewage project, 178–179, 178f
SIAM (Society for Industrial and Applied Mathematics), 2
Sick villagers, 173
Simulation
coin toss, MATLAB and Python code, 138
computer, 155, 160
defined, 1, 14
Hooke’s Law (HTML5), 49
modeling and, 14–15
applications, 15–17
concepts and terminology, 160f
terminology and approaches, mathematical, 14
society, 160
Single precision numbers, 153, 155
size() function, 28t
Skydiver project, 176–177
Slices, array, 28, 38
Society for Industrial and Applied Mathematics (SIAM), 2
Spatial model, 102
Sporozoites, 172–173
Spring constant, 49
Spyder, 30–31
advantage over testing code, 41
Editor, 40
interface, 31–32
default, 31f
stats() functions, 145–146, 148
Steady-state model, 15
Stochastic models, 17, 133–141, 165
creation, 134–136
definition, 133
larger scale, example of, 139–141
MATLAB® and Python, random number generators, 136–137, 137t
overview, 133–134
simple code example, 137–139
traffic, 180–181
Streeter–Phelps model and formulation, 178–179
Supercomputers, 1–2, 8
Cray-1, 2
Switch-case structures, 91–92
Systems dynamics, 101–111
components, 102–104
constrained growth, 108–111
exercises, 110–111, 111t
models, 16–17
unconstrained growth and decay, 104–108, 105f
exercises, 106–108
T
Thermostat control, 102–103
Tipping flowchart, 91f, 94f
Title function, 68, 75
Top–down design, 82–83
Toxic effect, 172
Traffic control devices, 13, 180
Truck loading data, 47t
Truncation
code, 154
error, 153, 155
rounding, 153–155
Truncation.m/truncation.py program, 154
T test, 124
Tuple, 148
Two-dimensional array, 27, 36
U
Unconstrained growth and decay, 104–108, 105f
exercises, 106–108
Uniform random number scheme, 135
V
Varargin and varargout, variables, 146
Variable(s), 147–148
Command Window, 24
in computational science, 24, 34
Explorer button, 32
homogeneous, 24
llight, 127
local, 24, 33
MATLAB, 23–24
operators and, 23–25, 32–34
Python, 32–33
scalar, 24
varargin and varargout, 146
Vector, 27, 36
Vector-borne disease, 172
Velocity, 113
Verification and validation (V&V), 159–166
definition, 152, 160
guidelines, 162–166
graphical methods, 166
quantitative and statistical validation measures, 164–166
overview, 160–162
Verification, validation, and accreditation (VV&A), 161
von Neumann, John, 3
V&V. See Verification and validation (V&V)
VV&A (verification, validation, and accreditation), 161
W
While loops, MATLAB® and Python, 99
whos() function, 28t
Y
Yellowstone National Park, 174, 174f
Z
Zero-indexed array, 37–38
zeros() function, 27, 37