Page - 34 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 34 -
Text of the Page - 34 -
34 1 TheFirstFewSteps
elegantly in the code, since it is possible to (put simply) try some statements,
and if they go wrong, rather run some other code lines! This way, an exception
is handled, and an unintended program stop (βcrashβ) is avoided. More about
exceptionhandling inSect.5.2.
Testing Code When a program finally runs without error messages, it might be
tempting to think that Ah..., I am finished!. But no! Then comes program testing,
youneedtoverify that theprogramdoesthecomputationsasplanned.This isalmost
an art and may take more time than to develop the program, but the program is
useless unless you have much evidence showing that the computationsare correct.
Also,havingasetof (automatic) tests saveshugeamountsof timewhenyoufurther
developtheprogram.
VerificationVersusValidation
Verification is important, but validation is equally important. It is great if
yourprogramcan do the calculationsaccording to the plan,but is it the right
plan? Put otherwise, you need to check that the computations run correctly
accordingto the formulayou havechosen/derived.This is verification: doing
thethingsright.Thereafter,youmustalsocheckwhethertheformulayouhave
chosen/derivedis the right formula for the case you are investigating.This is
validation:doing the right things.
In the present book, it is beyond scope to question how well the mathe-
maticalmodelsdescribea givenphenomenonin natureor engineering,as the
answerusually involvesextensiveknowledgeof theapplicationarea.We will
therefore limit our testing to theverificationpart.
1.8 ConcludingRemarks
1.8.1 ProgrammingDemandsYoutoBeAccurate!
In this chapter, you have seen some examples of how simple things may be done
in Python. Hopefully,you have tried to do the exampleson your own. If you have,
most certainly you have discovered that what you write in the code has to be very
accurate.
Forexample, in ourprogramball_plot.py, wecalledlinspace in thisway
t = np.linspace(0, 1, 1001)
If thishad ratherbeenwritten
t = np.linspace[0, 1, 1001)
we would have got an error message ([was used instead of (), even if you and I
wouldunderstandthemeaningperfectlywell!
Remember that it isnot a humanthat runsyourcode, it is a machine.Therefore,
evenif themeaningofyourcode looksfine toahumaneye, it still has tocomply in
detail to the rules of the programming language. If not, you get warnings and error
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