1.2 A Python Program with Variables
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
installation of Anaconda.
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
Python code. So, unless you have good reasons to choose otherwise, we recommend
Spyder to be your main “working” environment, meaning that to read, write and run
code you start Spyder and do it there. Thus, it is a good idea to make Spyder easy
accessible on your machine.
With Anaconda installed, the only additional package you need to install is
Odespy. 3 Odespy is relevant for the solving of differential equations that we treat in
Chaps. 8 and 9.
In Appendix A you will find more information on the installation and use of
Python.
1.2 A Python Program with Variables
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 the vertical position y of the ball at time t:
y = v 0 t − 0.5gt
2 .
Here, v 0 is the initial upwards velocity and g is the acceleration of gravity, for which
9.81 ms −2 is a reasonable value (even if it depends on things like location on the
earth).
With this formula at hand, and when v 0 is known, you may plug in a value for
time and get out the corresponding height.
1.2.1 The Program
Let us next look at a Python program for evaluating this simple formula. To do this,
we need some values for v 0 and t, so we pick v 0 = 5 ms −1 and t = 0.6 s (other
choices would of course have been just as good). Assume the program is contained
as text in a file named ball.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, Odespy has been updated to Python 3.6 (https://github.com/thomasantony/odespy/tree/py36/odespy),
thanks to Thomas Antony. This version is the one used herein.
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
installation of Anaconda.
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
Python code. So, unless you have good reasons to choose otherwise, we recommend
Spyder to be your main “working” environment, meaning that to read, write and run
code you start Spyder and do it there. Thus, it is a good idea to make Spyder easy
accessible on your machine.
With Anaconda installed, the only additional package you need to install is
Odespy. 3 Odespy is relevant for the solving of differential equations that we treat in
Chaps. 8 and 9.
In Appendix A you will find more information on the installation and use of
Python.
1.2 A Python Program with Variables
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 the vertical position y of the ball at time t:
y = v 0 t − 0.5gt
2 .
Here, v 0 is the initial upwards velocity and g is the acceleration of gravity, for which
9.81 ms −2 is a reasonable value (even if it depends on things like location on the
earth).
With this formula at hand, and when v 0 is known, you may plug in a value for
time and get out the corresponding height.
1.2.1 The Program
Let us next look at a Python program for evaluating this simple formula. To do this,
we need some values for v 0 and t, so we pick v 0 = 5 ms −1 and t = 0.6 s (other
choices would of course have been just as good). Assume the program is contained
as text in a file named ball.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, Odespy has been updated to Python 3.6 (https://github.com/thomasantony/odespy/tree/py36/odespy),
thanks to Thomas Antony. This version is the one used herein.
