Page - 20 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 20 -
Text of the Page - 20 -
20 1 TheFirstFewSteps
t = np.linspace(0, 1, 1001)
y = v0*t - 0.5*g*t**2
plt.plot(t, y) # plots all y coordinates vs. all t coordinates
plt.xlabel(βt (s)β) # places the text t (s) on x-axis
plt.ylabel(βy (m)β) # places the text y (m) on y-axis
plt.show() # displays the figure
This program produces a plot of the vertical position with time, as seen in
Fig.1.1. As you notice, the code lines fromtheball.pyprogramin Sect.1.2 have
notchangedmuch,but theheight isnowcomputedandplottedfora thousandpoints
in time!
Let us take a closer look at this program. At the top, we recognize the import
statements
import numpy as np
import matplotlib.pyplot as plt
As we know by now, these statements imply that items from numpy and
matplotlib.pyplotmustbe prefixedwithnpandplt, respectively.
The linspaceFunction Next, there is a call to the functionlinspace from the
numpy library. When n evenly spaced floating point numbers are sought on an
interval [a,b],linspacemaygenerallybecalled like this:
np.linspace(a, b, n)
Thismeans that thecall
t = np.linspace(0, 1, 1001)
creates 1001 coordinates between 0 and 1, inclusive at both ends. The mathemati-
cally inclined reader might agree that 1001 coordinates correspond to 1000 equal-
Fig. 1.1 Plot generated by the script ball_plot.py showing the vertical position of the ball
(computed for a thousand points in time)
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