How to Design Programs: An Introduction to Computing and Programming
[Go to
first
,
previous
,
next
page;
contents
;
index
]
Section 23
Mathematical Examples
Applying mathematics to realworld problems requires programs that implement mathematical functions. In many cases, the
programs also employ functions that consume and produce functions. Mathematics is therefore a great starting point for
practicing programming with functions and, more generally, for creating abstract functions.
The first subsection covers sequences and series, a key element of mathematics. The second section discusses integration,
which relies heavily on series. Finally, the third section introduces function differentiation.
23.1
Sequences and Series
In prealgebra and algebra, we encounter
sequences
(also known as progressions) of numbers. Here are three examples:
1.
0, 2, 4, 6, 8;
2.
1, 3, 5, 7, 9;
3.
5, 10, 15, 20, 25.
The first two enumerate the first five even and odd natural numbers, respectively; the last one lists the first five positive
integers, evenly divisible by 5. Sequences can also be infinite:
1.
0, 2, 4, 6, 8,
...
;
2.
1, 3, 5, 7, 9,
...
;
3.
5, 10, 15, 20, 25,
...
Following mathematical tradition, infinite sequences end in ``
...
'' and the reader must determine how to find more of the
terms in the sequence.
One way to understand sequences of numbers, especially infinite ones, is to match them up with an enumeration of the
natural numbers. For example, the even and odd (natural) numbers match up like this:
[curriculum3aZG2.gif]
It is easy to see from this table that every even number is 2 ·
i
for its index
i
and that an odd number is 2 ·
i
+ 1.
Both statements can be translated into simple Scheme functions:
file:///C/Documents%20and%20Settings/Linda%20Graue.
..How%20to%20Design%20Programs/curriculumZH29.html (1 of 13) [2/5/2008 4:51:10 PM]
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentHow to Design Programs: An Introduction to Computing and Programming
;; makeeven :
N
>
N
[even]
;; to compute the ith even number
(define (makeeven i)
(* 2 i))
;; makeodd :
N
>
N
[odd]
;; to compute the ith odd number
(define (makeodd i)
(+ (* 2 i) 1))
In short, functions from natural numbers to numbers are representations of infinite sequences.
A mathematical
series
is the sum of a sequence. The three finite sequences have the sums 20, 25, and 75, respectively. In the
case of infinite sequences it is often interesting to consider a finite portion, staring with the first one.
51
For example, adding
the first 10 even numbers yields 90, and adding the first 10 odd numbers yields 100. Computing a series is clearly a job for a
computer. Here are functions that add up the first
n
odd or even numbers, respectively, using
makeeven
and
makeodd
to compute the required numbers:
;; serieseven :
N
>
number
;; to sum up the first
;; n even numbers
(define (serieseven n)
(cond
[(= n 0) (makeeven n)]
[else (+ (makeeven n)
(serieseven ( n
1)))]))
;; seriesodd :
N
>
number
;; to sum up the first
;; n odd numbers
(define (seriesodd n)
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '07
 Fisler
 Gregory, graphing.ss

Click to edit the document details