Page - 151 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 151 -
Text of the Page - 151 -
6.6 TestingCode 151
6.6.2 ProperTestProcedures
There are three serious ways to test the implementation of numerical methods via
unit tests:
1. Comparing with hand-computed results. Relevant for problems with few arith-
meticoperations, i.e., smalln.
2. Solving a problem without numerical errors. We know, for example, that the
trapezoidal rulemustbeexact for linear integrandfunctions.Theerrorproduced
by theprogrammust thenbezero (tomachineprecision).
3. Demonstrating correct convergence rates. When exact errors can be computed,
a strongtest is to letngrowandsee if theerrorapproacheszeroas fast as theory
predicts.Asstated previously, for the trapezoidalandmidpoint rules it isknown
that theerrordependsonnasn−2 whenn→∞.
Remark
When testing code, we usually choose computational problems for which
the exact solution is known. This is obviously a good idea, since it allows
the quality of approximate numerical answers to be judged. Do not forget,
however, that the exact solution is available because we deliberately chose
a problem with known exact solution. When we have finished testing (and
probably fixing) the code, our belief is that the code will work also for
problems with unknown exact solutions. Our strategy then, is to trust the
approximateanswer fromourcode.
Hand-Computed Results Let us use two trapezoids and compute the
integral∫1
0 v(t)dt, wherev(t)=3t2et 3
:
h (v(0)+v(0.5))
2 +h(v(0.5)+v(1))
2 =2.463642041244344,
whenh=0.5is thewidthofeachtrapezoid.Runningtheprogramgivesexactly the
sameresult.
Note that the exact solution is not involved here. We simply carry out the
numerical algorithm by hand, “independent” from the code. We should of course
get agreement between these handcalculationsand programoutput when the same
n isused.However,assumingwedogetagreement, thatnumericalanswermaystill
differ substantially from the exact solution to the problem.That is of no concern in
this test, as theaimis not togetasgoodanansweraspossible (potentiallyachieved
withalargen),butrathertocheckinasimplemannerwhetherthealgorithm“seems
tobe”correctly implemented.
Solving a Problem Without Numerical Errors The best unit tests for numerical
algorithms involve mathematical problems where we know the numerical result
beforehand. For these unit tests, we choose problems that fulfill two criteria. One
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