Information Technology Reference
In-Depth Information
while i n1
s s Cf.aC ih /
i i C1
end while
The forthcoming sections provide more examples.
6.1.2
Integration
Suppose we want to integrate a function f.x/ from x D a to x D b with the aid
of the composite trapezoidal RULE (derived in Sect. 1.3). Mathematically, we can
express this method for computing an integral as follows:
Z b
2 f.b/C n X
iD1
f.x/dx h
2 f.a/C h
h D b a
n
hf . a C ih /;
:
(6.1)
a
This is a compact formulation that contains all the necessary information for calcu-
lating the integral on a computer. There are some steps from (6.1) to running code,
but you have hopefully already tried to implement methods such as (6.1) while
working with exercises in the introductory chapter. Now it is time to revise such
implementational work and adopt good habits.
Before thinking of an implementation, we should express a compact formula
such as (6.1) as an algorithm, i.e., as a set of steps that naturally translate into
similar steps in a computer code. The complete algorithm corresponding to the
mathematical formulation (6.1) can be expressed as follows:
ba
n
h D
s D 0
for i D 1;:::;n1
s s C
hf .a C
ih /
end for
s s C
h
2 f.b/
The nature of this algorithm is that we provide a , b , n ,and f.x/ as input and
get the answer s as output . Such input-output algorithms are often conveniently
expressed in a way that resembles the concept of functions or subprograms in
programming languages.
For example, we could write the algorithms in a notation such as
h
2 f.a/C
trapezoidal ( a , b , f , n )
... do something ... store final result in s
return s
This notation is hopefully self-explanatory for anyone having some experience with
computer programming; we explicitly indicate that a , b , f ,and n are input data and
s the output, i.e., the result of the algorithm.
 
Search WWH ::




Custom Search