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 - 65 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 65 -

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

Text of the Page - 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()
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