Page - 12 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 12 -
Text of the Page - 12 -
12 1 TheFirstFewSteps
Whataboutunits?
The observant reader has noticed that the handling of quantities inball.py
didnot includeunits, even thoughvelocity (v0), acceleration(g) and time(t)
of course do have the units of ms−1, ms−2, and s, respectively. Even though
therearetoolsa inPythontoincludeunits, it isusuallyconsideredoutofscope
inabeginner’sbookonprogramming.Soalso in thisbook.
a See, e.g., https://github.com/juhasch/PhysicalQuantities, https://github.com/hgrecco/pint
and https://github.com/hplgit/parampool ifyou are curious.
1.3 APythonProgramwithaLibraryFunction
Imagine you stand on a distance, say 10.0m away, watching someone throwing a
ball upwards. A straight line from you to the ball will then make an angle with the
horizontalthat increasesanddecreasesastheballgoesupanddown.Letusconsider
the ball at a particular moment in time, at which it has a height of 10.0m. What is
theangleof the line then?
Well,wedoknow(with,orwithout,acalculator)thattheansweris45◦.However,
when learning to code, it is generally a good idea to deal with simple problems
withknownanswers.Simplicityensures that theproblemiswell understoodbefore
writing any code. Also, knowing the answer allows an easy check on what your
codinghasproducedwhen theprogramis run.
Before thinking of writing a program, one should always formulate the algo-
rithm, i.e., the recipe for what kind of calculations that must be performed. Here,
if the ball isx m away andy m up in the air, it makes an angle θ with the ground,
where tanθ=y/x.Theangle is then tan−1(y/x).
The Program Let us make a Python program for doing these calculations. We
introduce names x and y for the position data x and y, and the descriptive name
anglefor theangleθ.Theprogramisstoredinafileball_angle_first_try.py:
x = 10.0 # Horizontal position
y = 10.0 # Vertical position
angle = atan(y/x)
print((angle/pi)*180)
Before we turn our attention to the running of this program, let us take a look
at one new thing in the code. The line angle = atan(y/x), illustrates how the
functionatan, corresponding to tan−1 in mathematics, is called with the ratioy/x
as argument. The atan function takes one argument, and the computed value is
returned from atan. This means that where we see atan(y/x), a computation
is performed (tan−1(y/x)) and the result “replaces” the text atan(y/x). This is
actually no more magic than if we had written just y/x: then the computation of
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