Seite - 11 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 11 -
Text der Seite - 11 -
1.4 APythonProgramwithVectorizationandPlotting 11
Fig. 1.1 Plot generated by the scriptball_plot.py showing the vertical position of the ball at
a thousandpoints in time
andendingwithstop. Theexpressionlinspace(0, 1, 1001)creates 1001co-
ordinates between 0 and 1 (including both 0 and 1). Themathematically inclined
readerwillnotice that1001coordinatescorrespondto1000equal-sized intervals in
Œ0;1 and that thecoordinatesare thengivenby ti D i=1000 (i D0;1;:: :;1000).
Thevalue returned fromlinspace (beingstored int) is anarray, i.e., acollec-
tion of numbers. Whenwe start computingwith this collection of numbers in the
arithmetic expressionv0*t - 0.5*g*t**2, the expression is calculated for every
number int (i.e., every ti for i D 0;1;:: :;1000), yielding a similar collection of
1001numbers in the resulty. That is,y is alsoanarray.
This technique of computing all numbers “in one chunk” is referred to as vec-
torization.Whenit canbeused, it isveryhandy,sinceboth theamountofcodeand
computation time is reduced compared towriting a corresponding for or while
loop (Chapter2) fordoing thesame thing.
Theplottingcommandsaresimple:
1. plot(t, y)meansplottingall theycoordinatesversusall thetcoordinates
2. xlabel(’t (s)’)places the textt (s)on thex axis
3. ylabel(’y (m)’)places the texty (m)on they axis
At this stage, you are strongly encouraged to do Exercise 1.4. It builds on the
example above, but ismuch simpler bothwith respect to themathematics and the
amountofnumbers involved.
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