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

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

Bild der Seite - 40 -

Bild der Seite - 40 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text der Seite - 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()
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2016
Sprache
englisch
Lizenz
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
248
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