Seite - 19 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 19 -
Text der Seite - 19 -
1.5 APythonProgramwithVectorizationandPlotting 19
Here,pyplot is a module in thematplotlibpackage15 that is namedpltwhen
imported.Thus,whenimportedthisway,all itemsfrompyplotmusthavetheprefix
plt.Wewill sticktothis importandnamingstandardforpyplotalsoin thepresent
book,wheneverplotting is on the agenda.
1.4.5 TheModules/PackagesUsedinThisBook
Somereadersmightbe curious to knowwhich librariesare used in thisbook(apart
fromthemoduleswe makeourselves).Well, here theyare:
• math—see,e.g.,ball_angle.py,Sect.1.3.
• numpy—see,e.g.,check_functions.pyabove.
• matplotlib.pyplot—see,e.g.,ball_plot.py,Sect.1.5.
• random—see,e.g.,throw_2_dice.py inSect.2.4.
• sympy—see,e.g.,Sect.5.3.
• timeit—see,e.g.,Sect.5.6.
• sys—see,e.g.,Sect.7.2.2.
These libraries are all well known to Python programmers. The three first ones
(math, numpy and matplotlib.pyplot) are used repeatedly throughout the
text, while the remaining ones (random, sympy, timeit and sys) appear just
occasionally.
Not listed, are two modules that are used just once each, thekeywordmodule
(Sect.2.2) and theosmodule (Sect.9.2.4). It shouldbe mentioned that we also use
a package calledodespy (Sect.8.4.6), previously developed by one of the authors
(Langtangen).
1.5 APythonProgramwithVectorizationandPlotting
Wereturn to theproblemwhereaball is thrownupin theairandwehavea formula
fortheverticalpositionyoftheball.Sayweareinterestediny ateverymilli-second
for the first second of the flight. This requires repeating the calculation of y =
v0t−0.5gt2 one thousandtimes.Aswewill see, thecomputedheightsappearvery
informativewhen presentedgraphicallywith time, as opposed to a long printoutof
all the numbers.
The Program In Python, the calculations and the visualization of the curve may
bedonewith theprogramball_plot.py, reading
import numpy as np
import matplotlib.pyplot as plt
v0 = 5
g = 9.81
15 The matplotlib package (https://matplotlib.org/) comes with Anaconda. If you have not
installedAnaconda, you may have to installmatplotlib separately.
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