Information Technology Reference
In-Depth Information
s s Cx tanh x
s h.s C0:50 tanh 0C0:5 10 tanh 10/
Although the code is simple and to the point, it is not reusable on other occasions,
i.e., when you want to integrate a new function by the trapezoidal rule. You will
then need to write and test the integration method once more. This is a waste of
effort - perhaps not that much in the present example, but this is the simplest of all
examples. We believe that adopting sound programming styles is a good habit for
obtaining a reliable and efficient way of solving problems, even in simple problems
where any approach can work well.
The trapezoidal rule should, naturally, be implemented as indicated in all our
algorithms from Sect. 6.1.2 , namely as a Trapezoidal function (or subroutine)
where as many parameters as possible are represented by variables. This means
that you should never hardcode the function f.x/ and the integration limits a and
b as we did above. As soon as the function is well tested, it can be reused as a
working “black box” later. If you call this piece of software to integrate a function
and see that the result is wrong, you can be quite sure that the error is outside the
Trapezoidal function; it must be in the calling code. This principle of breaking up
a code into reusable and well-tested pieces is crucial for the efficient and reliable
development of software for more complicated problems.
As you need more sophisticated methods for numerical integration, you can
implement these as reusable functions. After some time you will have a collection
of functions for numerical integration. At that point you need to organize these func-
tions such that you can call the methods you want in an application in a convenient
way. The interface, i.e. the argument list, to the functions should be the same, if
possible. Furthermore, the functions should be available in a library such that there
is no need to copy the complete function codes to your application. The latter point
has two important advantages: It keeps the application code small, and there is only
one version (code) of the numerical integration function.
Suppose your collection of functions for numerical integration counts four methods:
-
Trapezoidal ( a , b , f , n )
-
Simpson ( a , b , f , n )
-
GaussLegendre ( a , b , f , n )
-
GaussLobatto ( a , b , f , n )
These functions have the same list of arguments. More sophisticated integration
methods, e.g. adaptive schemes, may need more input than a , b , f ,and n (see
page 260 ).
To make a library, we collect the code associated with the functions in a single file
or a collection of files (the file structure can depend on the programming language).
None of these files should contain a main program, only functions or subroutines.
Integrating a function numerically, utilizing this library, consists of writing a
program doing three things:
1. Enabling access to functionality in the library
2. Implementing the function f.x/ to be integrated
Search WWH ::




Custom Search