Page - 72 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 72 -
Text of the Page - 72 -
72 3 LoopsandBranching
Getting indicesright
To implement the traversing of arrays with loops and indices, is often
challengingtoget right.Youneedtounderstandthestart, stopandstep length
values for the loop variable, and also how the loop variable (possibly) enters
expressions inside the loop. At the same time, however, it is something that
programmers do often, so it is important to develop the right skills on these
matters.
You are encouraged to test your understanding of the search procedure
inball_max_height.pyby doing Exercise 3.9. That exercise will ask you
to compare what you get βby handβ to printouts from the code. It is of
fundamentalimportance toget thisprocedureasanestablishedhabitofyours,
sodo theexercise rightnow!
3.3.4 Example:RandomWalkinTwoDimensions
We willnowturntoanexamplewhichrepresentsthecoreofso-calledrandomwalk
algorithms.Theseare used in manybranchesof science andengineering, including
suchdifferentfieldsasmaterialsmanufacturingandbrain research.
The procedure we will consider, is to walk a series of equally sized steps, and
for each of those steps, there should be the same probability of going to the north
(N), east (E), south (S), or west (W). No other directions are legal. How can we
implementsuchan action in acomputerprogram?
To prepare our minds for the coding, it might be useful to first reflect uponhow
this could be done for real. One way, is to use a deck of cards, letting the four suits
correspondto thefourdirections:clubs toN,diamondstoE,hearts toS,andspades
to W, for instance. We draw a card, perform the corresponding move, and repeat
the process a large numberof times. The resulting path mimics, e.g., a typical path
followedbya diffusingmolecule.
Inacomputerprogram,wecannotdrawcards,butwecandrawrandomnumbers.
So, we may use a loop to repeatedlydraw a randomnumber,and dependingon the
number, we update the coordinates of our location. There are many ways to draw
random numbers and βtranslateβ them into our four directions, and the technical
detailswill typicallydependontheprogramminglanguage.However,our technique
hereisuniversal:wedrawarandomnumberfromtheinterval[0,1)andlet [0,0.25)
correspond to N, [0.25,0.5) to E, [0.5,0.75) to S, and [0.75,1) to W. We decide
to simulate 1000 steps, each of length 1 (e.g., meter), starting from Origo in our
coordinate system. To enable plotting our path, we use two arrays for storing the
coordinate history, one for the x-coordinates and one for the corresponding y-
coordinates.
Thesuggestedcoderandom_walk_2D.py then reads
import random
import numpy as np
import matplotlib.pyplot as plt
N = 1000 # number of steps
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik