Seite - 7 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 7 -
Text der Seite - 7 -
1.3 APythonProgramwithaLibraryFunction 7
Butfirst awarning: there aremany things thatmust come together in the right
way for ball.py to run correctly on your computer. There might be problems
with your Python installation, with your writing of the program (it is very easy
to introduce errors!), or with the location of the file, just tomention some of the
most commondifficulties for beginners. Fortunately, such problems are solvable,
and if you do not understand how to fix the problem, ask somebody. Typically,
once you are beyond these common start-up problems, you canmove on to learn
programmingandhowprogramscandoalotofotherwisecomplicatedmathematics
foryou.
Wedescribe the first steps using theSpyder graphical user interface (GUI), but
youcanequallywelluseastandard texteditor forwriting theprogramanda termi-
nalwindow(TerminalonMac,PowerShellonWindows) for running theprogram.
Start upSpyder and type in each lineof theprogramball.pyshownearlier. Then
run theprogram.Moredetailed descriptionsofoperatingSpyder are found inAp-
pendixA.3.
If youhave had the necessary luck to get everything right, you should nowget
the number 1.2342 out in the rightmost lower window in the Spyder GUI. If so,
congratulations! You have just executed your first self-written computer program
inPython,andyouare ready togoonstudying thisbook!Youmay like to save the
programbeforemovingon (File,save as).
1.3 APythonProgramwithaLibraryFunction
Imagineyoustandonadistance, say10maway,watchingsomeone throwingaball
upwards. A straight line from you to the ball will thenmake an angle with the
horizontalthat increasesanddecreasesastheballgoesupanddown.Letusconsider
theball at aparticularmoment in time, atwhich it hasaheightof10m.
What is the angle of the line then? Again, this could easily bedonewith a cal-
culator, but we continue to address gentlemathematical problemswhen learning
to program. Before thinking of writing a program, one should always formulate
thealgorithm, i.e., the recipe forwhatkindofcalculations thatmustbeperformed.
Here, if the ball isxmaway andymup in the air, it makes an angle with the
ground,where tan Dy=x. Theangle is then tan 1.y=x/.
LetusmakeaPythonprogramfordoingthesecalculations.Weintroducenames
xandy for thepositiondatax andy, and thedescriptivenameangle for theangle
. Theprogramis stored inafileball_angle_first_try.py:
x = 10 # Horizontal position
y = 10 # Vertical position
angle = atan(y/x)
print (angle/pi)*180
Beforewe 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
function atan, corresponding to tan 1 in mathematics, is called with the ratio
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik