This preview shows pages 1–3. Sign up to view the full content.
Chapter 4
Exponential Growth
§
4.1
Powers
Userdefned Function,
function
declarations, preconditions and post conditions,
parameter lists, Formal and actual parameters, Functions that call other Functions,
scope rules, development through generalization.
§
4.2
Binomial Coeﬃcients
Weakening the precondition
There are a number of reasons why the builtin
sin
function is so handy. To begin with, it
enables us to compute sines
without having a clue
about the method used. It so happens that the
design of an accurate and eﬃcient sine function is somewhat involved. But by taking the “black
box” approach, we are able to be eﬀective
sin
users while being blissfully unaware of how the
builtin function works. All we need to know is that
sin
expects a real input value and that it
returns the sine of that value interpreted in radians.
Another advantage of
sin
can be measured in keystrokes and program readability. Instead
of disrupting the “real business” of a program with lengthy computethesine fragments, we
merely invoke
sin
as required. The resulting program is shorter and reads more like traditional
mathematics.
Most programming languages come equipped with a
library
of builtin functions. The design
ers of the language determine the library’s content by anticipating who will be using the language.
If that group includes scientists and engineers, then invariably there will be builtin functions for
the sine, cosine, log, and exponential functions because they are of central importance to work
in these areas.
It turns out that if you need a function that is not part of the builtin function library, then
you can write your own.
The art of being able to write eﬃcient, carefully organized functions
is an absolutely essential skill for the computational scientist because it suppresses detail and
permits a higher level of algorithmic thought.
To illustrate the mechanics of function writing we have chosen a set of examples that highlight
a number of important issues. On the continuous side we look at powers, exponentials, and logs.
These functions are monotone increasing and can be used to capture diﬀerent rates of growth.
115
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document116
Chapter 4. Exponential Growth
Factorials and binomial coeﬃcients are important for counting combinations. We bridge the
continuous/discrete dichotomy through a selection of problems that involve approximation.
4.1
Powers
To raise
x
to the
n
th power, in
Matlab
we use the expression
x
ˆ
n
. Some programming lan
guages, however, do not include a power operator and a programmer would have to write a code
fragment, such as the one below, to evaluate
x
n
:
xpower= 1;
for
k= 1:n
xpower= x*xpower; %
{
xpower = x
ˆ
k
}
end
Each pass through the loop raises the “current” power of
x
by one. Without the power operator, it
is not unreasonable to insert this singleloop calculation as required in a program that requires the
computation of a power in just a few places. However, it is not hard to imagine a situation where
exponentiations are required many times throughout a program. It is then a major inconvenience
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '07
 FAN/CHEW

Click to edit the document details