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

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

Image of the Page - 148 -

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

Text of the Page - 148 -

148 6 ComputingIntegralsandTestingCode 6.4.3 SpeedupGainedwithVectorization Nowthatwehavecreatedfaster,vectorizedversionsof thefunctions, it isof interest tomeasurehowmuchfaster theyare.Restrictingourselves to themidpointmethod, wemightproceedas: import timeit from integration_methods_vec import midpoint as midpoint_vec from midpoint import midpoint from numpy import exp v = lambda t: 3*t**2*exp(t**3) t = timeit.Timer(’midpoint(v, 0, 1, 1000000)’, \ setup=’from __main__ import midpoint, v’) time_midpoint = t.timeit(10) print(’Time, midpoint: {:g} seconds’.format(time_midpoint)) # Vectorized version t = timeit.Timer(’midpoint_vec(v, 0, 1, 1000000)’, \ setup=’from __main__ import midpoint_vec, v’) time_midpoint_vec = t.timeit(10) print(’Time, midpoint vec: {:g} seconds’.format(time_midpoint_vec)) print(’Efficiency factor: {:g}’.format(time_midpoint/time_midpoint_vec)) Running theprogramgives Time, midpoint: 19.6083 seconds Time, midpoint vec: 0.868379 seconds Efficiency factor: 22.5804 We see that the vectorized version is about 20 times faster. The results for the trapezoidal method are very similar, and the factor of about 20 is independent of thenumberof intervals. 6.5 RateofConvergence We have seen that the numerical integration error drops when the number of sub- intervalsn is increased (causing each sub-interval to become smaller). This is fine and in linewithourexpectations,but some importantdetails shouldbe added. Asymptotic Behavior of the Integration Error It is known that, if only the size hof thesub-intervals is small enough,numerical integrationmethodstypicallygive anerror E=Khr , (6.21) whereK is anunknownconstant,while theconvergencerate r is aknownconstant that depends on the method. When a method has convergence rate r, it is known as an r-th order method. A large r is beneficial, sinceE then drops quicker when h→0.
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