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

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

Image of the Page - 152 -

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

Text of the Page - 152 -

152 6 ComputingIntegralsandTestingCode criterionis that theexactsolutionisknown.Theothercriterionis that thenumerical algorithm should produce the exact solution, within machine precision, for any chosenn. The second criterion may seem strange at first, but take the trapezoidalmethod, for example, and consider an integral like ∫b a (6x−4)dx. The integrand is here a straight line, and if you sketch it in a coordinate system along with some relevant trapezoids,yourealize that the topmostsideofeach trapezoidcomesexactlyon the straightline.Thisis thecaseforwhatevernumbernoftrapezoidsyoumaychooseto use.Thus, the trapezoidalmethodshouldsolve thatproblemwithoutanynumerical error.4 We can thereforepicksomelinear functionandconstructa test function that checks for equality between the exact solution and the numerical approximation producedbyour implementationof the trapezoidalmethod. Note that, when testing, numbers should not just be taken out of the air. For example, a specific test case can be ∫4.4 1.2 (6x − 4)dx. This integral involves an “arbitrary” interval [1.2,4.4]andan “arbitrary” linear functionf(x)=6x−4.By “arbitrary”, we mean expressions where the special numbers 0 and 1 are avoided, since these have special properties in arithmetic operations (e.g., forgetting to multiply is equivalent to multiplying by 1, and forgetting to add is equivalent to adding0). DemonstratingCorrect Convergence Rates Also for these unit tests we choose problemsfor which the exact solution is known.However,contrary to the previous testprocedure,wenowworkwithproblemsforwhich thenumericalalgorithmdoes not give zero approximation error. Normally, unit tests must be based on that kind ofproblems.Thus, the answer we get fromourcodewill containan approximation error, and since we know the exact solution,we may compute the size of this error. Unfortunately, this isnot toohelpful, sincewe have little chance telling if this error iswhatwe should havegot for theparticularnused! Nowtheconvergenceratecomesinhandy.Ifweknow(orhavereasontoassume) that the numerical error has a certain asymptotic behavior whenn→∞, we know atwhatrate thenumericalerrorshouldbereduced.Theideaofacorrespondingunit test is then to run the algorithm for somenvalues, compute the error (the absolute value of the difference between the exact solution and the one produced by the numerical method), and check that the error has approximately correct asymptotic behavior.For the trapezoidalandmidpointmethodsinparticular, thismeansthat the error shouldbecomeproportional ton−2 whenn→∞. Let us develop a more precise method for such unit tests based on convergence rates. Consider a set of q+1 experiments with variousn: n0,n1,n2,.. .,nq. We compute the corresponding errors E0,.. .,Eq. For two consecutive experiments, number i and i−1,we have theerrormodel Ei =Cn−ri , (6.26) Ei−1 =Cn−ri−1 . (6.27) 4 In fact, so would themidpoint method!This isbecause, for each rectangle, the error toeach side of the midpoint is equally large withopposite signs, meaning that they cancel each other.
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