Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Page - 29 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 29 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Image of the Page - 29 -

Image of the Page - 29 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 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
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2016
Language
English
License
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
248
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python