Page - 5 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 5 -
Text of the Page - 5 -
1.2 APythonProgramwithVariables 5
To download Anaconda, you must pick the Anaconda version suitable for your
machine (Windows/Mac/Linux) and choose which version of Python you want
(3.6 is used for this book). When the download has completed, proceed with the
installationofAnaconda.
After installation, you may want to (search for and) start up Spyder to see what
it looks like (see also Appendix A). Spyder is an excellent tool for developing
Pythoncode.So,unlessyouhavegoodreasonstochooseotherwise,werecommend
Spyder tobeyourmain“working”environment,meaningthat to read,writeandrun
code you start Spyder and do it there. Thus, it is a good idea to make Spyder easy
accessibleonyourmachine.
With Anaconda installed, the only additional package you need to install is
Odespy.3 Odespy is relevant for thesolvingofdifferentialequations thatwetreat in
Chaps.8and9.
In Appendix A you will find more information on the installation and use of
Python.
1.2 APythonProgramwithVariables
Our first example regards programming a mathematical model that predicts the
height of a ball thrown straight up into the air. From Newton’s 2nd law, and
by assuming negligible air resistance, one can derive a mathematical model that
predicts theverticalpositiony of the ball at time t:
y=v0t−0.5gt2.
Here,v0 is the initialupwardsvelocityandg is theaccelerationofgravity,forwhich
9.81ms−2 is a reasonable value (even if it depends on things like location on the
earth).
With this formula at hand, and when v0 is known, you may plug in a value for
timeandgetout thecorrespondingheight.
1.2.1 TheProgram
Letusnext lookat aPythonprogramforevaluating this simple formula.Todo this,
we need some values for v0 and t, so we pick v0 = 5ms−1 and t = 0.6s (other
choiceswouldof coursehave been just as good).Assume the programis contained
as text inafile namedball.py, reading
# Program for computing the height of a ball in vertical motion
v0 = 5 # Initial velocity
g = 9.81 # Acceleration of gravity
3 The original version of Odespy (https://github.com/hplgit/odespy) was written in Python 2.7 by
H.P. Langtangen and L. Wang. However, since the sad loss of Prof. Langtangen in 2016, Ode-
spy has been updated to Python 3.6 (https://github.com/thomasantony/odespy/tree/py36/odespy),
thanks to Thomas Antony. Thisversion is theone used herein.
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