6
Computing Integrals and Testing Code
We now turn our prime attention to the solving of mathematical problems through
computer programming. A fundamentally important part of programming, is to
check that the written code works as intended. That is, the code must be tested. This
far, we have checked our coding in rather simple ways, e.g., by comparing to hand
calculations. Now, we will look at more powerful test strategies, while addressing
numerical computation of integrals.
There are many reasons to choose integration as our first application. Integration
is well known already from high school mathematics. Most integrals are not
tractable by pen and paper, and a computerized solution approach is both very much
simpler and much more powerful—you can essentially treat all integrals
b
a f (x)dx
in 10 lines of computer code!
Integration also demonstrates the difference between exact mathematics by pen
and paper and numerical mathematics on a computer. The latter approaches the
result of the former without any worries about rounding errors due to finite precision
arithmetics in computers (in contrast to differentiation, where such errors prevent us
from getting a result as accurate as we desire).
Finally, integration is thought of as a somewhat difficult mathematical concept
to grasp, and programming integration should greatly improve the understanding of
what integration really is and how it works.
Not only shall we understand how to use the computer to integrate, but we shall
also learn a series of good habits to ensure your computer work is of the highest
scientific quality. In particular, we will have a strong focus on how to write Python
code that is free of programming mistakes.
© The Author(s) 2020
S. Linge, H. P. Langtangen, Programming for Computations - Python,
Texts in Computational Science and Engineering 15,
https://doi.org/10.1007/978-3-030-16877-3_6
131
Précédent

- 152/350

Suivant