Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Page - 11 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 11 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Image of the Page - 11 -

Image of the Page - 11 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 11 -

1.4 APythonProgramwithVectorizationandPlotting 11 Fig. 1.1 Plot generated by the scriptball_plot.py showing the vertical position of the ball at a thousandpoints in time andendingwithstop. Theexpressionlinspace(0, 1, 1001)creates 1001co- ordinates between 0 and 1 (including both 0 and 1). Themathematically inclined readerwillnotice that1001coordinatescorrespondto1000equal-sized intervals in Ε’0;1 and that thecoordinatesare thengivenby ti D i=1000 (i D0;1;:: :;1000). Thevalue returned fromlinspace (beingstored int) is anarray, i.e., acollec- tion of numbers. Whenwe start computingwith this collection of numbers in the arithmetic expressionv0*t - 0.5*g*t**2, the expression is calculated for every number int (i.e., every ti for i D 0;1;:: :;1000), yielding a similar collection of 1001numbers in the resulty. That is,y is alsoanarray. This technique of computing all numbers β€œin one chunk” is referred to as vec- torization.Whenit canbeused, it isveryhandy,sinceboth theamountofcodeand computation time is reduced compared towriting a corresponding for or while loop (Chapter2) fordoing thesame thing. Theplottingcommandsaresimple: 1. plot(t, y)meansplottingall theycoordinatesversusall thetcoordinates 2. xlabel(’t (s)’)places the textt (s)on thex axis 3. ylabel(’y (m)’)places the texty (m)on they axis At this stage, you are strongly encouraged to do Exercise 1.4. It builds on the example above, but ismuch simpler bothwith respect to themathematics and the amountofnumbers involved.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2016
Language
English
License
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
248
Keywords
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Category
Informatik
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python