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 - 65 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 65 -

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

Text der Seite - 65 -

3.2 ThewhileLoop 65 An example of how to use break and continue can be found in Sect.5.2 (times_tables_4.py). The break and continue statements may also be used in while loops, to be treatednext. 3.2 ThewhileLoop Theotherbasic loopconstruction inPythonis thewhile loop,whichrunsas longas a condition isTrue.Let us movedirectly to an example,and explainwhat happens there,beforeweconsider the loopmoregenerally. 3.2.1 Example:FindingtheTimeofFlight To demonstrate awhile loop in action, we will make a minor modification of the case handled withball_plot.py in Sect.1.5. Now, we choose to find the time of flight for theball. The Case Assume the ball is thrown with a slightly lower initial velocity, say 4.5ms−1, while everything else is kept unchanged. Since we still look at the first second of the flight, the heights at the end of the flight will then become negative. However, this only means that the ball has fallen below its initial starting position, i.e., theheightwhereit left thehand,so there isnothingwrongwith that. Inanarray y,wewill thenhaveaseriesofheightswhichtowards theendofybecomenegative. Asbefore,wewill alsohaveanarraytwithall the times forcorrespondingheights iny. TheProgram Inaprogramnamedball_time.py,wemayfind the timeofflight as the time when heights switch from positive to negative.The programcould look like this import numpy as np v0 = 4.5 # Initial velocity g = 9.81 # Acceleration of gravity t = np.linspace(0, 1, 1000) # 1000 points in time interval y = v0*t - 0.5*g*t**2 # Generate all heights # Find index where ball approximately has reached y=0 i = 0 while y[i] >= 0: i = i + 1 # Since y[i] is the height at time t[i], we do know the # time as well when we have the index i... print(’Time of flight (in seconds): {:g}’.format(t[i])) # We plot the path again just for comparison import matplotlib.pyplot as plt plt.plot(t, y) plt.plot(t, 0*t, ’g--’) plt.xlabel(’Time (s)’) plt.ylabel(’Height (m)’) plt.show()
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