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

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

Bild der Seite - 151 -

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

Text der Seite - 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
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