Table Of Content15
Svein Linge · Hans Petter Langtangen
Programming for
Computations –
Python
Editorial Board
T. J.Barth
M.Griebel
D.E.Keyes
R.M.Nieminen
D.Roose
T.Schlick
Texts in Computational
15
Science and Engineering
Editors
TimothyJ.Barth
MichaelGriebel
DavidE.Keyes
RistoM.Nieminen
DirkRoose
TamarSchlick
Moreinformationaboutthisseriesathttp://www.springer.com/series/5151
(cid:2)
Svein Linge Hans Petter Langtangen
Programming for
Computations – Python
A Gentle Introduction to Numerical
Simulations with Python
SveinLinge HansPetterLangtangen
DepartmentofProcess,Energyand SimulaResearchLaboratory
EnvironmentalTechnology Lysaker,Norway
UniversityCollegeofSoutheastNorway
Porsgrunn,Norway Onleavefrom:
DepartmentofInformatics
UniversityofOslo
Oslo,Norway
ISSN1611-0994
TextsinComputationalScienceandEngineering
ISBN978-3-319-32427-2 ISBN978-3-319-32428-9(eBook)
DOI10.1007/978-3-319-32428-9
SpringerHeidelbergDordrechtLondonNewYork
LibraryofCongressControlNumber:2016945368
MathematicSubjectClassification(2010):26-01,34A05,34A30,34A34,39-01,40-01,65D15,65D25,
65D30,68-01,68N01,68N19,68N30,70-01,92D25,97-04,97U50
©TheEditor(s)(ifapplicable)andtheAuthor(s)2016Thisbookispublishedopenaccess.
Open Access This book is distributed under the terms of the Creative Commons Attribution-Non-
Commercial4.0InternationalLicense(http://creativecommons.org/licenses/by-nc/4.0/),whichpermits
anynoncommercialuse,duplication,adaptation,distributionandreproductioninanymediumorformat,
aslongasyougiveappropriatecredittotheoriginalauthor(s)andthesource,alinkisprovidedtothe
CreativeCommonslicenseandanychangesmadeareindicated.
Theimagesorotherthirdpartymaterialinthisbook areincludedinthework’s CreativeCommons
license, unless indicatedotherwise in the credit line; if such materialis not included in the work’s
CreativeCommonslicenseandtherespectiveactionisnotpermittedbystatutoryregulation,userswill
needtoobtainpermissionfromthelicenseholdertoduplicate,adaptorreproducethematerial.
Thisworkissubjecttocopyright.AllcommercialrightsarereservedbythePublisher,whetherthewhole
orpartofthematerialisconcerned,specificallytherightsoftranslation,reprinting,reuseofillustrations,
recitation,broadcasting,reproductiononmicrofilmsorinanyotherphysicalway,andtransmissionor
informationstorageandretrieval,electronicadaptation,computersoftware,orbysimilarordissimilar
methodologynowknownorhereafterdeveloped.
Theuseofgeneraldescriptivenames,registerednames,trademarks,servicemarks,etc.inthispublica-
tiondoesnotimply,evenintheabsenceofaspecificstatement,thatsuchnamesareexemptfromthe
relevantprotectivelawsandregulationsandthereforefreeforgeneraluse.
Thepublisher,theauthorsandtheeditorsaresafetoassumethattheadviceandinformationinthisbook
arebelievedtobetrueandaccurateatthedateofpublication. Neitherthepublishernortheauthorsor
theeditorsgiveawarranty,expressorimplied,withrespecttothematerialcontainedhereinorforany
errorsoromissionsthatmayhavebeenmade.
Printedonacid-freepaper
SpringerInternationalPublishingAGSwitzerlandispartofSpringerScience+BusinessMedia
(www.springer.com)
Preface
Computing,inthesenseofdoingmathematicalcalculations,isaskillthatmankind
has developed over thousands of years. Programming, on the other hand, is in
its infancy, with a history that spans a few decades only. Both topics are vastly
comprehensive and usually taught as separate subjects in educational institutions
around the world, especially at the undergraduate level. This book is about the
combinationofthetwo,becausecomputingtodaybecomessomuchmorepowerful
whencombinedwithprogramming.
Most universitiesand colleges implicitly requirestudentsto specialize in com-
puter science if they want to learn the craft of programming, since other student
programsusuallydonotofferprogrammingtoanextentdemandedforreallymas-
teringthiscraft.Commonargumentsclaimthatitissufficientwithabriefintroduc-
tion,thatthereisnotenoughroomforlearningprogramminginadditiontoallother
must-havesubjects,andthatthereissomuchsoftwareavailablethatfewreallyneed
toprogramthemselves. Aconsequenceisthatengineeringstudentsoftengraduate
with shallow knowledgeabout programming, unless they happened to choose the
computersciencedirection.
Wethinkthisisanunfortunatesituation. Thereisnodoubtthatpracticingengi-
neersandscientistsneedtoknowtheirpenandpapermathematics. Theymustalso
beabletorunoff-the-shelfsoftwareforimportantstandardtasksandwillcertainly
dothatalot. Nevertheless,thebenefitsofmasteringprogrammingaremany.
Whylearnprogramming?
1. Ready-madesoftwareislimitedtohandlingcertainstandardproblems.Whatdo
you do when the problem at hand is not coveredby the software you bought?
Fortunately,alotofmodernsoftwaresystemsareextensibleviaprogramming.
Infact,manysystemsdemandpartsoftheproblemspecification(e.g.,material
models)tobespecifiedbycomputercode.
2. With programming skills, you may extend the flexibility of existing software
packagesbycombiningthem. Forexample,youmayintegratepackagesthatdo
notspeaktoeachotherfromtheoutset. Thismakestheworkflowsimpler,more
efficient,andmorereliable,anditputsyouinpositiontoattacknewproblems.
v
vi Preface
3. It is easy to use excellent ready-made software the wrong way. Insight in
programming and the mathematics behind is fundamental for understanding
complexsoftware,avoidingpitfalls,andbecomeasafeuser.
4. Bugs (errors in computer code) are present in most larger computer programs
(also in the ones from the shop!). What do you do when your ready-made
softwaregivesunexpectedresults? Isitabug,isitwronguse,orisitthemath-
ematicallycorrectresult? Experiencewith programmingofmathematicsgives
you a good backgroundfor answering these questions. The one who can pro-
gram,canalsomaketailoredcodeforasimplifiedproblemsettingandusethat
toverifythecomputationsdonewithoff-the-shelfsoftware.
5. Lotsofskilledpeoplearoundtheworldsolvecomputationalproblemsbywrit-
ingtheirowncodeandoffertheircodeforfreeontheInternet.Totakeadvantage
ofthistrulygreatsourceofsoftwareinareliableway,onemustnormallybeable
tounderstandandpossiblymodifycomputercodeofferedbyothers.
6. Itisrecognizedworldwidethatstudentsstrugglewithmathematicsandphysics.
Too many find such subjects difficult and boring. With programming, we can
execute the good old subjects in a brand new way! According to the authors’
ownexperience,studentsfind itmuchmoremotivatingandenlighteningwhen
programming is made an integrated part of mathematics and physical science
courses. Inparticular,theproblembeingsolvedcanbemuchmorerealisticthan
whenthemathematicsisrestrictedtowhatyoucandowithpenandpaper.
7. Finally,welaunchourmostimportantargumentforlearningcomputerprogram-
ming:thealgorithmicthinkingthatcomeswiththeprocessofwritingaprogram
for a computational problem enforces a thorough understanding of both the
problem and solution method. We can simply quote the famous Norwegian
computerscientistKristenNyggaard:“Programmingisunderstanding”.
In the authors’ experience, programming is an excellent pedagogical tool for un-
derstandingmathematics: “Youthinkyouknowwhenyoucanlearn,aremoresure
when you can write, even more when you can teach, but certain when you can
program”(AlanPerlis,computerscientist,1922-1990). Consider,forexample,in-
tegration. A numericalmethodforintegrationhas amuchstrongerfocusonwhat
the integral actually is and means compared to analytical methods, where much
timeandeffortmustbedevotedtointegrationbyparts,integrationbysubstitution,
etc. Moreover,when programmingthe numericalintegration formula, it becomes
evidentthatitworksfor“all”mathematicalfunctionsandthattheimplementation
shouldbein termsof ageneralfunctionapplicableto “all”integrals. In thisway,
students learn to recognize a special problem as belonging to a class of problems
(e.g., integration, differential equations, root finding), for which we have general
numerical methods implemented in widely applicable software. When they write
thissoftware, aswedointhisbook,theylearnhowtogeneralizeandincreasethe
abstraction level of themathematical problem. When they usethis software, they
learn how a special case should be attacked by general methods and software for
theclassofproblemsthatcomprisesthespecialcaseathand. Thisisthepowerof
mathematicsinanutshell,anditisparamountthatstudentsunderstandthiswayof
thinking.
Preface vii
Targetaudienceandbackgroundknowledge Thisbookwaswrittenforstudents,
teachers, engineers and scientists that know nothing about programming and nu-
merical methods from before, but who seek a minimum of the fundamental skills
requiredto getstarted with programmingas atoolforsolving scientificand engi-
neeringproblems.Someknowledgeofone-andmulti-variablecalculusisassumed.
The basic programming concepts are presented in only 50 pages (Chapters 1 and
2), before practical applications of these concepts are demonstrated in important
mathematicalsubjectsaddressedintheremainingpartsofthebook(Chapters3-6).
Eachchapterisfollowedbyasetofexercisesthatcoverawiderangeofapplication
areas,e.g.biology,geology,statistics,physicsandmathematics. Theexerciseswere
particularlydesignedtobringacrossimportantpointsfromthetext. Thereaderwill
realizethatthemodestcontentofthefirst 50pagescaninfactbringyouquitefar
inpowerfulproblemsolving!
Learningtheverybasicsofprogrammingshouldnottakelong,butaswithany
other craft, mastering the skill requires continued and extensive practice. Some
beginningpracticeisgainedthroughChapters3-6,buttheauthorsstronglyempha-
size thatthis isonly astart. Studentsshouldcontinueto practiceprogrammingin
subsequentcourses,whilethosewhoexerciseself-study,shouldkeepupthelearn-
ingprocessthroughcontinuedapplicationofthecraft. Thebookisagoodstarting
pointwhenteachingcomputerprogrammingasanintegratedpartofstandarduni-
versity courses in mathematics and physical sciences. In our experience, such an
integrationisdoableandindeedrewarding.
Numericalmethods Anoverallgoalwith thisbookistomotivatecomputerpro-
grammingasaverypowerfultoolfordoingmathematics. Allexamplesarerelated
to mathematics and its use in engineering and science. However, to solve math-
ematical problems through computer programming, we need numerical methods.
Explainingbasicnumericalmethodsisthereforeanintegralpartofthebook. Our
choiceoftopicsisgovernedbywhatismostneededinscienceandengineering,as
well as in the teaching ofapplied physical science courses. Mathematical models
are then central, with differential equationsconstituting themost frequenttype of
models. Consequently,thenumericalfocusinthisbookisondifferentialequations.
Asasoftpedagogicalstarterfortheprogrammingofmathematics,wehavechosen
the topic of numerical integration. There is also a chapter on root finding, which
isimportantforthenumericalsolutiononnonlineardifferentialequations. Were-
markthatthebookisdeliberatelybriefonnumericalmethods. Thisisbecauseour
focus is on implementing numerical algorithms, but to develop reliable, working
programs,theprogrammermustbeconfidentaboutthebasicideasofthenumerical
approximationsinvolved.
The computer language: Python We havechosen to use the programminglan-
guage Python, because this language gives very compact and readable code that
closelyresemblesthemathematicalrecipeforsolvingtheproblemathand. Python
also has a gentle learning curve. There is a MATLAB/Octave companion of this
bookincasethatlanguageispreferred. Comparingthesetwoversionsofthebook
provides an excellent demonstration of how similar these languages are. Other
computer languages, like Fortran, C, and C++, have a strong position in science
andengineering.Duringthelasttwodecades,however,therehasbeenasignificant
viii Preface
shiftinpopularityfromthesecompiledlanguagestomorehigh-levelandeasier-to-
readlanguageslikeMatlab,Python,R,Maple,Mathematica,andIDL,forinstance.
Thislatterclassoflanguagesiscomputationallylessefficient,butsuperiorwithre-
spect to overall human problem solving efficiency. This book emphasizes how to
thinklikeaprogrammer,ratherthanfocusingontechnicallanguagedetails. Thus,
thebookshouldputthereader inagoodpositionfor learningotherprogramming
languageslater,includingtheclassicones: Fortran,C,andC++.
Howthisbookisdifferent Therearenumeroustextsoncomputerprogramming
and numerical methods, so how does the present one differ from the existing lit-
erature? Comparedtobooksonnumericalmethods,ourbookhasamuchstronger
emphasisonthecraftofprogrammingandonverification.Wewanttogivestudents
athoroughunderstandingofhowonethinksaboutprogrammingasaproblemsolv-
ingmethodandhowonecanbesurethatprogramsarecorrect(well,youcannever
be completely sure, but we show how you can provide convincing evidence for
correctness).
Even though there are lots of books on numerical methods where many algo-
rithmshavea correspondingcomputerimplementation (see, e.g., [1, 3–6, 11, 16–
18,21,24,26–31] –thelatter two aretheonlytextsweknowthatapplyPython),
itisassumedthatthereader“canprogram”beforehand. Thepresentbookteaches
the craft of structured programmingalong with the fundamental ideas of numeri-
calmethods. Furthermore,wehavesofarnotfoundanyothernumericalmethods
book that has a strong emphasis on verifying implementations. In this book, unit
testingandcorrespondingtestfunctionsareintroducedearlyon. Wealsoputmuch
emphasisoncodingalgorithmsasfunctions,asopposedto“flatprograms”,which
often dominate in the literature and among practitioners. Functions are reusable
becausetheyutilizethegeneralformulationofamathematicalalgorithmsuchthat
itbecomesapplicabletoalargeclassofproblems.
Therearealsonumerousbooksoncomputerprogramming,buttoourknowledge
only one [13] that aims to teach how to think about programming in the context
of numerical methods and scientific applications. That book [13] has its primary
focus on teaching Python and is a very comprehensive introduction to Python as
alanguageandthethinkingaboutprogrammingasacomputerscientist. Sometimes
one needs a text that does not go so deep into the language-specific details, but
instead targets theshortest path to reliablemathematical problemsolving through
programming.Withthisattitudeinmind,alotoftopicswereleftoutofthepresent
book, simply because they were not strictly needed in the mathematical problem
solving process. Examples of such topics are object-oriented programming and
Pythondictionaries(ofwhichthelatteromissionispossiblysubjecttomoredebate).
Ifyoufindthepresentbooktooshallow,[13]mightbetherightchoiceforyou.That
sourceshouldalsoworknicelyasamorein-depthsuccessorofthepresenttext.
Whenever the need for a structured introduction to programming arises in sci-
enceandengineeringcourses,thisbookmaybeyouroption,eitherforself-studyor
foruseinorganizedteaching.Thethinking,habits,andpracticecoveredinacouple
ofhundredpageswillputreadersinafirmpositionforutilizingandunderstanding
thepowerofcomputersforproblemsolvinginscienceandengineering.
Preface ix
Supplementary materials Allprogramanddatafilesreferredtointhisbookare
availablefromthebook’sprimarywebsite: http://hplgit.github.io/prog4comp/.
Acknowledgments First of all, we want to thank all students who attended the
coursesFM1006Modellingandsimulationofdynamicsystems,FM1115Scientific
Computing,FB1012MathematicsIandFB2112PhysicsattheUniversityCollege
ofSoutheastNorwayoverthelastcoupleofyears. Theyworkedtheirwaythrough
earlyversionsofthistextandgaveusconstructiveandpositivefeedbackthathelped
uscorrecterrorsandimprovethebookinsomanyways. Specialacknowledgement
goes to Guandong Kou and Edirisinghe V. P. J. Manjula for their careful reading
ofthemanuscriptandconstructivesuggestionsforimprovement.Thecarefulproof
readingbyYapiDonatienAchouisalsohighlyappreciated. Wethankallourgood
colleaguesattheUniversityCollegeofSoutheastNorway,UniversityofOslo,and
SimulaResearchLaboratoryfortheircontinuedsupportandinterest, enlightening
discussions, andforprovidingsuchaninspiringenvironmentforteachingandsci-
ence. In particular, Svein Linge is thankful to Marius Lysaker for their fruitful
collaboration on introducing programmingas an integralpart of mathematics and
physics bachelor courses at the University Collegeof Southeast Norway. Finally,
theauthorsmustthanktheSpringerteamwithDr.MartinPeters,Thanh-HaLeThi,
andYvonneSchlatterfortheeffectiveeditorialandproductionprocess.
ThetextwaswrittenintheDocOnce1 [12]markuplanguage,whichallowedus
toworkwithasingletextsourceforboththePythonandtheMatlabversionofthis
book,andtoproducevariouselectronicversionsofthebook.
December2015 SveinLingeandHansPetterLangtangen
1https://github.com/hplgit/doconce