Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 72 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 72 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Bild der Seite - 72 -

Bild der Seite - 72 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 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
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python