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

Page - 21 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 21 -

Image of the Page - 21 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 21 -

1.6 Plotting,PrintingandInputData 21 sizedintervalsin [0,1]andthat thecoordinatesarethengivenby ti = 1−01000i= i1000, i=0,1,.. .,1000. The object returned fromlinspace is an array, i.e., a certain collection of (in this case) numbers.Through the assignment, this array gets the namet. If we like, we may think of the arrayt as a collection of “boxes” in computer memory (each containinga number) that collectivelygoby thenamet (later,wewill demonstrate how these boxes are numbered consecutively from zero and upwards, so that each “box”maybe identifiedandused individually). Vectorization Whenwestart computingwitht in y = v0*t - 0.5*g*t**2 the right hand side is computed for every number in t (i.e., every ti for i = 0,1,.. .,1000),yieldingasimilarcollectionof1001numbersin theresulty,which (automatically)alsobecomesanarray! This technique of computing all numbers “in one chunk” is referred to as vectorization. When it can be used, it is very handy, since both the amount of code and computation time is reducedcompared to writing a correspondingloop16 (Chap.3) fordoingthe same thing. Plotting Theplottingcommandsarenew,but simple: plt.plot(t, y) # plots all y coordinates vs. all t coordinates plt.xlabel(’t (s)’) # places the text t (s) on x-axis plt.ylabel(’y (m)’) # places the text y (m) on y-axis plt.show() # displays the figure At this stage, you are encouraged to do Exercise 1.4. It builds on the example above,but is muchsimplerbothwith respect to the mathematicsand the amountof numbers involved. 1.6 Plotting,PrintingandInputData 1.6.1 PlottingwithMatplotlib Often, computationsandanalyses producedata that are best illustrated graphically. Thus,programminglanguagesusuallyhavemanygoodtoolsavailableforproducing andworkingwithplots, andPython isnoexception.17 In thisbook,weshall stick to theexcellentplotting libraryMatplotlib,whichhas becomethe standardplottingpackage in Python.Below, we demonstrate just a few of the possibilities that come with Matplotlib, much more information is found on theMatplotlibwebsite.18 16 It should be mentioned, though, that the computations are still done with loops “behind the scenes” (coded in C or Fortran). They generally run much quicker than the Python loops we write ourselves. 17 In Sect.9.2.4 we give a brief example ofhow plotsmay be turned intovideos. 18 https://matplotlib.org/index.html.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python