Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS,...

63

Transcript of Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS,...

Page 1: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2
Page 2: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

IntroductiontoModelingandSimulation

withMATLAB®andPython

Page 3: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Chapman&Hall/CRCComputationalScienceSeries

SERIESEDITOR

HorstSimonDeputyDirector

LawrenceBerkeleyNationalLaboratoryBerkeley,California,U.S.A.

PUBLISHEDTITLES

COMBINATORIALSCIENTIFICCOMPUTING

EditedbyUweNaumannandOlafSchenk

CONTEMPORARYHIGHPERFORMANCECOMPUTING:FROMPETASCALETOWARDEXASCALE

EditedbyJeffreyS.Vetter

CONTEMPORARYHIGHPERFORMANCECOMPUTING:FROMPETASCALETOWARDEXASCALE,VOLUMETWO

EditedbyJeffreyS.Vetter

DATA-INTENSIVESCIENCE

EditedbyTerenceCritchlowandKerstinKleesevanDam

ELEMENTSOFPARALLELCOMPUTING

EricAubanel

THEENDOFERROR:UNUMCOMPUTING

JohnL.Gustafson

EXASCALESCIENTIFICAPPLICATIONS:SCALABILITYANDPERFORMANCEPORTABILITY

Page 4: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

EditedbyTjerkP.Straatsma,TimothyJ.Williams,andKaterinaAntypas

FROMACTIONSYSTEMSTODISTRIBUTEDSYSTEMS:THEREFINEMENTAPPROACH

EditedbyLuigiaPetreandEmilSekerinski

FUNDAMENTALSOFMULTICORESOFTWAREDEVELOPMENT

EditedbyVictorPankratius,Ali-RezaAdl-Tabatabai,andWalterTichy

FUNDAMENTALSOFPARALLELMULTICOREARCHITECTURE

YanSolihin

THEGREENCOMPUTINGBOOK:TACKLINGENERGYEFFICIENCYATLARGESCALE

EditedbyWu-chunFeng

GRIDCOMPUTING:TECHNIQUESANDAPPLICATIONS

BarryWilkinson

HIGHPERFORMANCECOMPUTING:PROGRAMMINGANDAPPLICATIONS

JohnLevesquewithGeneWagenbreth

HIGHPERFORMANCEPARALLELI/O

PrabhatandQuinceyKoziol

HIGHPERFORMANCEVISUALIZATION:ENABLINGEXTREME-SCALESCIENTIFICINSIGHT

EditedbyE.WesBethel,HankChilds,andCharlesHansen

INDUSTRIALAPPLICATIONSOFHIGH-PERFORMANCECOMPUTING:BESTGLOBALPRACTICES

EditedbyAnwarOsseyranandMerleGiles

INTRODUCTIONTOCOMPUTATIONALMODELINGUSINGCANDOPEN-SOURCETOOLS

JoséMGarrido

INTRODUCTIONTOCONCURRENCYINPROGRAMMINGLANGUAGES

Page 5: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

MatthewJ.Sottile,TimothyG.Mattson,andCraigERasmussen

INTRODUCTIONTOELEMENTARYCOMPUTATIONALMODELING:ESSENTIALCONCEPTS,PRINCIPLES,ANDPROBLEMSOLVING

JoséM.Garrido

INTRODUCTIONTOHIGHPERFORMANCECOMPUTINGFORSCIENTISTSANDENGINEERS

GeorgHagerandGerhardWellein

INTRODUCTIONTOMODELINGANDSIMULATIONWITHMATLAB®ANDPYTHON

StevenI.GordonandBrianGuilfoos

INTRODUCTIONTOREVERSIBLECOMPUTING

KalyanS.Perumalla

INTRODUCTIONTOSCHEDULING

YvesRobertandFrédéricVivien

INTRODUCTIONTOTHESIMULATIONOFDYNAMICSUSING

SIMULINK®

MichaelA.Gray

PEER-TO-PEERCOMPUTING:APPLICATIONS,ARCHITECTURE,PROTOCOLS,ANDCHALLENGES

Yu-KwongRickyKwok

PERFORMANCETUNINGOFSCIENTIFICAPPLICATIONS

EditedbyDavidBailey,RobertLucas,andSamuelWilliams

PETASCALECOMPUTING:ALGORITHMSANDAPPLICATIONS

EditedbyDavidA.Bader

PROCESSALGEBRAFORPARALLELANDDISTRIBUTEDPROCESSING

EditedbyMichaelAlexanderandWilliamGardner

PROGRAMMINGFORHYBRIDMULTI/MANY-COREMPPSYSTEMS

JohnLevesqueandAaronVose

Page 6: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

SCIENTIFICDATAMANAGEMENT:CHALLENGES,TECHNOLOGY,ANDDEPLOYMENT

EditedbyArieShoshaniandDoronRotem

SOFTWAREENGINEERINGFORSCIENCE

EditedbyJeffreyC.Carver,NeilP.ChueHong,andGeorgeK.Thiruvathukal

Page 7: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

IntroductiontoModelingand

SimulationwithMATLAB®andPython

StevenI.GordonBrianGuilfoos

Page 8: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

MATLAB® and Simulink® are trademarks of theMathWorks, Inc. and are used with permission. TheMathWorks does not warrant the accuracy of the text or exercises in this book. This book’s use or

discussionofMATLAB®andSimulink®softwareorrelatedproductsdoesnotconstituteendorsementor

sponsorshipby theMathWorksofaparticularpedagogicalapproachorparticularuseof theMATLAB®

andSimulink®software.

CRCPressTaylor&FrancisGroup6000BrokenSoundParkwayNW,Suite300BocaRaton,FL33487-2742

©2017byTaylor&FrancisGroup,LLCCRCPressisanimprintofTaylor&FrancisGroup,anInformabusiness

NoclaimtooriginalU.S.Governmentworks

Printedonacid-freepaper

InternationalStandardBookNumber-13:978-1-4987-7387-4(Hardback)

Thisbookcontains informationobtained fromauthentic andhighly regarded sources.Reasonable effortshave been made to publish reliable data and information, but the author and publisher cannot assumeresponsibilityforthevalidityofallmaterialsortheconsequencesoftheiruse.Theauthorsandpublishershaveattemptedtotracethecopyrightholdersofallmaterialreproducedinthispublicationandapologizetocopyrightholdersifpermissiontopublishinthisformhasnotbeenobtained.Ifanycopyrightmaterialhasnotbeenacknowledgedpleasewriteandletusknowsowemayrectifyinanyfuturereprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced,transmitted,orutilizedinanyformbyanyelectronic,mechanical,orothermeans,nowknownorhereafterinvented, includingphotocopying,microfilming,andrecording,or inany informationstorageor retrievalsystem,withoutwrittenpermissionfromthepublishers.

For permission to photocopy or use material electronically from this work, please accesswww.copyright.com(http://www.copyright.com/)orcontact theCopyrightClearanceCenter, Inc. (CCC),222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization thatprovides licenses and registration for a variety of users. For organizations that have been granted aphotocopylicensebytheCCC,aseparatesystemofpaymenthasbeenarranged.

TrademarkNotice:Productorcorporatenamesmaybetrademarksorregisteredtrademarks,andareusedonlyforidentificationandexplanationwithoutintenttoinfringe.

VisittheTaylor&FrancisWebsiteathttp://www.taylorandfrancis.com

andtheCRCPressWebsiteat

Page 9: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

http://www.crcpress.com

Page 10: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Contents

PrefaceAuthors

CHAPTER1■IntroductiontoComputationalModeling

1.1THEIMPORTANCEOFCOMPUTATIONALSCIENCE

1.2HOWMODELINGHASCONTRIBUTEDTOADVANCESINSCIENCEANDENGINEERING

1.2.1SomeContemporaryExamples

1.3THEMODELINGPROCESS

1.3.1StepsintheModelingProcess

1.3.2MathematicalModelingTerminologyandApproachestoSimulation

1.3.3ModelingandSimulationTerminology

1.3.4ExampleApplicationsofModelingandSimulation

EXERCISES

REFERENCES

CHAPTER2■IntroductiontoProgrammingEnvironments

2.1THEMATLAB®PROGRAMMINGENVIRONMENT

2.1.1TheMATLAB®Interface

2.1.2BasicSyntax

2.1.2.1VariablesandOperators

2.1.2.2Keywords

2.1.2.3ListsandArrays2.1.3CommonFunctions

2.1.4ProgramExecution

Page 11: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

2.1.5CreatingRepeatableCode

2.1.6Debugging

2.2THEPYTHONENVIRONMENT

2.2.1RecommendationsandInstallation

2.2.2TheSpyderInterface

2.2.3BasicSyntax

2.2.3.1VariablesandOperators

2.2.3.2Keywords

2.2.3.3ListsandArrays2.2.4LoadingLibraries

2.2.5CommonFunctions

2.2.6ProgramExecution

2.2.7CreatingRepeatableCode

2.2.8Debugging

EXERCISES

CHAPTER3■DeterministicLinearModels

3.1SELECTINGAMATHEMATICALREPRESENTATIONFORAMODEL

3.2LINEARMODELSANDLINEAREQUATIONS

3.3LINEARINTERPOLATION

3.4SYSTEMSOFLINEAREQUATIONS

3.5LIMITATIONSOFLINEARMODELS

EXERCISES

REFERENCES

CHAPTER4■ArrayMathematicsinMATLAB®andPython

4.1INTRODUCTIONTOARRAYSANDMATRICES

4.2BRIEFOVERVIEWOFMATRIXMATHEMATICS

4.3MATRIXOPERATIONSINMATLAB®

4.4MATRIXOPERATIONSINPYTHON

EXERCISES

Page 12: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

CHAPTER5■Plotting

5.1PLOTTINGINMATLAB®

5.2PLOTTINGINPYTHON

EXERCISES

CHAPTER6■ProblemSolving

6.1OVERVIEW

6.2BOTTLEFILLINGEXAMPLE

6.3TOOLSFORPROGRAMDEVELOPMENT

6.3.1Pseudocode

6.3.2Top–DownDesign

6.3.3Flowcharts

6.4BOTTLEFILLINGEXAMPLECONTINUED

EXERCISES

CHAPTER7■ConditionalStatements

7.1RELATIONALOPERATORS

7.2LOGICALOPERATORS

7.3CONDITIONALSTATEMENTS

7.3.1MATLAB®

7.3.2Python

EXERCISES

CHAPTER8■IterationandLoops

8.1FORLOOPS

8.1.1MATLAB®Loops

8.1.2PythonLoops

8.2WHILELOOPS

8.2.1MATLAB®WhileLoops

8.2.2PythonWhileLoops

8.3CONTROLSTATEMENTS

Page 13: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

8.3.1Continue

8.3.2Break

EXERCISES

CHAPTER9■NonlinearandDynamicModels

9.1MODELINGCOMPLEXSYSTEMS

9.2SYSTEMSDYNAMICS

9.2.1ComponentsofaSystem

9.2.2UnconstrainedGrowthandDecay

9.2.2.1UnconstrainedGrowthExercises9.2.3ConstrainedGrowth

9.2.3.1ConstrainedGrowthExercise9.3MODELINGPHYSICALANDSOCIALPHENOMENA

9.3.1SimpleModelofTossedBall

9.3.2ExtendingtheModel

9.3.2.1BallTossExerciseREFERENCES

CHAPTER10■EstimatingModelsfromEmpiricalData

10.1USINGDATATOBUILDFORECASTINGMODELS

10.1.1LimitationsofEmpiricalModels

10.2FITTINGAMATHEMATICALFUNCTIONTODATA

10.2.1FittingaLinearModel

10.2.2LinearModelswithMultiplePredictors

10.2.3NonlinearModelEstimation

10.2.3.1LimitationswithLinearTransformation

10.2.3.2NonlinearFittingandRegression

10.2.3.3SegmentationEXERCISES

FURTHERREADINGS

REFERENCES

Page 14: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

CHAPTER11■StochasticModels

11.1INTRODUCTION

11.2CREATINGASTOCHASTICMODEL

11.3RANDOMNUMBERGENERATORSINMATLAB®ANDPYTHON

11.4ASIMPLECODEEXAMPLE

11.5EXAMPLESOFLARGERSCALESTOCHASTICMODELS

EXERCISES

FURTHERREADINGS

REFERENCES

CHAPTER12■Functions

12.1MATLAB®FUNCTIONS

12.2PYTHONFUNCTIONS

12.2.1FunctionsSyntaxinPython

12.2.2PythonModules

EXERCISES

CHAPTER13■Verification,Validation,andErrors

13.1INTRODUCTION

13.2ERRORS

13.2.1AbsoluteandRelativeError

13.2.2Precision

13.2.3TruncationandRoundingError

13.2.4ViolatingNumericAssociativeandDistributiveProperties

13.2.5AlgorithmsandErrors

13.2.5.1Euler’sMethod

13.2.5.2Runge–KuttaMethod

13.2.6ODEModulesinMATLAB®andPython

13.3VERIFICATIONANDVALIDATION

13.3.1HistoryandDefinitions

13.3.2VerificationGuidelines

Page 15: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

13.3.3ValidationGuidelines

13.3.3.1QuantitativeandStatisticalValidationMeasures

13.3.3.2GraphicalMethodsEXERCISES

REFERENCES

CHAPTER14■CapstoneProjects

14.1INTRODUCTION

14.2PROJECTGOALS

14.3PROJECTDESCRIPTIONS

14.3.1DrugDosageModel

14.3.2MalariaModel

14.3.3PopulationDynamicsModel

14.3.4SkydiverProject

14.3.5SewageProject

14.3.6EmpiricalModelofHeartDiseaseRiskFactors

14.3.7StochasticModelofTraffic

14.3.8OtherProjectOptions

REFERENCE

INDEX

Page 16: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

M

Preface

ODELINGANDSIMULATIONUSINGcomputationorcomputationalsciencehasbecomeanessentialpartof theresearchanddevelopmentprocess in the

physical, biological, and social sciences and engineering. It allows theexplorationofphysicalandbiologicalsystemsatthemicro-andmolecularlevelthat increase our understanding of their function and the discovery of newmaterials and new drugs. It allows us to understand the interactions ofcomponents in complex systems from those we engineer and build to ourecosystems and climate. In recent years, computational science has producedenormous advances in almost all fields of scientific and technological inquiry,including DNA sequencing, behavioral modeling, global climatic predictions,drugdesign, financial systems, andmedicalvisualization.At the same time, ithas become critical in the design, testing, andmanufacturing of new productsand services, savingmillions of dollars in development costs and getting newproductstomarketmorerapidly.

Scientists,socialscientists,andengineersmusthaveanunderstandingofbothmodelingandcomputerprogrammingprinciplessothattheyappropriatelyapplythose techniques in their practice. Several sets of knowledge and skills arerequired to achieve that understanding. How dowe translate the relationshipswithin a system being modeled into a set of mathematical functions thataccurately portray the behavior of that system? How are the mathematicstranslatedintocomputercodethatcorrectlysimulatesthoserelationships?Whatis the nature of errors introduced by simplifying the depiction of the system,introducedbythecomputeralgorithmusedtosolvetheequations,andlimitedbyourknowledgeofthesystembehavior?Howaccurateisthemodel?Howdoweknow the model is logically correct and follows from the physical andmathematicallawsusedtocreateit(verification)?Howdowedemonstratethatthemodelcorrectlypredictsthephenomenamodeled(validation)?Thesearetheunderlyingquestionsthatarethefocusofthisbook.

Thebookisintendedforstudentsandprofessionalsinscience,socialscience,andengineeringwhowishtolearntheprinciplesofcomputermodelingaswell

Page 17: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

as basic programming skills. For many students in these fields, with theexception of computer science students and some engineering students,enrollment in an introductory programming course may be impractical ordifficult.Atmanyinstitutions,thesecoursesarefocusedprimarilyoncomputersciencemajorsanduseaprogramminglanguagesuchasJavathatisnotreadilyapplicable to science and engineering problems.We have found that teachingprogramming as a just-in-time tool used to solve real problems more deeplyengages those students to master the programming concepts. Combining thateffortwithlearningtheprinciplesofmodelingandsimulationprovidesthelinkbetweenprogrammingandproblemsolvingwhilealsofittingmorereadilyintoacrowdedcurriculum.

For students from all fields, learning the basic principles of modeling andsimulation prepares them for understanding and using computer modelingtechniquesthatarebeingappliedtoamyriadofproblems.Theknowledgeofthemodeling process should provide the basis for understanding and evaluatingmodelsintheirownsubjectdomain.Thebookcontentfocusesonmeetingasetofbasicmodelingandsimulationcompetencies thatweredevelopedaspartofseveral National Science Foundation grants (seehttp://hpcuniversity.org/educators/undergradCompetencies/). Even thoughcomputer science students are much more expert programmers, they are notoften given the opportunity to see how those skills are being applied to solvecomplex science and engineering problems, andmay also not be aware of thelibrariesusedbyscientiststocreatethosemodels.

WehavechosentouseMATLAB®andPythonforseveralreasons.First,bothofferinterfacesthattheintendedaudienceshouldfindintuitive.Bothinterfacesprovide instant feedback on syntax errors and extensive help documents andtutorials that are important for novice programmers. AlthoughMATLAB is acommerciallylicensedprogram,whereasPythonisopensource,manycampusescurrently have a site license for MATLAB. Students can also purchase thestudentversionofMATLABrelativelycheaply.

Perhapsmostimportantly,bothprogramsareextensivelyusedbythescienceand engineering community for model development and testing. Even thoughneitherprogramscalesasefficientlyasC,Fortran,orotherlanguagesforlarge-scale modeling on current parallel computing architectures, they do offer asteppingstonetothoseenvironments.Bothhaveextensivetoolkitsandscientificandmathematical libraries thatcanbe invokedtoreduce theamountofcodingrequired to undertake many modeling projects. Although we use these

Page 18: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

programming environments to teach rudimentary programming techniqueswithoutapplyingalargenumberofthesetools,theyareavailabletostudentsfordevelopingcapstoneprojectsorforuseinmoreadvancedcourseslater intheircurriculum.

ORGANIZATIONOFTHEBOOK

Thebookinterleaveschaptersonmodelingconceptsandrelatedexerciseswithprogramming concepts and exercises. We start out with an introduction tomodelinganditsimportancetocurrentpracticesinthesciencesandengineering.Wethenintroduceeachof theprogrammingenvironmentsandthesyntaxusedto represent variables and compute mathematical equations and functions. Asstudents gainmore programming expertise,we go back tomodeling concepts,providingstartingcodeforavarietyofexerciseswherestudentsaddadditionalcodetosolvetheproblemandprovideananalysisoftheoutcomes.Inthisway,we build both modeling and programming expertise with a “just-in-time”approachsothatbytheendofthebook,studentscantakeonrelativelysimplemodelingexampleontheirown.

Eachchapterissupplementedwithreferencestoadditionalreading,tutorials,andexercises thatguidestudents toadditionalhelpandallow them topracticeboththeirprogrammingandanalyticalmodelingskills.Thecompanionwebsiteathttp://www.intromodeling.comprovidesupdatestoinstructionswhentherearesubstantialchangesinsoftwareversionsaswellaselectroniccopiesofexercisesand the related code. Solutions to the computer exercises are available toinstructorsonthepublisher’swebsite.

Eachoftheprogramming-relatedchaptersisdividedintotwoparts—oneforMATLABandoneforPython.Weassumethatmostinstructorswillchooseoneortheothersothatstudentscanfocusonlyonthelanguageassociatedwiththeircourse.Inthesechapters,wealsorefertoadditionalonlinetutorialsthatstudentscanuseiftheyarehavingdifficultywithanyofthetopics.

The book culminates with a set of final project exercise suggestions thatincorporateboththemodelingandtheprogrammingskillsprovidedintherestofthevolume.Theseprojectscouldbeundertakenbyindividualsorsmallgroupsofstudents.Theygenerallyinvolveresearchintoaparticularmodelingproblemwithsuggestedbackgroundreadingfromtheliterature.Eachexercisehasasetofstartingcodeprovidingaverysimplisticviewofthesystemandsuggestionsforextending the model by adding additional components to relax some of the

Page 19: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

assumptions. Students then complete the program code and use the model toansweranumberofquestionsaboutthesystem,completemodelverificationandvalidationwherepossible,andpresentareportinwrittenandoralform.

Thewebsitealsooffersaspacewherepeoplecansuggestadditionalprojectsthey are willing to share as well as comments on the existing projects andexercises throughout the book. We hope that the combination of materialscontributestothesuccessofthoseinterestedingainingmodelingandsimulationexpertise.

MATLAB® is a registered trademark of The MathWorks, Inc. For productinformation,pleasecontact:

TheMathWorks,Inc.3AppleHillDriveNatick,MA01760-2098USATel:5086477000Fax:508-647-7001E-mail:[email protected]:www.mathworks.com

Page 20: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Authors

StevenI.GordonisaprofessoremeritusoftheCityandRegionalPlanningandEnvironmentalScienceProgramsattheOhioStateUniversity,Columbus,Ohio.He earned a bachelor’s degree from the University at Buffalo, Buffalo, NewYork,in1966andaPhDdegreefromColumbiaUniversity,NewYork,in1977.HealsoservesasthesenioreducationleadattheOhioSupercomputerCenter.Inthat and other roles at OSC, he has focused primarily on the integration ofcomputationalscienceintothecurriculaathighereducationinstitutionsinOhioand throughout the United States. He has worked with multiple institutionsthroughavarietyofgrantsfromtheNationalScienceFoundation,includingtheExtremeScience andEngineeringDiscoveryEnvironment (XSEDE) andBlueWatersproject.

Dr.Gordon is alsooneof the founders and first chair of theAssociationofComputing Machinery (ACM) Special Interest Group High PerformanceComputing (SIGHPC)EducationChapter and serves as a representativeof theSIGHPC on the ACM Education Council. He has published extensively ontopics related to environmental planning and the applications ofmodeling andsimulationineducationandresearch.

Brian Guilfoos serves as the High Performance Computing (HPC) ClientServicesmanagerfor theOhioSupercomputerCenter(OSC),Columbus,Ohio.GuilfoosleadstheHPCClientServicesGroup,whichprovidestrainingandusersupport to facilitate the use of computational science by the center’s usercommunities.Heearnedamaster’sdegreeinpublicpolicyandadministrationin2014 and a bachelor’s degree in electrical engineering in 2000, both from theOhioStateUniversity,Columbus,Ohio.HealsoworksdirectlywithOSCclientstohelpconvertcomputercodesanddevelopbatchscripting,compiling,andcodedevelopment so that these researchers can efficiently use the center’ssupercomputersandlicensedsoftware.

GuilfoosdevelopedanddeliveredtraininginMATLAB®asapartoftheU.S.

Page 21: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Department ofDefenseHigh PerformanceComputingModernization Programsupport.

PriortojoiningOSC,hewascontractedbytheAirForceResearchLaboratory(AFRL)tofocusonsoftwaredevelopmentinsupportofunmannedaerialvehicleinterfaceresearch.Hewasakeytechnicalmemberofateamthatwasawardedthe 2004 Scientific and Technological Achievement Award by the AFRL’sHumanEffectivenessDirectorate.

Page 22: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

CHAPTER1IntroductiontoComputationalModeling

1.1THEIMPORTANCEOFCOMPUTATIONALSCIENCE

Advances in science and engineering have come traditionally from theapplication of the scientificmethod using theory and experimentation to poseand test our ideas about the nature of our world from multiple perspectives.Through experimentation and observation, scientists develop theories that arethen testedwithadditional experimentation.Thecauseandeffect relationshipsassociated with those discoveries can then be represented by mathematicalexpressionsthatapproximatethebehaviorofthesystembeingstudied.

Withtherapiddevelopmentofcomputers,scientistsandengineerstranslatedthosemathematicalexpressionsintocomputercodesthatallowedthemtoimitatetheoperationof the systemover time.Thisprocess is called simulation.Earlycomputersdidnothave the capabilityof solvingmanyof the complex systemsimulationsofinteresttoscientistsandengineers.Thisledtothedevelopmentofsupercomputers,computerswithhigherlevelcapacityforcomputationcomparedto the general-purpose computers of the time. In 1982, a panel of scientistsprovideda report to theU.S.DepartmentofDefenseand theNationalScienceFoundationurgingthegovernmenttoaidinthedevelopmentofsupercomputers(Lax, 1982). They indicated that “the primacy of the U.S. in science,engineering, and computing technology could be threatened relative to that ofothercountrieswithnationaleffortsinsupercomputeraccessanddevelopment.”They recommended both investments in research and development and in thetrainingofpersonnelinscienceandengineeringcomputing.

Thecapabilityofthecomputerchipsinyourcellphonetodayfarexceedsthatofthesupercomputersofthe1980s.TheCray-1supercomputerreleasedin1975

Page 23: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

hada rawcomputingpowerof80million floating-pointoperationspersecond(FLOPS). The iPhone 5s has a graphics processor capable of 76.8 Gigaflops,nearly one thousand times more powerful (Nick, 2014). With that growth incapability, there has been a dramatic expansion in the use of simulation forengineeringdesignandresearchinscience,engineering,socialscience,andthehumanities. Over the years, that has led to many efforts to integratecomputational science into the curriculum, to calls for development of aworkforce prepared to apply computing to both academic and commercialpursuits, and to investments in the computer and networking infrastructurerequired tomeet the demands of those applications. For example, in 2001 theSocietyforIndustrialandAppliedMathematics(SIAM)providedareviewofthegraduate education programs in science and engineering (SIAM, 2001). Theydefined computational science and engineering as a multidisciplinary fieldrequiringexpertiseincomputerscience,appliedmathematics,andasubjectfieldof science and engineering.They provided examples of emerging research, anoutlineofacurriculum,andcurriculumexamplesfrombothNorthAmericaandEurope.

YasarandLandau(2001)providedasimilaroverviewoftheinterdisciplinarynatureofthefield.Theyalsodescribethepossiblescopeofprogramsattheboththeundergraduateandgraduatelevelsandprovideasurveyofexistingprogramsandtheircontent.Morerecently,Gordonetal.(2008)describedthecreationofacompetency-basedundergraduateminor program in computational science thatwas put into place at several institutions in Ohio. The competencies weredevelopedbyaninterdisciplinarygroupoffacultyandreviewedbyanindustryadvisorycommitteefromtheperspectiveoftheskillsthatprospectiveemployersare looking for in students entering the job market. The competencies haveguided the creation of several other undergraduate programs. They have alsobeen updated and augmented with graduate-level computational sciencecompetenciesandcompetenciesfordata-drivenscience.Themostrecentversionof those competencies can be found on the HPC University website (HPCUniversity,2016).

More recently, there have been a number of national studies and panelsemphasizingtheneedfortheinfrastructureandworkforcerequiredtoundertakelarge-scalemodelingandsimulation(CouncilonCompetitiveness,2004;Josephet al., 2004;Reed, 2005; SBES, 2006). This book provides an introduction tocomputational science relevant to students across the spectrum of science andengineering. In this chapter, we begin with a brief review of the history or

Page 24: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

computationalmodelinganditscontributionstotheadvancementofscience.Wethen provide an overview of the modeling process and the terminologyassociatedwithmodelingandsimulation.

As we progress through the book, we guide students through basicprogrammingprinciplesusing twoof thewidelyusedsimulationenvironments

—MATLAB® and Python. Each chapter introduces either a new set ofprogrammingprinciplesorappliesthemtothesolutionofoneclassofmodels.Eachchapterisaccompaniedbyexercisesthathelptobuildbothbasicmodelingand programming skills that will provide a background for more advancedmodelingcourses.

1.2HOWMODELINGHASCONTRIBUTEDTOADVANCESINSCIENCEANDENGINEERING

Thereareamyriadofexamplesdocumentinghowmodelingandsimulationhascontributed to research and to the design and manufacture of new products.Here, we trace the history of computation andmodeling to illustrate how thecombination of advances in computing hardware, software, and scientificknowledge has led to the integration of computational modeling techniquesthroughout the sciences and engineering.We then provide a few,more recentexamplesofadvancestofurtherillustratethestate-of-the-art.Oneexerciseattheend of the chapter provides an opportunity for students to examine additionalexamplesandsharethemwiththeirclassmates.

The first electronic programmable computer was the ENIAC built for thearmy toward the end ofWorldWar II as a way to quickly calculate artillerytrajectories. Herman Goldstine (1990), the project leader, and two professorsfromtheUniversityofPennsylvania,J.PresperEckert,andJohnMauchlysoldtheideatothearmyin1942(McCartney,1999).Asthemachinewasbeingbuiltandtested,alargeteamofengineersandmathematicianswasassembledtolearnhowtouseit.Thatincludedsixwomenmathematicianswhowererecruitedfromcollegesacrossthecountry.Asthemachinewascompletedin1945,thewarwasnearanend.

ENIACwas used extensively by themathematician John vonNeumann notonly toundertake itsoriginalpurposes for thearmybutalso tocreate the firstweather model in 1950. That machine was capable of 400 floating-pointoperationspersecondandneeded24hourstocalculatethesimpledailyweather

Page 25: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

model for North America. To provide a contrast to the power of currentprocessors,PeterandOwenLynch(2008)createdaversionofthemodelthatranonaNokia6300mobilephoneinlessthanonesecond!

Itisimpossibletodocumentallofthechangesincomputationalpoweranditsrelationship to the advancements in science that have occurred since this firstcomputer.Tables 1.1 and 1.2 show a timeline of the development of selectedmajor hardware advances, software and algorithm development, and scientificapplicationsfromafewfields.LookingatthefirstcolumninTable1.1,onecansee the tremendous growth in the power of the computers used in large-scalescientific computation. Advances in electronics and computer design havebroughtusfromtheENIACwith400flopstoBlueWaterswith13.34petaflops,an increase in themaximumnumberoffloating-pointoperationspersecondofmorethan1015!

TABLE1.1TimelineofAdvancesinComputerPowerandScientificModeling(Part1)ExampleHardware Max.Speed Date WeatherandClimateModelingENIAC 400Flops 1945

1950 FirstautomaticweatherforecastsUNIVAC 1951IBM704 12KFLOP 1956

1959 EdLorenzdiscoversthechaoticbehaviorofmeteorologicalprocessesIBM7030Stretch;UNIVACLARC 500-500KFLOP ~1960

1965 GlobalclimatemodelingunderwayCDC6600 1Megaflop 1966CDC7600 10MFLOP 1975CRAY1 100MFLOP 1976CRAY-X-MP 400MFLOP

1979 JuleCharneyreporttoNASCRAYY-MP 2.67GFLOP

1988 IntergovernmentalPanelonClimateChange1992 UNFCCCinRio

IBMSP2 10Gigaflop 1994ASCIIRed 2.15TFLOP 1995 CoupledModelIntercomparisonProject(CMIP)

2005 EarthsystemmodelsBlueWaters 13.34PFLOP 2014

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(accessedDecember15,2016).Bell, T., Supercomputer timeline, 2016, https://mason.gmu.edu/~tbell5/page2.html (accessedDecember15,2016).Esterbrook,S.,Timelineofclimatemodeling,2015,https://prezi.com/pakaaiek3nol/timeline-of-climate-modeling/(accessedDecember15,2016).

TABLE1.2TimelineofAdvancesinComputerPowerandScientificModeling(Part2)

Page 26: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Date TheoreticalChemistry AeronauticsandStructures SoftwareandAlgorithms1950 Electronicwavefunctions1951 Molecularorbitaltheory(Roothan)

1953 Oneofthefirstmolecularsimulations(Metropolisetal.)

1954 Vectorprocessingdirectives

1956 FirstcalculationofmultipleelectronicstatesofamoleculeonEDSAC(Boys)

1957 FORTRANcreated1965 Creationofabinitiomolecularmodeling(People)

1966 2DNavier-Stokessimulations;FLO22;transonicflowoverasweptwing

1969 UNIXcreated1970 2DInviscidFlowModels;designofregionaljet1971 Nastran(NASAStructuralAnalysis)1972 Cprogramminglanguagecreated

1973 Matrixcomputationsanderrors(Wilkinson)

1975 3DInviscidFlowModels;completeairplanesolution

1976 Firstcalculationofachemicalreaction(Warshel) DYNA3DwhichbecameLS-DYNA(mid-70s)1977 Firstmoleculardynamicsofproteins(Karplus) Boeingdesignof737-500

Firstcalculationofareactiontransitionstate(Chandler)

1979 BasicLinearAlgebraSubprograms(BLAS)librarylaunched

1980s JournalofComputationalChemistryfirstpublished

800,000meshcellsaroundawing,FLO107

1984 MATLABcreated

1985 DesignofBoeing767,777 GNUprojectlaunched(freeSoftwarefoundation)

1991 Linuxlaunched

1993 Messagepassinginterface(MPI)specification

1994 Pythoncreated

1995 Firstsuccessfulcomputer-baseddrugdesign(Kubinyi)

1997 Openmultiprocessing(OpenMP)specification

2000 Discontinuousfiniteelementmethods;turbulentflow;designofairbus

2007 CUDAlaunched

2014 Openaccelerator(OpenACC)specification

Sources:Bartlett,B.N.,ThecontributionsofJ.H.Wilkinsontonumericalanalysis.InS.G.Nash,(Ed.),AHistoryofScientificComputing,ACMPress,NewYork,pp.17–30,1990.Computer History Museum, Timeline of computer history, software and languages, 2017,http://www.computerhistory.org/timeline/software-languages/(accessedJanuary2,2017).Dorzolamide,2016,https://en.wikipedia.org/wiki/Dorzolamide(accessedDecember15,2016).Jameson, A., Computational fluid dynamics, past, present, and future, 2016, http://aero-comlab.stanford.edu/Papers/NASA_Presentation_20121030.pdf(accessedDecember15,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(accessedDecember15,2016).Vassberg,J.C.,AbriefhistoryofFLO22,http://dept.ku.edu/~cfdku/JRV/Vassberg.pdf (accessedDecember15,2016).

Page 27: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Tracingweather and climatemodeling from vonNeumann’s firstmodel onENIAC,wecanseethatthecomputationalpowerhasallowedscientiststomakerapidprogressintherepresentationofweatherandclimate.In1959,Lorenzlaidthe foundation for the mathematics behind weather events. By 1965, furtheradvances in computing power and scientific knowledge provided the basis forthefirstglobalclimatemodels.Thesehavegrowninscopetothepresentdaytoearthsystemmodelsthatcoupleatmosphericandoceancirculationthatprovideforthebasisfortheclimatechangeforecastsoftheinternationalcommunity.

Table 1.2 documents similar developments in computational chemistry,aeronautics and structures, and selected achievements in software andalgorithms. The scientific advances were made possible not only byimprovements in the hardware but also by the invention of programminglanguages,compilers,andthealgorithmsthatareusedtomakethemathematicalcalculationsunderlyingthemodels.Aswithweathermodeling,onecantracetheadvancementofcomputationalchemistryfromthefirstsimulationofmoleculesto the screening of drugs by modeling their binding to biomolecules. Inaeronautics, the simulation of airflow over a wing in two dimensions hasadvancedtothethree-dimensionalsimulationofafullairplanetocreateafinaldesign. Similar timelines could be developed for every field of science andengineering from various aspects of physics and astronomy to earth andenvironmental science, to every aspect of engineering, and to economics andsociologicalmodeling.

For those just getting introduced to these concepts, the terminology isdaunting.Thelessonatthispointistounderstandthatcomputationhasbecomean essential part of the design and discovery process across a wide range ofscientific fields. Thus, it is essential that everyone understands the basicprinciples used in modeling and simulation, the mathematics underlyingmodelingefforts,andthetoolsofmodelingalongwiththeirpitfalls.

1.2.1SomeContemporaryExamples

Although this book will not involve the use of large-scale models onsupercomputers, some contemporary examples of large-scale simulations mayprovide insights into the need for the computational power described inTable1.1.Weprovidefoursuchexamples.

Vogelsberger et al. created a model of galaxy formation comprised of 12billion resolution elements showing the evolution of the universe from 12

Page 28: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

million years after the Big Bang evolving over a period of 13.8 billion years(Vogelsberger et al., 2014).The simulationproduceda largevarietyofgalaxyshapes, luminosities, sizes, and colors that are similar to observed population.The simulation provided insights into the processes associated with galaxyformation. This example also illustrates how computation can be applied to asubjectwhereexperimentationisimpossiblebutwheresimulationresultscanbecomparedwithscientificobservations.

Drugscreeningprovidesanexampleofhowcomputermodelingcanshortenthetimetodiscovery.Thedrugscreeningpipelinerequiresamodelofa targetprotein or macromolecular structure that is associated with a specific diseasemechanism.Alistofpotentialcandidatecompoundsisthentestedtoseewhichhavethehighestaffinitytobindtothatprotein,potentiallyinhibitingthemedicalproblem.Biesiadaetal. (2012)provideanexcellentoverviewof theworkflowassociated with this process and the publically available software foraccomplishing those tasks.Theuse of these tools allows researchers to screenthousandsofcompoundsfortheirpotentialuseasdrugs.Thecandidatelistcanthen be pared down to only a few compounds where expensive experimentaltestingisused.

The reports on global warming use comprehensive models of the earth’sclimate including components on the atmosphere and hydrosphere (oceancirculation and temperature, rainfall, polar ice caps) to forecast the long-termimpacts on our climate and ecosystems (Pachauri and Meyer, 2014). Themodels:

reproduce observed continental-scale surface temperature patterns andtrendsovermanydecades,includingthemorerapidwarmingsincethemid-20th century and the cooling immediately following large volcaniceruptions(veryhighconfidence)(IPC,2013,p.15).

Modeling and simulation has also become a key part of the process anddesigning, testing,andproducingproductsandservices.Where thebuildingofphysicalprototypesorthecompletionoflaboratoryexperimentsmaytakeweeksor months and cost millions of dollars, industry is instead creating virtualexperiments that can be completed in a short time at greatly reduced costs.ProctorandGambleusescomputermodeling to improvemanyof itsproducts.One example is the use ofmolecularmodeling to test the interactions amongsurfactantsintheircleaningproductswithagoalofproducingproductsthatare

Page 29: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

environmentally friendly and continue to perform as desired (Council onCompetiveness,2009).

Automobile manufacturers have substituted modeling for the building ofphysical prototypes of their cars to save time and money. The building ofphysicalprototypescalledmules is expensive, costing approximately$500,000for each vehicle with 60 prototypes required before going into production(Mayne,2005).Thedesignofthe2005ToyotaAvalonrequirednomulesatall—using computer modeling to design and test the car. Similarly, all of theautomobile manufacturers are using modeling to reduce costs and get newproductstomarketfaster(Mayne,2005).

These examples should illustrate the benefits of using modeling andsimulation as part of the research, development, and design processes forscientists and engineers. Of course, students new to modeling and simulationcannotbeexpectedtoeffectivelyusecomplex,large-scalesimulationmodelsonsupercomputers at the outset of their modeling efforts. They must firstunderstandthebasicprinciplesforcreating,testing,andusingmodelsaswellassomeoftheapproachestoapproximatingphysicalrealityincomputercode.WebegintodefinethoseprinciplesinSection1.3andcontinuethroughsubsequentchapters.

1.3THEMODELINGPROCESS

Basedon the examples discussed earlier, it shouldbe clear that amodel is anabstractionorsimplificationofareal-worldobjectorphenomenonthathelpsusgaininsightsintothestateorbehaviorofacomplexsystem.Eachofuscreatesinformal,mentalmodelsallthetimeasanaidtomakingdecisions.Oneexamplemaybedecidingonatravelroutethatgetsustoseveralshoppinglocationsfasterorwith the fewest traffic headaches.Todo this,we analyze information fromprevious trips tomake an informed decision aboutwhere theremay be heavytraffic,construction,orotherimpedimentstoourtrip.

Some of our first formal models were physical models. Those includesimplified prototypes of objects used to evaluate their characteristics andbehaviors.Forexample,automanufacturersbuiltclaymodelsofnewcardesignstoevaluatethestylingandtotestthedesigninwind-tunnelexperiments.Oneofthemostambitiousphysicalmodelseverbuiltwasacostly200acremodeloftheMississippiRiverBasinusedtosimulatefloodinginthewatershed(U.S.Army

Page 30: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

CorpsofEngineers,2006).AphotoofaportionofthismodelisshowninFigure1.1. It included replicas of urban areas, the (Fatherree, 2006) stream bed, theunderlying topography, levees, and other physical characteristics. Specialmaterialswereusedtoallowfloodsimulationstobetestedandinstrumented.

FIGURE1.1PhotoofportionofMississippiRiverBasinmodel.

Throughtheoryandexperimentation,scientistsandengineersalsodevelopedmathematicalmodelsrepresentingaspectsofphysicalbehaviors.Thesebecamethe basis of computer models by translating the mathematics into computercodes. Over time, mathematical models that started as very simplisticrepresentationsofcomplexsystemshaveevolvedintosystemsofequationsthatmorecloselyapproximatereal-worldphenomenasuchasthelarge-scalemodelsdiscussedearlierinthischapter.

Page 31: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Creating, testing, and applying mathematical models using computationrequireaniterativeprocess.Theprocessstartswithaninitialsetofsimplifyingassumptionsandisfollowedbytesting,alteration,andapplicationofthemodel.ThosestepsarediscussedinSection1.3.1.

1.3.1StepsintheModelingProcess

Agreatdealofworkmustbedonebeforeonecanbuildamathematicalmodelonacomputer.Figure1.2illustratesthestepsinthemodelingprocess.Thefirststepistoanalyzetheproblemanddefinetheobjectivesofthemodel.Thisstepshould include a review of the literature to uncover previous research on thetopic,experimentalorfield-measureddatashowingvariousstatesofthesystemandthemeasuredoutcomes,mathematicalrepresentationsofthesystemderivedfromtheories,andpreviousmodelingefforts.

As that information is being gathered, it is also important to define theobjectives of the modeling effort. There are several questions that should beaddressedwhile considering themodelobjectives:What are theoutcomes thatwewouldlikethemodeltopredict?Areweinterestedineverypossibleoutcomeor is therea subsetof conditions thatwould satisfyourmodelobjectives?Forexample,we could be interested in just theaverage ornormal state of affairsassociatedwith a phenomenon or potential extreme eventsmay be critical forouranalysis.Whatlevelofaccuracyisrequiredforthepredictedoutcomes?Thiswillimpactthenatureofthesimplifyingassumptions,inputdata,andcomputingalgorithmsthatarerequiredtobuildthemodel.

Page 32: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

FIGURE1.2Majorstepsinthemodelingprocess.

Thesecondstepintheprocessistocreateaconceptualmodelofthesystembasedontheanalysisinthefirststep.Aconceptualmodelwillbegintospecifyallof thecauseandeffect relationships in the system, informationon thedatarequiredandavailabletoimplementamodel,andreferencestodocumentsthatwere found in the initial analysis. The conceptual model should include aconcept map showing the cause and effect relationships associated with themodeland tables showing thedifferentvariables,data sources, and references.This can be done on a whiteboard, pencil and paper, or using a formalflowchartingor concept-mapping tool.Thereare several free tools for conceptmapping.Cmapprovidesafreeconcept-mappingtooldevelopedbytheFloridaInstituteforHumanandMachineCognition.Itcreatesnodesrepresentingmajor

Page 33: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

components of a concept and labels the links between nodes with theirrelationships (Cmap, 2016). Mind Map Maker is a free mind-mapping toolprovidedasanappforGoogleChromeusers(Mindmapmaker,2016).Thistoolallowsonetocreatelinksbetweenassociateditems.Therearealsoanumberofcommercialpackagesinbothcategories.

Figures1.3 and1.4 are examples of a partially completed conceptmap andmindmapshowingthecomponentsofamodelofthetimeittakestomakeacartripbetweentwopoints.

FIGURE1.3PartialconceptmapofmodeltocalculatetraveltimeusingCmap.

FIGURE1.4Partialmindmapofmodeltocalculatetraveltimeusingmindmapmaker.

Page 34: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Theaveragespeedacrossaroadsegmentisslowedbyparkedcarsandtrafficcontrol deviceswhilewider lanes and higher speed limits take less time. Thetotal time for a trip would need to add the average times associated withtraversing each road segment. Thus, data on each segment will be needed asinput to themodel. Simple versions of such estimates are provided by globalpositioningsatellite (GPS)equipmentor the Internetmappingservices thatareavailableonline.Therearemanyotherconditionsthatwouldimpactthissystem.Modelingtrafficconditionsareatopicofoneoftheexercisesattheendofthechapter.

Going back to Figure 1.2, onemust choosewhich simplifying assumptionscanbemadeinamodel.This,inturn,leadstoaselectionofthedatathatwouldbe needed, the variables that will drive the model, and the equations andmathematicalfunctionsthatwillcomprisethemodel.

Oncetheseitemshavebeendefined,acomputerversionofthemodelcanbecreated and tested. The results must be verified to ascertain that the code isworking properly. If the model is giving unexpected results with the codeworkingproperly,theremaybeaneedtoreexaminethesimplifyingassumptionsandtoreformulatethemodel.Thus,onemaygothroughseveraliterationsuntilthemodelisprovidingsufficientlyaccurateresults.Thiscanbevalidatedagainstavailable experimental or field data to provide a quantitative assessment ofmodel accuracy. Finally, the model can be used to undertake more detailedanalysisandtheresultsreported.Astimegoeson,themodelmustbemaintainedandmaybeimprovedbyrelaxingmoreoftheassumptionsand/orimprovingtheinput data. It should be noted that the judgment ofwhether amodel is givingreasonableresultsissometimesasmuchanartasascience.Confidenceinthatjudgment is a function of the experience of the modeler and the breadth anddepthofthepreviousresearchaboutthesystemunderstudy.Ofcoursethebestvalidationofmodelingresultscomesfromcomparisonswithrealdatagatheredfromobservationsorexperiments.

1.3.2MathematicalModelingTerminologyandApproachestoSimulation

Similar to all scientificdisciplines,mathematicalmodelinghas its ownuniquevocabulary.Modelingnovicesmaybelievethatthelanguageusedjustcreatesasmoke screen that hides any problems associatedwith amodel’s developmentanduse.Unfortunately,sometimesthereistruthinthatbelief.Nevertheless,itisimportant to learn that language to enable a critical understanding of the

Page 35: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

modeling literature. We will begin with some basic definitions of modelingtermsinthissection.

It is also important to begin to understand the variety of approaches tomodelingdifferent typesof systems.Wewill use someof the terminologyweintroduce to provide a few examples of different modeling approaches tosimulate avarietyof situations.Wewill then conclude this chapterwith someexercisesthatletyoudelvedeeperintotheworldofmodelingandsimulation.

1.3.3ModelingandSimulationTerminology

Bynow,youshouldhaveyourownconceptofwhatconstitutesamathematicalorcomputermodel.Amoreformaldefinitionisprovidedhere.

Amathematicalmodelisarepresentationofaphenomenonorsystemthatisusedtoprovideinsightsandpredictionsaboutsystembehavior.

Simulation is the application of a model to imitate the behavior of thesystemunderavarietyofcircumstances.

Thereareseveraldifferentwaystoclassifymodels.Modelscanbedeterministicorprobabilistic.Another termforprobabilistic is stochasticmeaninga randomprocess or a process,which occurs by chance.A probabilisticmodel includesone or more elements that might occur by chance or at random while adeterministicmodel doesnot.Adeterministicmodel applies a set of inputs orinitialconditionsandusesoneormoreequationstoproducemodeloutputs.Theoutputs of a deterministic model will be the same for each execution of thecomputercodewiththesameinputs.Aprobabilisticmodelwillexhibitrandomeffectsthatwillproducedifferentoutputsforeachmodelrun.

Models can also be characterized as static or dynamic. A dynamic modelconsiders the state of a system over time while a static model does not. Forexample,onecouldhaveamodelofamateriallikeasteelbeamthatconsidereditsabilitytobearweightwithoutbendingunderasetofstandardenvironmentalconditions. This would be considered to be a static model of that system. Adynamicmodelof thesamestructurewouldsimulatehow thebearingstrengthand possible deformation of the beamwould change under stresses over time

Page 36: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

suchasunderhightemperatures,vibration,andchemicalcorrosion.

Asteady-statemodelisamodelthathasgonethroughatransientstatesuchas a start-uporwarm-upperiod and arrived at anobservedbehavior thatremainsconstant.

Anexampleofthesteady-statemodelistheflowoffluidthroughapipe.Intheinitial, transient state period, the pipe is empty and will fill with fluid underpressure until the capacity of the pipe is reached. Thiswill be its steady-statecondition. In economics, a steady-state economy is one that has reached arelativelystablesize.

Perhaps making things more confusing, a dynamic model can havedeterministiccomponents.Suchamodelwouldtrackthestateofasystemovertimeand/orspace.Givenacurrentstate,adeterministicfunctionmaybeusedtopredict the future state of the system. Alternatively, the future state may bestochastic,whichisimpactedbyrandomevents.

Finally, dynamic models may be characterized as being discrete orcontinuous.Acontinuousmodelwouldrepresenttimeasacontinuousfunction,whereas adiscretemodeldivides time into small increments andcalculates itsstate for each timeperiod. In computermodeling,most (all?)dynamicmodelsdivide time into discrete increments to facilitate rapid calculations thatmimiccontinuoussystems.

1.3.4ExampleApplicationsofModelingandSimulation

Inordertogaininsightsintosystembehavior,simulationsareusedtoaskwhatifquestions about how the system changes under different circumstances. Howthese questions are addressed depends in part on the type of model and itsunderlyingmathematical structure. Solving thosemathematical equations on acomputeralso leads todifferences inprogramminglogicor thealgorithmsthatareusedtocalculatethemostaccurateanswermostefficiently.Wewilldiscusssomeofthosealgorithmsaswegothroughtherestofthisbook.Fornow,itmayhelptoprovidesomeexamplesofdifferentsimulationapproachesastheyrelatetovariousmodeltypes.

Deterministicmodels consist of one ormore equations that characterize thebehaviorofa system.Most suchmodels simplify thesystembyassuming that

Page 37: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

oneormorecausalvariablesorparametersareconstantforasinglecalculationofthemodeloutcomes.

Forexample,modelsofpeople’scartripbehaviorassumethatthewillingnesstomakea trip is inverselyproportional to the tripdistance.That is,peoplearemorelikelytomakeatripfromhometogettoadestinationthatiscloserthanthe one that is far away. Empirical studies have shown that this friction ofdistance changes depending on the nature of the trip. People are much morewilling tomakea longer trip toget towork than theyare todoaconvenienceshoppingtrip.Tosimplifythesystem,thesemodelsassumeaconstantvalueofthis friction of distance factor for each type of trip. When such a model isappliedtoanewurbanarea,thereissomeuncertaintythattheconstantsfoundinprevious studies in different places match the area where the model is beingapplied. Thus, a study is done where the model is run with different butreasonablevariationsintheconstantstoascertaintheimpactofthosechangesonthepredicted trips.Those can thenbe comparedwith a sampleof real data tocalibrateandvalidatethemodel.

Other examples of parametric studies include models of structures wheredifferent environmental conditions will alter system behavior, air and waterpollutionmodels where assumptions aremade about the rate of dispersion ofcontaminants, and models of drug absorption into the blood stream whereassumptions are made about absorption rates and excretion rates of the drugwithinthebody.Manymodelsincludecomponentsthatarebothstochasticanddeterministic where parametric studies are done on the deterministiccomponents.

Fordynamicmodels,thefocusisonthebehaviorofthesystemovertimeandsometimesoverspace.Foronegroupofsuchmodelscalledsystemsdynamicsmodels, thestateof thesystematanytimeperiod isdependent, inpart,onthestateofthesystemattheprevioustimeperiod.Simulationscalculatethechangesinthestateofthesystemovertime.Anexampleisamodelofballbeingdroppedfromabridge.Asitisdroppedtheballacceleratesduetotheforceofgravity.Ateach time increment, the model will calculate the velocity of the ball and itspositioninspace.Thatpositionwilldependonwhereitwasintheprevioustimeperiodandhowfaritwasdroppedrelatedtoitsvelocityduringthattimeperiod.Themodelwillthenpredictwhentheballwillhitthewaterandatwhatvelocity.

Stochasticmodelstypicallywillhavecharacteristicsincommonwithdynamicmodels. The difference is that one or more of the governing parameters areprobabilisticorcouldhappenbyrandomchance.Oneexampleisamodelofthe

Page 38: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

spreadofadisease that ispassedbyhumancontact.Asusceptiblepersonmaymakecontactwithaninfectedpersonbutwillnotnecessarilybecomeinfected.There is a probability of being infected that is related to the virility of thedisease, the state of health of the susceptible person, and the nature of thecontact.Amodelofthissystemwouldsimulatethoseprobabilitiestoprojectthepotentialspreadofadiseaseoutbreak.

Aswe go through the rest of this book,wewill describe themathematicalrepresentation of each of these types of models and the programming stepsneeded to implement them on the computer. Exercises will involve thecompletionofexampleprograms,theuseofthemodeltomakepredictions,theanalysisofmodeloutcomes,and,insomecases,validationofmodelresults.Theexercises for thischapter focuson themodelingprocessandexamplesofhowmodelshavebeenusedtosolveresearchandproductionproblems.

EXERCISES

1. Using a graphics program or one of the free concept-mapping ormind-mapping tools, create a complete conceptual map of the traffic modelintroducedearlierinthechapter.Youshouldincludealloftheotherfactorsyoucanthinkofthatwouldcontributeeithertotheincreaseordecreaseinthetrafficspeedthatmightoccurinarealsituation.

2.Insertanotherconceptmappingexamplehere.

3. Read the executive summary of one of the following reports and bepreparedtodiscussitinclass:

a.PITACreporttothepresident

b.Simulation-basedengineeringsciencereport

c.WorldTechnologyEvaluationsCenter

4. Usingthestudentwebsitefor thebookathttp://www.intromodeling.com,choose an example model project in the document example models fordiscovery and design as assigned by your instructor. Read through theavailablematerial and thenwrite abrief summaryof themodelingeffortanditscharacteristicsusingthesummarytemplateprovided.

Page 39: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

REFERENCES

Bartlett,B.N.1990.ThecontributionsofJ.H.Wilkinsontonumericalanalysis.InAHistoryofScientificComputing,ed.S.G.Nash,pp.17–30.NewYork:ACMPress.

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(accessedDecember15,2016).

Bell, T. 2016. Supercomputer timeline, 2016. https://mason.gmu.edu/~tbell5/page2.html (accessedDecember15,2016).

Biesiada, J.,A.Porollo,andJ.Meller.2012.Onsettingupandassessingdockingsimulations forvirtualscreening. InRationalDrugDesign:MethodsandProtocols,Methods inMolecularBiology,ed.YiZheng,pp.1–16.NewYork:SpringerScienceandBusinessMedia.

Cmapwebsite.http://cmap.ihmc.us/(accessedFebruary22,2016).Computer History Museum. 2017. Timeline of computer history, software and languages.

http://www.computerhistory.org/timeline/software-languages/(accessedJanuary2,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%20Conference%20Final.pdfCouncil on Competitiveness. 2009. Procter & gamble’s story of suds, soaps, simulations and

supercomputers.http://www.compete.org/publications/all/1279(accessedJanuary2,2017).Dorzolamide.2016.https://en.wikipedia.org/wiki/Dorzolamide(accessedDecember15,2016).Esterbrook, S. 2015. Timeline of climate modeling. https://prezi.com/pakaaiek3nol/timeline-of-climate-

modeling/(accessedDecember15,2016).Fatherree,B.H.2006.U.S.Armycorpsofengineers,Chapter5hydraulicsresearchgiant,1949–1963,Part

I:Rivermodeling,potamology,andhydraulicstructures.InTheFirst75Years:HistoryofHydraulicsEngineering at the Waterways Experiment Station,http://chl.erdc.usace.army.mil/Media/8/5/5/Chap5.htm.Vicksburg,MS:U.S.ArmyEngineerResearchandDevelopmentCenter(accessedOctober15,2016).

Goldstine,H.1990.Remembranceofthingspast.InAHistoryofScientificComputing,ed.S.G.Nash,pp.5–16.NewYork:ACMPress.

Gordon,S.I.,K.Carey,andI.Vakalis.2008.Ashared,interinstitutionalundergraduateminorprogramincomputationalscience.ComputinginScienceandEngineering,10(5):12–16.

HPCUniversitywebsite.http://hpcuniversity.org/educators/competencies/(accessedJanuary15,2016).International Panel on Climate Change. 2013. Climate change 2013—The physical science basis

contributionofworkingGroup I to the fifth assessment report of the IPCC,NewYork:CambridgeUniversity Press. http://www.climatechange2013.org/images/report/WG1AR5_ALL_FINAL.pdf(accessedDecember15,2016).

Jameson, A. 2016. Computational fluid dynamics, past, present, and future. http://aero-comlab.stanford.edu/Papers/NASA_Presentation_20121030.pdf(accessedDecember15,2016).

Joseph,E.,A.Snell,andC.Willard.2004.StudyofU.S.industrialHPCusers.CouncilonCompetitiveness.http://www.compete.org/publications/all/394(accessedDecember15,2016).

Lax,P.D.1982.Reportofthepanelonlargescalecomputinginscienceandengineering.Reportpreparedunder the sponsorship of the Department of Defense and the National Science Foundation.Washington,D.C.:NationalScienceFoundation.

Lynch,P.andO.Lynch.2008.ForecastsbyPHONIAC.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(accessedJanuary25,2016).

McCartney,S.1999.ENIAC.NewYork:WalkerandCompany.

Page 40: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Mindmapmakerwebsite.http://mindmapmaker.org/(accessedFebruary22,2016).NationalScienceFoundation.2006.Simulation-basedengineeringscience:ReportoftheNSFblueribbon

panelonsimulation-basedengineeringscience.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(accessedJanuary15,2016).

Pachauri, R. K. and L. A. Meyer (ed.). 2014. Climate change 2014: Synthesis report. Contribution ofworkinggroupsI, IIandIII to thefifthassessmentreportof the intergovernmentalpanelonclimatechange.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(accessedDecember15,2016).

Reed, D. 2005. Computational science: America’s competitive challenge. President’s informationtechnology advisory committee subcommittee on computational science.http://www.itrd.gov/pitac/meetings/2005/20050414/20050414_reed.pdf.

SocietyofIndustrialandAppliedMathematics(SIAM).2001.Graduateeducationincomputationalscienceandengineering.SIAMReview,43(1):163–177.

Vassberg,J.C.AbriefhistoryofFLO22.http://dept.ku.edu/~cfdku/JRV/Vassberg.pdf(accessedDecember15,2016).

Vogelsberger,M.,S.Genel,V.Springel,etal.2014.Propertiesofgalaxiesreproducedbyahydrodynamicsimulation.Nature,509(8):177–182.doi:10.1038/nature13316.

Yasar,O. andR.H.Landau.2001.Elementsof computational science andengineeringeducation.SIAMReview,45(4):787–805.

Page 41: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

CHAPTER2IntroductiontoProgrammingEnvironments

2.1THEMATLAB®PROGRAMMINGENVIRONMENT

MATLAB®(shortformatrixlaboratory)isapopularsoftwarepackageinmanydifferent science and engineering disciplines. It has a number of features thatmakeitagoodpackageformodelingandsimulation.Therearealargenumberof toolboxesavailablefor license,aswellasanumberofcommunity-providedtoolboxes to solve common problems. In addition, as a fourth-generationprogramminglanguagefocusedonnumericalcomputingthelanguagehasbuilt-in features that make it easy to work with vectors and matrices, allowingmodelerstoconcentrateontheirmodels,andnotonimplementingthedetailsofamatrixoperation.

2.1.1TheMATLAB®Interface

WewillbeusingMATLABR2016ainthisbook,asitisthemostrecentversionavailable at the time ofwriting.TheMathWorks has typically notmade largechangestotheuserinterfaceveryfrequently.

Onceinstalled,whenyoulaunchMATLAByouwillseethedefaultinterfaceasshowninFigure2.1.Across the top isa ribbon toolbarwithclearlymarkedfunctions,andtabsforHome,Plots,andApps.Wewill return to fewfunctionsontheHomeribbonlaterinthischapter.

Page 42: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

FIGURE2.1DefaultMATLAB®interface.

ThelargepaneinthemiddleistheCommandWindow.Thispaneprovidesaninterpreter and allows you to type MATLAB commands and see the resultsimmediately.

Theleftmostpanedisplaysthecurrentworkingdirectory’scontents.Youcanbrowse through your directory tree using the widget directly above the threemainpanes.ThecurrentworkingdirectoryisthefirstlocationwhereMATLABwilllookforfileswhenattemptingtoexecute,open,orclosefiles.Thereshouldbeahelloworld.mfile;ifyoudouble-clickittheEditorwillopen,showingyouthecontentsofthefileandmakingitpossibletoeditandsavetheupdatedfile.TheEditorprovidessomerichtools,includingsyntaxcoloring,debugging,andmore.WhentheEditorisopen,acontext-appropriatemenuribbonappearsatthetop, which includes debugging controls. We will return to those later in thischapter.

The pane to the far right is the Workspace, which displays the variablescurrently being used by MATLAB, and their value. Double-clicking on avariableinthislistwillopentheVariablespane,whichallowsfullerinspectionandeditingofvariables,includinglargematrices.Returningtothemenuribbonatthetopof thewindow,youshouldnoticeImportDataandSaveWorkspace,

Page 43: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

which allow you to quickly import and export datasets to and from yourWorkspace.

2.1.2BasicSyntax

MATLAB(theapplicationsharesanamewith itsprogramming language) isarelativelyflexiblelanguagethatusescertaincharactersforflowcontrolsuchas“{”and“}”.Also,unlikealanguagelikeC,whichrequiresaspecialterminationcharacter at the end of every line (a semicolon), that character is optional inMATLAB.However, thedecisionto includeitornot is important.Includingasemicolonattheendofalinewillsuppressthedisplayofoutputrelatedtotheexecutionofthatline.Omittingthesemicolonwilltelltheinterpretertodisplaytheresultofthatcommand.Wewillseeexamplesofthislater.Wewillintroducevariousconceptsinajust-in-timebasisasweworkthroughthecoursematerials.Aswegothroughanumberofthesyntaxexampleslater,wesuggestyoutotrythemoutintheCommandWindow,Workspace,orEditor,asappropriate.

2.1.2.1VariablesandOperatorsAll programming languages provide variables—a method to store andmanipulatedatathatmaybedifferentfromonerunoftheprogramtothenext—andwaystomanipulatethosevariables.Wewillintroducesomebasicshereandwillleavesomeofthemoreadvancedtoolsuntillaterchapters.

Inthemostbasicsense,avariableisjustanamethatweusetorefertoavaluethatmaychangeovertime.InMATLAB,wecansimplycreatevariablesastheyare needed,without having to declare a variable type (as is required inmanyotherlanguages).Forexample:

x=2

five=5

z=3.14159

my_string='HelloWorld!'

Type these commands into the console.Once a variable is declared, it can berecalledandusedinanappropriatecalculation.Asanexampleenterthis:

y=x*five;

Youwillnotethatweincludedasemicolon,whichsuppressedtheoutputofthe

Page 44: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

command,unlikethepreviousexamples.Toseetheresult,type:

y

Nowthatyouhavesomevariableinmemory,youcanlookattheWorkspacetoseethelistofvariablesandtheircurrentvalues.

Variableshavesomethingcalledscope,whichdefineswheretheyarevisible.Forexample,variablesdefinedintheCommandWindowareglobal;theycanbereferenced,used,andmodifiedfromanyotherpieceofcode.Forexample,ifwedefine “x” in the CommandWindow, and then inmy_script.m if we add thecommand“x”,whenwerunthatprogram,itwillprintthevalueof“x”.

However, variables defined within a function are only visible within thatfunction.Thesearecalledlocalvariables.Wewillcomebacktothisidealaterwhenyoubegintocreatefullprograms.

Youcandeletevariablesintwoways.Thefirstistheclearcommand:

clearx

Thesecond is togo to theWorkspacewindow, right-clickon thevariableyouwishtodelete,andselectDeletefromthepop-upmenu.

Youwill also note that theWorkspace gives you tools such as renameandedit,whichwillbeuseful.

Incomputationalscience,wecallvariablesthatholdasinglevalueascalar.This is slightlydifferent than themathematicaldefinitionofscalar.MATLABsupportsanumberofarithmeticoperations(Table2.1).

YoucantrysomeofthesecalculationsintheCommandWindowbytypinginthe left sideof theequation.Thatwillbedefinedas input,and the resultswillemergeasoutput.

Please note that, in MATLAB, some of these operators are also used formatrixoperations,andMATLABwillreturnanerror(ordosomethingyoumaynotbeexpecting)ifyouareusingmatrices.Youcanexplicitlytelltheinterpreterthatyouwanttodoscalaroperationsbyprependingtheoperatorswithaperiod(“.”). For example, to conduct a scalar multiplication instead of a matrixmultiplication, use “.*”. This can be a concern when doing multiplication,division, and exponentiation. You can include the “.” safely at any time youwanttoperformthescalaroperation.

TABLE2.1MATLAB®MathematicalOperators

Page 45: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Symbol Operation Example+ Addition 2+2=4− Subtraction 4–1=3/ Division 9/3=3* Multiplication 8*6=48^ Exponential 3^2=9

TABLE2.2OrderofExecutionforMathematicalOperationsinMATLAB®

() Itemsenclosedinparentheses^ Exponentiation*,/ Multiplication,division+,− Additionandsubtraction= Assignment

MATLABfollowsthenormallyexpectedorderofoperations:exponentsandroots, followed by multiplication and division, followed by addition andsubtraction.Specifically,operationsshouldbecarriedoutinthefollowingorder,andforoperationsatthesamelevelfromlefttorightasshowninTable2.2.

Forexample,

(3*2)**3+6

Whathappensifweexecutethiscommandwithouttheparentheses?Pleasenote thatwehavenot includedalloperators.Comparisonand logical

operatorswillbediscussedlater.

2.1.2.2KeywordsMATLAB reserves certain words, called keywords, which cannot be used asvariable names. Note that these words will be colored in blue when they aretypedintheEditororCommandWindow.TheyarelistedinTable2.3.

TABLE2.3MATLAB®ReservedKeywordsbreak casecatch classdefcontinue elseelseif endfor functionglobal ifotherwise parforpersistent returnspmd switchtry while

An additionalword ofwarning: It is possible to overwrite a function name(but not a keyword) with a variable name (and vice versa). Use care whenselecting variable names, or you might experience unexpected errors when

Page 46: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

executingcode.

2.1.2.3ListsandArraysInprogramming,weoftenhaveagroupofhomogeneousvariablesthatrepresentmultiplevaluesofeitherinputsoroutputsfromamodel.Forexample,asinputswemighthavetrafficcountsforaparticularlocationfordifferenthoursand/ordifferent days, multiple values of an environmental indicator such as airpollution fordifferent times,orasequenceofvaluesofamodelparameterwewillusetotestitsimpactsonthemodeloutcomes.Wethenwouldwanttostoretheseresultsinasimilargroup.

One way to represent such a group of variables in MATLAB is called anarray. Interestingly, in MATLAB everything is an array, including the scalarvalueswewereworkingwith before.An array is defined to include places inmemoryformultipleitemsindexedbyasequencenumber.Itcanbedeclaredinseveralways:

traffic=[200,150,350,235,450];

nox=zeros(1,365);

Ifyoutypeinthefirstexample,youwillseethatatrafficvariableiscreatedwithfive itemswith thevalues indicated. In the secondexample (our first useof afunction), an array is createdwith 365 items initially set to a value of 0.Thiscouldbe,forexample,spacetoholdtheaveragedailynitrogenoxidecontentintheair.NoticehowtheyarerepresentedintheWorkspace.

Wecanusetheindextooperateoneachoftheitemsinthelistinturnorcanoperateonanyindividualitembyusingitsindexnumber.Toseeasinglevalue,weuse thevariablenamewith the index inparentheses.Try thisandseewhathappens:

traffic(2)

You should get the value 150. This is because MATLAB starts all lists andarrays with the index number 1. So if you put traffic(1) in the console, youshouldget200.Whathappensifyouputthisin:

traffic

Aone-dimensionalarrayisoftencalledavector,whereasatwo-dimensionalarray is called amatrix.Arrays can usematrixmathematics to operate on the

Page 47: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

entirearrayinspecificwayswewillintroducelater.Wehavealreadyseenthebasicinterfaceforcreatingvectorsandmatrices.

x=[1,2,3,4];

y=[1,2;3,4];

Note that we created a 1 × 4 vector and a 2 × 2 matrix with the previouscommands.ExaminehowthesevariablesarecharacterizedintheWorkspaceandhowtheyareshownintheCommandWindowwhenyouqueryforthecontentsofxandy.

Thereareseveralspecialfunctionstocreatecertaintypesofarrays.Anarraywith all zeros can be created with the “zeros()” function, as we saw earlier,whereasanarrayofonescanbecreatedwiththe“ones()”function.Anidentityarraycanbecreatedwiththe“eye()”function.Trythesefunctions:

x=zeros(6)

y=ones(8)

To create an evenly spaced array, MATLAB provides a function called“colon()”,which alsohas shorthandusing the “:” operator.To create an arraythat goes from 0 to 5 (inclusive—so 6 elements), you can type “0:5”. For anarraythatstartswith2andendswith8,youcantype“2:8”or“colon(2,8)”.

z=2:8

Youcanalsospecifyacustomstepsize,insteadofbeingrestrictedto“1”asthedefault,byaddinga thirdparameter, locatedbetween thestartandstoppoints.Forexample,3:.2:4willreturnanarraycontaining[33.23.43.63.84].

Another useful function for creating vectors containing regularly spacedvaluesis“linspace()”.Ratherthanspecifyingthesizeofthestep,youspecifythenumber of elements youwant in your array. For example, linspace(3,4,6)willreturnanarraycontaining[3.3.23.43.63.84].

myarray=linspace(3,4,6)

Wecanaccess(andmodify)individualelementsinarraysinMATLAB.Usethelinspace() example above to create the array “myarray”. We can look at thesecondelementofmyarrayby typing“myarray(2)” in theCommandWindow.

Page 48: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Wecanchangethevalueofthesecondelementin“myarray”simplybyusingitontheleftsideofanassignmentexpression.

myarray(2)=2

Thiswillturn“myarray”into[323.43.63.84].We can also extract portions of arrays called slices. For example,we could

create a slice of “myarray” containing the second and third elementswith thecommand “myarray(2:3).” The argument is in the form “start:step:stop” (thesameasthe“colon()”function).Thestepargumentisoptional(andassumedtobe1unlessspecified),andstopcanusethekeywordendtomeangototheendofthelist.“myarray(1:3)”will return[323.4],whereas“myarray(4:end)”willreturn [3.6 3.8 4]. “myarray(1:2:end)” will select every other element in thearray:[33.43.8].

2.1.3CommonFunctions

We could not possibly exhaustively list every function included inMATLABhere,muchlesseverythingavailableinthenumeroustoolboxes.Throughoutthebook,wewill introduceadditional functions asneeded,butTable2.4 shows afew important ones available in the base MATLAB program. To use thesefunctions,enclosethetargetvariableinsidetheparentheses.

2.1.4ProgramExecution

One of the things thatmakesMATLAB a powerful tool is that you can bothwork interactively in the Command Window, and you can also writecomplicated programs capturing very detailed work flows to ensure accuraterepeatability. Programs can be executed outside of theCommandWindow bypassingtheprogramfile(inthisexamplecalledmyprogram.m)totheMATLABexecutableinyouroperatingsystem’scommandwindow:

TABLE2.4ExampleBuilt-inFunctionsforMATLAB®

abs() Absolutevalue.mod() Taketwononcomplexnumbersandreturntheirremainderwhenusinglongdivision.whos() ReturnsthevariablesinthecurrentWorkspace.“whosGLOBAL”returnsthegloballyscopedvariables.size() Returnsthesizeofthearray,meaningthenumberofitemsineachdimension.max() Returnsthelargestiteminthearray.min() Returnsthesmallestiteminthearray.open() Opensafile.Wewilltalkaboutfileinput/outputinmoredetaillater.disp() Printsarraystothedisplay,whilenotshowingthevariablename.Usefulforinteractingwithusersofyourprograms.

Page 49: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

matlab–rmyprogram.m

We can also run these programs inside of the CommandWindow or via theMATLABgraphicaluser interface,whichprovides someadditionaldebuggingcapabilities.

2.1.5CreatingRepeatableCode

InMATLAB,creatingrepeatablecodeisassimpleastypingthecommandsyouwant into a single file with a “.m” extension. One commonmethod for codedevelopment in interpreted languages is to interactively manipulate variablesuntil you begin to see how to get the results you want, and then pull thosecommandsfromyourhistoryandputthemintotheprogramfile.Laterwewillexploreflowcontrol—howtoexecutesomeblocksofcodebutnotothers—andhow to create your own functions or classes, but for now,wewill focus on asimplelistofcommandstoexecute.

MATLABstartsoffwithafilecalled“helloworld.m”inthedefaultworkingdirectory. You can directly edit this file to explore this functionality (double-clickonittoopenitintheEditor),butyoucanalsouseNewbuttononthemenuribbontocreateotherfiles.Letusedithelloworld.mtomodifythebasicHelloWorldprogram.Addalineatthebottomtodisplay(disp)thestring“Mynameis‘Hal’.”.YourEditorpaneshouldlooksimilartoFigure2.2.

You can execute this program by clicking the gray triangle labeled Rundirectly above the Editor, which will execute the program in the CommandWindow.Youshouldseeahelloworldcommand,followedbyyourcodeoutputon the next line. Alternatively, you can simply type helloworld in yourCommandWindow.

FIGURE2.2MATLAB®HelloWorldscript.

Page 50: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

2.1.6Debugging

MATLAB includes some debugging tools, which can be very useful indiscoveringproblemsinyourcode.

Ifyousetabreakpointinyourcode,simplyrunningthecodewilltriggerthedebugger when the breakpoint is reached. Breakpoints allow you to target aspecific point in the code that you wish to stop and investigate. When thedebuggerreachesabreakpoint,youcaneitherruncommands in theCommandWindowtoinvestigatethestateofyourprogram,oryoucanusetheWorkspaceandVariablespanestolookatyourdata.Youcansetbreakpointsbyclickingonthe“-”inthegrayspacetotheleftofthelineyouwishtostopexecutionatintheEditor.

The most common bugs you are likely to encounter when working onmodeling and simulation problemswill bewhen your data do not contain thevaluesyou expectedwhen executing a certainblockof code, andMATLAB’sdebuggerisausefultoolfordiscoveringthis.

Onceyouwishtocontinueexecution,youcanStepthroughtheprogramonelineatatime,StepIntothefunctionofthecurrentline,StepOutandrununtilthecurrentfunctionends,Continueuntil thedebuggerhits thenextbreakpoint,orQuitDebugging.Youwillneedtoexitthedebuggertoreturntheconsoletothenormalmode.Theexercises includeanexamplewhereyoucan tryout thedebugger.

2.2THEPYTHONENVIRONMENT

Pythonisaverypopularhigh-levelgeneral-purposeprogramminglanguage.Asan interpreted language,codedoesnotneed tobecompiledandcanbe runonany system where an interpreter is available. The core language is Free andOpen-SourceSoftware(FOSS),andcanbeacquiredandusedatnocost.Thereisaverylargelibraryofroutinesavailable,includingonesspecificallydesignedforscientificcomputing,suchasSciPyorNumPy.

2.2.1RecommendationsandInstallation

Thereareanumberofintegrateddevelopmentenvironments(IDEs)availableforPython, many of which would be quite good for this course. We arerecommending the use of Spyder, a free IDE, which includes a number of

Page 51: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

MATLAB-like features that make it especially useful for modeling andsimulationuse.Inaddition,youwillneedtodownloadandinstallaninterpreter;werecommendAnaconda,whichisavailableforMac,Linux,andWindowsathttps://www.continuum.io/downloadsandwhichprovidesabundleofscientificprogramming packages in the base installation (including Spyder). WerecommendgettingPython3;therearesomeminorsyntaxdifferencesbetweenPython2andPython3,andPython3hasafewnewfeaturesthatmakeitbettersuitedtoourpurposes.ThisbookusesPython3syntax.

2.2.2TheSpyderInterface

TheSpyderIDEisalreadyincludedintheAnacondainstallation.Ifyouarenotusing Anaconda, you can find Spyder and installation instructions athttps://pythonhosted.org/spyder/.

To launch Spyder, you can select it from the list of programs for yourcomputeroryoucanrunSpyderinaterminalorcommandwindow.WhenyoustartSpyder,youshouldseeaninterfacewithmultiplepanesasshowninFigure2.3.Inthelowerrightofthewindow,Spyderprovidesaninteractiveconsole.Bydefault, it launches IPython,which is an enhanced interactive Python terminalthat provides some improvements that make it more useful for interactiveprogramming than the standardPython interpreter.Here, you can typePythoncommands,whichwillbeexecutedimmediately.

Page 52: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

FIGURE2.3DefaultSpyderinterface.

The upper right region of the window shows the Help pane. It willautomaticallyshowdocumentationforafunctionorobjectbeinginstantiatedintheeditororconsole,oryoucantypeafunctionnameintheObjectfieldatthetopofthewindowtopullupthecorrespondingdocumentation.

In the same pane as the Help is the Variable Explorer. Clicking on theVariableExplorerbuttonat thebottomof thepanewillswitch theviewto theVariableExplorer.Thisgivesyouaviewintotheglobalvariablesforthecurrentconsole.Itwillbeemptyuntilyouentercommandstocreateormodifyvariablesintheconsoleorrunascriptintheeditor.Mostcommonvariabletypescanbedisplayed,andvariablescanbeeditedinthisinterface.Youcandoquickplotsofarraysandshowmatricesasimages.

You can also save sets of variables in a Spyder-specific format,MATLABdataformat,orHDF5formatbyusingtheDiskicontotherightofthepane.Inaddition,youcanimportdatafromalargevarietyofsupportedformatsusingtheImportDataicon(downwardpointingarrowicon).

TheleftpaneshowstheSpydereditor.ItenableseditingofPython(andotherlanguages!)andincludesfeaturessuchassyntaxcoloring,function/class/methodbrowsing, code analysis, introspection, and more. At the top of the Spyder

Page 53: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

window are debugging controls that allow you to execute the Editor pane’scurrentfileintheIPythonconsole;wewillcoverthoseinmoredetaillater.

2.2.3BasicSyntax

Python uses whitespace to designate code blocks. In short, this meansindentation ishowwegroup linesof code together for flowcontrol.Thiswillbecomeclearerlater.Also,unlikemanyotherlanguages(includingMATLAB),semicolonsarenotusedaslineterminators.Wewillfillinotherdetailsinajust-in-time basis as we work through the course materials. As we go through anumber of the syntax examples below,we suggest you to try them out in theSpyderinterfacebyinsertingtheminconsole,help,oreditorasappropriate.

2.2.3.1VariablesandOperatorsAll programming languages provide variables—a method to store andmanipulatedatathatmaybedifferentfromonerunoftheprogramtothenext—andwaystomanipulatethosevariables.Wewillintroducesomebasicshere,andleavesomeofthemoreadvancedtoolsuntillaterchapters.

Inthemostbasicsense,avariableisjustanamethatweusetorefertoavaluethatmaychangeovertime.InPython,wecansimplycreatevariablesastheyareneeded,withouthavingtodeclareavariabletype(asisrequiredinmanyotherlanguages).Forexample:

x=2

five=5

z=3.14159

my_string="HelloWorld!"

Type these commands into the console.Once a variable is declared, it can berecalledandusedinanappropriatecalculation.Asanexample,enterthis:

y=x*five

Toseetheresult,type:

y

Nowthatyouhavesomeinmemory,openthevariableexplorertoseethelistofvariablesandtheircurrentvalues.

Page 54: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Variableshavesomethingcalledscope,whichdefineswheretheyarevisible.Forexample,variablesdefinedintheconsoleareglobal;theycanbereferenced,used,andmodifiedfromanyotherpieceofcode.Forexample,ifwedefine“x”intheconsole,andthenintemp.pyifweaddthecommand“print(x)”,whenwerunthatprogram,itwillprintthevalueof“x”.

However, variables defined within a code block (such as a function or anindented section of code) are only visible within that code block, and anydependent blocks (subblocks).These are called localvariables.Wewill comebacktothisidealaterwhenyoubegintocreatefullprograms.

Youcandeletevariablesintwoways.Thefirstisthedelcommand:

delx

ThesecondistogototheVariableExplorer,right-clickonthevariableyouwishtodelete,andselectRemovefromthepop-upmenu.

YouwillalsonotethattheVariableExplorergivesyoutoolssuchasrenameandeditthatwillbeuseful.

TABLE2.5PythonMathematicalOperatorsSymbol Operation Example+ Addition 2+2=4− Subtraction 4−1=3/ Division 9/3=3% Modulo 7%2=1* Multiplication 8*6=48// Floordivision 7//2=3** Exponential 3**2=9

TABLE2.6OrderofExecutionforMathematicalOperationsinPython() Itemsenclosedinparentheses** Exponentiation*,/,%,// Multiplication,division,modulo,floordivision+,− Additionandsubtraction= Assignment

Incomputationalscience,wecallvariablesthatholdasinglevalueascalar.This is slightly different than the mathematical definition of scalar. Pythonsupportsanumberofarithmeticoperations,asshowninTable2.5.

Youcantrysomeofthesecalculationsintheconsolebytypingintheleftsideof the equation. That will be defined as input, and the results will emerge asoutput.

Python follows the normally expected order of operations: exponents and

Page 55: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

roots, followed by multiplication and division, followed by addition andsubtraction.Specifically,operationsshouldbecarriedoutinthefollowingorder,andforoperationsatthesamelevelfromlefttorightasshowninTable2.6.

Forexample,

(3*2%4//2)**3+6

Whathappensifweexecutethiscommandwithouttheparentheses?Pleasenote thatwehavenot includedalloperators.Comparisonand logical

operatorswillbediscussedlater.

2.2.3.2KeywordsPython reserves certain words, called keywords, which cannot be used asvariablenames.NotethatSpyderwillcolorthesewordsinmagentawhentheyaretypedintheEditororconsole.TheyarelistedinTable2.7.

TABLE2.7PythonReservedKeywordsand importas inassert isbreak lambdaclass nonecontinue nonlocaldef notdel orelif passelse raiseexcept returnfalse truefinally tryfor whilefrom withglobal yieldif

An additionalword ofwarning: It is possible to overwrite a function namewithavariablename(andviceversa).Usecarewhenselectingvariablenames,oryoumightexperienceunexpectederrorswhenexecutingcode.

2.2.3.3ListsandArraysInprogramming,weoftenhaveagroupofhomogeneousvariablesthatrepresentmultiplevaluesofeitherinputsoroutputsfromamodel.Forexample,asinputswemighthavetrafficcountsforaparticularlocationfordifferenthoursand/ordifferent days, multiple values of an environmental indicator such as airpollution fordifferent times,orasequenceofvaluesofamodelparameterwe

Page 56: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

willusetotestitsimpactsonthemodeloutcomes.Wethenwouldwanttostoretheseresultsinasimilargroup.

OnewaytorepresentsuchagroupofvariablesinPythoniscalledalist.Alistisdefinedtoincludeplacesinmemoryformultipleitemsindexedbyasequencenumber.Itcanbedeclaredinseveralways:

traffic=[200,150,350,235,450]

nox=[0.]*365

Ifyoutypeinthefirstexample,youwillseethatatrafficvariableiscreatedwithfiveitemswiththevaluesindicated.Inthesecondexample,alistiscreatedwith365itemsinitiallysettoavalueof0.Thiscouldbe,forexample,spacetoholdthe average daily nitrogen oxide content in the air. Notice how they arerepresentedintheVariableExplorer.

Wecanusetheindextooperateoneachoftheitemsinthelistinturnorcanoperateonanyindividualitembyusingitsindexnumber.Toseeasinglevalue,we use the variable name with the index in brackets. Try this and see whathappens:

traffic[1]

Youshouldgetthevalue150.ThisisbecausePythonstartsall listsandarrayswiththeindexnumber0.Soifyouputtraffic[0]intheconsole,youshouldget200.Whathappensifyouputthisin:

traffic

Formanyprogrammingapplications,wewillneedanarrayofvaluesthathaveamorespecificmathematicaldefinition.Anarrayisanorderedsequenceofvaluesmuchlikealist.Aone-dimensionalarrayisoftencalledavector,whereasatwo-dimensional array is called a matrix. Unlike a list, numerical arrays can usematrix mathematics to operate on the entire array in specific ways we willintroduce later. For now, we just wish to define how an array is declared inPython. To do so, we need to import a special module into our PythonenvironmentcalledNumPy.WecanimportNumPyintoourcodeandrenameitasnpwiththefollowingcommand:

importnumpyasnp

Page 57: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

For the rest of this textbook, when using functions or objects provided byNumPy,wewillassumethatithasbeenimportedasnp,whichisconsistentwiththedocumentationforNumPy.PleasenotethatrenamingapackageuponimportdoesnotrenamethepackageintheHelp.

The basic interface for creating vectors and matrices is NumPy’s “array()”function,anditcanacceptalistobjectasinput.

x=np.array([1,2,3,4])

y=np.array([[1,2],[3,4]])

Note that we created a 1 × 4 vector and a 2 × 2 matrix with the previouscommands.Thedatatypecreatedbyarray()isndarray,whichisdistinctfromalist,andhassomesupportingfunctionsthatlistsdonothave.Examinehowthesevariablesarecharacterized in thevariableexplorerandhow theyare shown intheconsolewhenyouqueryforthecontentsofxandy.

Aswithlists,Pythonarraysarewhatwecallzero-indexed.Whatthismeansisthat the very first element in the array (in any dimension) is in the positionlabeled zero. This is a very common convention in many programminglanguages, comparedwith theeverydayconventionof calling the first elementindex1.Thiswillbeimportantwheniteratingoverarraysorselectingindividualelementsorranges.

Thearray()functionhasanumberofoptionalparameters,whichwewillnotcoverindetail.ThereareseveralspecialNumPyfunctionstocreatecertaintypesof arrays. An array with all zeros can be created with the “zeros()” function,whereasanarrayofonescanbecreatedwiththe“ones()”function.Anidentityarraycanbecreatedwiththe“eye()”function.Trythesefunctions:

x=np.zeros(6)

y=np.ones(8)

OneNumPyfunctionwewilluseheavilywillbe“arange()”.Initsmostsimpleform, itwillgiveyouavector startingatzeroandcountingbyone to theendpointyouspecify.Forexample,arange(5)willreturnanndarraycontaining[0,1,2,3,4].Alternatively,youcansupplyoptionalargumentstospecifythestartpointandthesizeofthestep.Forexample,arange(3,4,.2)willreturnanndarraycontaining[3.,3.2,3.4,3.6,3.8].

z=np.arange(6)

AnotherusefulNumPyfunctionforcreatingvectorscontainingregularlyspaced

Page 58: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

valuesis“linspace()”.Ratherthanspecifyingthesizeofthestep,youspecifythenumberofelementsyouwantinyourndarray.Forexample,linspace(3,4,6)willreturnanndarraycontaining[3.,3.2,3.4,3.6,3.8,4.].

myarray=np.linspace(3,4,6)

We can access (andmodify) individual elements in arrays in Python.Use thelinspace() example above to create the array “myarray”. We can look at thesecond element ofmyarrayby typing “myarray[1]” in the console.Rememberthatpreviouslyweexplained thatPython iszero-indexed,meaningmyspace[0]referstothefirstelement,andmyspace[1]referstothesecond.Wecanchangethevalueofthesecondelementin“myarray”simplybyusingitontheleftsideofanassignmentexpression.

myarray[1]=2

Thiswillturn“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 elementswith thecommand“myarray[1:3]”.Theargument is in theform“start:stop:step”,and itdoesnotincludethestopelement.The“step”argumentisoption(andassumedtobe1unlessspecified),whereasstartandstop,ifnotspecified,areassumedtobe the beginning and end of the array. “myarray[:3]” will return [3., 2., 3.4],whereas“myarray[3:]”willreturn[3.6,3.8,4.].“myarray[::2]”willselecteveryotherelementinthearray,startingwithpositionzero:[3.,3.4,3.8].

2.2.4LoadingLibraries

Codelibrariesarebundledupinthingscalledmodules.Tomakethosemodulesavailable in our programs,we need to import them aswe did for theNumPymoduleearlier.Forexample,amodulecalledmathprovidesanumberofbasicmathematicsfunctions.Toloadthatmodule,weissuethiscommandinourscriptorIPythonconsole:

importmath

Now,wecanexecutefunctionsfromthemathmodule.Forexample,wecanfindthesquarerootofavalue:

math.sqrt(9)

Page 59: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

Goaheadandtryoutthesecommandsintheconsole.Importing a module is not persistent; when you restart a console, you will

need to reimport any modules. Remember, you can find documentation formodules and functions in the Help. Now that you have imported the mathmodule, type the termmath.sqrt in theHelppane toget thedocumentationforthiscommand.Youcanalsoputyourcursoronthelineintheconsolewhereyoutypedmath.sqrtandhittheControlandIkeytogetthesameobjectfeedback.

Rather than importing an entire module, we can also import just a singlefunction of thatmodule. This has the advantage of simplifying the call to thefunctionlaterinyourcode.Returningtothesquarerootfunction:

frommathimportsqrt

Now,youcandirectlyexecutesqrt:

sqrt(9)

However,otherfunctionsinthemathmodulewillnotbeavailable.

2.2.5CommonFunctions

Wecouldnotpossiblyexhaustivelylisteveryfunctionincludedinthecommonmodules here, much less everything available in the Anaconda distribution.Throughout the book, we will introduce additional functions as needed, butTable 2.8 shows a few important ones from the builtins module (availablewithout loading).Touse these functions, enclose the targetvariable inside theparentheses.

TABLE2.8ExampleBuilt-inFunctionsforPythonabs() Absolutevalue.divmod() Taketwononcomplexnumbersandreturntheirquotientandremainderwhenusinglongdivision.float() Turnstheinputstringornumberintoafloating-pointnumber.

globals() Returnsavariablecalledadictionarythatshowsthecurrentglobalsymboltable.ThisincludeseverythingyoucanseeintheVariableExplorer,butalsothingslikeloadedmodulesandcommandhistory.

int() Turnstheinputstringornumberintoaninteger.len() Returnsthenumberofitemsintheinput.Acceptsstrings,lists,dictionaries,sets,etc.max() Returnsthelargestitemintheinputlist.min() Returnsthesmallestitemintheinputlist.open() Opensafile.Wewilltalkaboutfileinput/outputinmoredetaillater.print() Printsobjects,eithertoaspecificfileortothedisplay.Usefulforinteractingwithusersofyourprograms.

range() Createslistsofarithmeticprogressions.Willbeveryuseful;readthedocumentationintheHelpformoredetails.Wewillvisitaverysimilarfunctionlaterinthischapter.

2.2.6ProgramExecution

Page 60: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

OneofthethingsthatmakesPythonapowerfultoolisthatyoucanbothworkinteractively in the IPython console, and you can also write complicatedprograms capturing very detailed work flows to ensure accurate repeatability.Programs can be executed outside of the Spyder environment by passing theprogramfile(inthisexamplecalledmyprogram.py)tothepythoninterpreter:

pythonmyprogram.py

We can also run these programs inside of the Spyder environment, whichprovidessomeadditionaldebuggingcapabilities.

2.2.7CreatingRepeatableCode

In Python, creating repeatable code is as simple as typing the commands youwanttoexecuteintoasinglefilewitha“.py”extension.Onecommonmethodfor code development in interpreted languages is to interactively manipulatevariablesuntilyoubegin toseehowtoget the resultsyouwant,and thenpullthosecommandsfromyourhistoryandputthemintotheprogramfile.Laterwewillexploreflowcontrol—howtoexecutesomeblocksofcodebutnotothers—andhowtocreateyourownfunctions,classes,ormodules,butfornowwewillfocusonasimplelistofcommandstoexecute.

The Spyder Editor starts off with a file called “temp.py” opened. You candirectlyeditthisfiletoexplorethisfunctionality,butyoucanalsouseNewFileundertheFilemenutocreateotherfiles.Letusedittemp.pytocreatethebasic“Hello World” program. Add a line at the bottom to print the string “HelloWorld!”.YourEditorpaneshouldlooksimilartoFigure2.4.

Youcanexecutethisprogrambyclickingthe“RunFile”button(lookslikea“Play”button)directlyabovetheEditor,whichwillexecutetheprogramintheIPython console. You should see a “runfile()” command created by the IDE,followedbyyourcodeoutputonthenextline.

HereisanEasterEgg:type“import__hello__”intheconsole.(Note:“__”isarepeatedunderscorecharacter.)

Page 61: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

FIGURE2.4PythonHelloWorldscript.

2.2.8Debugging

One advantage to Spyder over testing code directly in the interpreter asdescribed above is that Spyder includes some debugging tools, which can beveryusefulindiscoveringproblemsinyourcode.

Tolaunchthedebugger,clickonthe“DebugFile”button,whichshowsaplayandpausebuttonnext toeachother. (Hoveringyourmouseoveranybutton intheSpydertoolbarwilldisplayatipinthelowerleftofthewindowexplainingwhat the button does.) Breakpoints allow you to target a specific point in thecode that you wish to stop and investigate. When the debugger reaches abreakpoint,youcaneitherruncommandsintheconsoletoinvestigatethestateofyourprogram,oryoucanusetheVariableExplorertolookatyourdata.Youcan set breakpoints bydouble-clicking in thegray space to the left of the lineyouwishtostopexecutionat;thedebuggerwillruntothatlinebydefaultorwillstopattheveryfirstlineotherwise.

The most common bugs you are likely to encounter when working onmodelingandsimulationproblemswillbewhenyourdatadoesnotcontainthevalues you expected when executing a certain block of code, and Spyder’sdebuggerisausefultoolfordiscoveringthis.

Once you wish to continue execution, you can (examining the debuggingbuttonsfromlefttoright)stepthroughtheprogramonelineatatime,stepinto

Page 62: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

thefunctionofthecurrentline,rununtilthecurrentfunctionends,rununtilthedebuggerhitsthenextbreakpoint,orexit.Youwillneedtoexitthedebuggertoreturntheconsoletothenormalmode.Theexercisesincludeanexamplewhereyoucantryoutthedebugger.

EXERCISES

1.BasicArithmetic

Convert the following equations into valid MATLAB or Pythoncommands,andsubmitboththeinputandoutputofthecommandfromtheinterpreter.

a.

b.

c.

2.CreatingandEditingMatrices

Createthefollowingmatrices,andsubmitboththeinputandoutputofthecommandfromtheinterpreter.

a.

b.

c.

d.

For the followingproblems, create thematrices as described, and theneditasdirected,andsubmittheeditingcommandandtheoutputfromtheinterpreter.

e. Create the array , and replace the thirdelementwiththenumber9.

Page 63: Introduction to Modeling and Simulation with MATLAB · PEER-TO-PEER COMPUTING: APPLICATIONS, ARCHITECTURE, PROTOCOLS, AND CHALLENGES ... 4.1 INTRODUCTION TO ARRAYS AND MATRICES 4.2

f.Createthearray ,andreplacethesecondelementwiththe value in that location cubed. That is, cube the value found in thatlocationandstoreitbackinthesamelocation.

g. Create the identity matrix and replace the element at

location2,3withthenumberofelementsintheidentitymatrix.

3.Savingandloadingdata

Use thematerialson thebookwebsite todownload thedatasetnecessaryfortheseproblemsineitherMATLABorPython.

a. Load theChapter2 dataset (Chapter2.m or Chapter2.py).What is thevalueinvariable“x”?

b. Cleanout thevariables inyourWorkspaceorVariableExplorer.LoadtheChapter2dataset.Modifythevariable“z”toequal .Savethedataset,andsubmit.

4.Debugging

Use the materials on the book website to download the sample codenecessaryfortheseproblemsineitherMATLABorPython.

a.OpenthecodeintheEditor.Setabreakpointonline22.Runthecodeinthedebugger,andreportthevalueofthevariable“x”whenthecodehitsthebreakpoint.

b.OpenthecodeintheEditor.Setabreakpointonline19andonline24.Run thecode in thedebugger,and report thevalueof thevariable“y”everytimethecodehitsthefirstbreakpoint.Thenreportthevalueof“z”whenthecodehitsthesecondbreakpoint.