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

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

Image of the Page - 40 -

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

Text of the Page - 40 -

40 2 BasicConstructions N = 5 x = 0 for i in range(1, N+1): x += 2*i print x Executing this codewill print thenumber30 to the screen.Note inparticular how theaccumulation variablex is initialized to zero. Thevalueofx thengets updatedwith each iteration of the loop, and not until the loop is finishedwill xhave the correct value. Thisway of building up the value is very common in programming, somake sure you understand it by simulating the code segment abovebyhand. It is a techniqueusedwith loops inanyprogramminglanguage. 2.4 WhileLoops Pythonalsohasanotherstandard loopconstruction, thewhile loop, doingiterations with a loop indexverymuch like thefor loop. To illustratewhat sucha loopmay looklike,weconsideranothermodificationofball_plot.pyinSect.1.4.Wewill now change it so that it finds the time of flight for the ball. Assume the ball is thrownwith a slightly lower initial velocity, say4:5ms 1,while everythingelse is keptunchanged. Sincewestill lookat thefirstsecondoftheflight, theheightsat the endof theflightbecomenegative.However, thisonlymeans that theballhas fallen below its initial starting position, i.e., the heightwhere it left the hand, so there is no problemwith that. In our array ywewill then have a series of heightswhich towards theendofybecomenegative. Letus, in aprogramnamedball_time.py find the timewhenheights start to get negative, i.e., when the ball crossesy D 0. Theprogramcould look like this from numpy import linspace v0 = 4.5 # Initial velocity g = 9.81 # Acceleration of gravity t = linspace(0, 1, 1000) # 1000 points in time interval y = v0*t - 0.5*g*t**2 # Generate all heights # Find where the ball hits y=0 i = 0 while y[i] >= 0: i += 1 # Now, y[i-1]>0 and y[i]<0 so let’s take the middle point # in time as the approximation for when the ball hits h=0 print "y=0 at", 0.5*(t[i-1] + 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"
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