Seite - 29 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 29 -
Text der Seite - 29 -
2BasicConstructions
2.1 IfTests,ColonandIndentation
Very often in life, and in computer programs, the next action depends on the out-
come of a question starting with “if”. This gives the possibility to branch into
different types of action depending on some criterion. Let us as usual focus on
a specific example,which is the coreof so-called randomwalk algorithmsused in
awide rangeofbranches in scienceandengineering, includingmaterialsmanufac-
turingandbrainresearch. Theactionis tomoverandomlyto thenorth(N),east (E),
south (S), orwest (W)with the sameprobability. Howcanwe implement such an
action in life and inacomputerprogram?
We need to randomly draw one out of four numbers to select the direction in
which tomove. Adeck of cards can be used in practice for this purpose. Let the
four suits correspond to the fourdirections: clubs toN,diamonds toE, hearts toS,
and spades toW, for instance. Wedrawa card, perform the correspondingmove,
and repeat the process a large number of times. The resulting path is a typical
realizationof thepathofadiffusingmolecule.
In a computer program,we need to drawa randomnumber, and depending on
thenumber,update thecoordinatesof thepoint tobemoved.Therearemanyways
todrawrandomnumbersand translate theminto (e.g.) four randomdirections, but
the technical details usually dependon theprogramming language. Our technique
here is universal: wedrawa randomnumber in the interval Œ0;1/ and let Œ0;0:25/
correspond toN, Œ0:25;0:5/ to E, Œ0:5;0:75/ to S, and Œ0:75;1/ toW.Letx andy
29©TheAuthor(s)2016
S.Linge,H.P.Langtangen,Programming for Computations –Python,
Texts inComputational Science andEngineering15,DOI10.1007/978-3-319-32428-9_2
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik