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

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

Bild der Seite - 170 -

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

Text der Seite - 170 -

170 6 ComputingIntegralsandTestingCode Fig. 6.4 Illustration of the rectangle method withevaluating the rectangle height byeither the left or right point Exercise6.7:WriteTest Functions for ∫4 0 √ xdx We want to test how the trapezoidal function works for the integral ∫4 0 √ xdx. Two of the tests in test_trapezoidal.py are meaningful for this integral. Compute by hand the result of using two or three trapezoids and modify the test_trapezoidal_one_exact_result function accordingly. Then modify test_trapezoidal_conv_rate tohandle thesquare root integral. Filename:test_trapezoidal3.py. Remarks The convergence rate test fails. Printing out r shows that the actual convergencerate for this integral is1.5andnot2.Thereason is that theerror in the trapezoidal method6 is −(b−a)3n−2f ′′(ξ) for some (unknown) ξ ∈ [a,b]. With f(x)=√x,f ′′(ξ)→−∞asξ →0,pointingtoapotentialproblemin thesizeof theerror.Runninga testwitha>0, say ∫4 0.1 √ xdx showsthat theconvergencerate is indeedrestored to2. Exercise6.8:RectangleMethods Themidpointmethoddividestheintervalofintegrationintoequal-sizedsubintervals andapproximatestheintegralineachsubintervalbyarectanglewhoseheightequals the function value at the midpoint of the subinterval. Instead, one might use either the left or right end of the subinterval as illustrated in Fig.6.4. This defines a rectangle method of integration. The height of the rectangle can be based on the leftor rightendor themidpoint. a) Write a functionrectangle(f, a, b, n, height=’left’) for computing an integral ∫b a f(x)dx by the rectangle method with height computed based on thevalueofheight,which is eitherleft,right, ormid. b) Write three test functions for the three unit test procedures described in Sect.6.6.2. Make sure you test for height equal to left, right, and mid. Youmaycall themidpoint functionforcheckingtheresultwhenheight=mid. 6 http://en.wikipedia.org/wiki/Trapezoidal_rule#Error_analysis.
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