Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 21 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 21 -

Bild der Seite - 21 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 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.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python