Page - 69 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 69 -
Text of the Page - 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.
back to the
book Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik