Teach Yourself Scheme in Fixnum Days
[Go to
first
,
previous
,
next
page;
contents
;
index
]
Appendix C
Numerical techniques
Recursion (including iteration) combines well with Scheme's mathematical primitive procedures to
implement various numerical techniques. As an example, let's implement Simpson's rule, a procedure for
finding an approximation for a definite integral.
C.1
Simpson's rule
The definite integral of a function
f
(
x
) within an interval of integration [
a
,
b
] can be viewed as the
area
under the curve
representing
f
(
x
) from the lower limit
x
=
a
to the upper limit
x
=
b
. In other words, we
consider the graph of the curve for
f
(
x
) on the
x
,
y
plane, and find the area enclosed between that curve,
the
x
axis, and the
ordinates
of
f
(
x
) at
x
=
a
and
x
=
b
.
[tyschemeZG1.gif]
According to Simpson's rule, we divide the interval of integration [
a
,
b
] into
n
evenly spaced intervals,
where
n
is even. (The larger
n
is, the better the approximation.) The interval boundaries constitute
n
+ 1
points on the
x
axis, viz,
x
0
,
x
1
,
...
,
x
i
,
x
i
+1
,
...
,
x
n
, where
x
0
=
a
and
x
n
=
b
. The length of each
interval is
h
= (
b

a
)/
n
, so each
x
i
=
a
+
ih
. We then calculate the ordinates of
f
(
x
) at the interval
boundaries. There are
n
+ 1 such ordinates, viz,
y
0
,
...
,
y
i
,
...
,
y
n
, where
y
i
=
f
(
x
i
) =
f
(
a
+
ih
).
Simpson's rule approximates the definite integral of
f
(
x
) between
a
and
b
with the value
12
:
[tyschemeZG2.gif]
We define the procedure
integratesimpson
to take four arguments: the integrand
f
; the
x
values
at the limits
a
and
b
; and the number of intervals
n
.
(define integratesimpson
(lambda (f a b n)
;...
(1 of 9) [2/6/2008 11:43:05 AM]