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

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

Bild der Seite - 69 -

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

Text der Seite - 69 -

3.4 Testing 69 n D 4, the error is 0.1, butwithout the bug the error is 0.2! It is of course com- pletely impossible to tell if0.1 is therightvalueof theerror. Fortunately, increasing n shows that the error stays about0.3 in theprogramwith thebug, so the test pro- cedurewith increasingn andchecking that the error decreasespoints to aproblem in thecode. Let us look at another bug, this time in the mathematical algorithm: instead of computing 1 2 .f.a/Cf.b// as we should, we forget the second 1 2 and write 0.5*f(a) + f(b). The error forn D 4;40;400when computing R1:9 1:1 3t2et 3 dt goes like 1400, 107, 10, respectively,which lookspromising. Theproblem is that the right errors shouldbe369, 4.08, and0.04. That is, the error shouldbe reduced faster in the correct than in the buggy code. The problem, however, is that it is reduced in both codes, andwemay stop further testing and believe everything is correctly implemented. Unittesting Agoodhabit is to test small pieces of a larger code individually, one at a time. This is knownasunit testing. One identifies a (small) unit of thecode, and then onemakes a separate test for this unit. The unit test should be stand-alone in the sense that it can be runwithout the outcome of other tests. Typically, one algorithminscientificprograms is consideredasaunit. Thechallengewithunit tests in numerical computing is to deal with numerical approximation errors. A fortunate side effect of unit testing is that the programmer is forced to use functions tomodularize thecode into smaller, logicalpieces. 3.4.2 ProperTestProcedures There are three seriousways to test the implementation of numericalmethods via unit tests: 1. Comparing with hand-computedresults in a problemwith fewarithmetic oper- ations, i.e., smalln. 2. Solving a problem without numerical errors. Weknowthat the trapezoidal rule mustbeexactfor linearfunctions. Theerrorproducedbytheprogrammust then bezero (tomachineprecision). 3. Demonstrating correct convergence rates. A strong testwhenwecan compute exact errors, is to see how fast the error goes to zero asn grows. In the trape- zoidal andmidpoint rules it is known that the error depends on n as n 2 as n!1. Hand-computed results Let us use two trapezoids and compute the integralR1 0 v.t/,v.t/D3t2et3: 1 2 h.v.0/Cv.0:5//C 1 2 h.v.0:5/Cv.1//D2:463642041244344; whenhD0:5 is thewidthofthetwotrapezoids.Runningtheprogramgivesexactly the sameresult.
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