Mathematical constants like
and e or functions like
,
,
are difficult to compute. Since these functions are important for
many daily engineering applications, mathematicians have spent a lot of
time and energy looking for better ways to compute these functions. One
method is to replace a function with its Taylor series, which is, roughly
speaking, an infinitely long polynomial.
A Taylor series is the sum of a sequence of terms. In contrast to arithmetic or geometric sequences, the terms of a Taylor series depend on two unknowns: some variable x and the position i in the sequence. Here is the Taylor series for the exponential function:
That is, if we wish to compute
for any specific x, we
replace x with the number and determine the value of the series.
In other words, for a specific value of x, say, 1, the Taylor
series becomes an ordinary series, that is, a sum of some sequence of numbers:
While this series is the sum of an infinitely long sequence, it actually is a number, and it often suffices to add just a few of the first few terms to have an idea what the number is.
The key to computing a Taylor series is to formulate each term in the underlying sequence as a function of x and its position i. In our running example, the Taylor sequence for the exponential function has the shape
Assuming a fixed x, here is an equivalent Scheme definition:
;; e-taylor : N -> number (define (e-taylor i) (/ (expt x i) (! i)))The first function computes the term; the second computes the factorial of a natural number. To compute the value of;; ! : N -> number (define (! n) (cond [(= n 0) 1] [else (* n (! (sub1 n)))]))
Putting everything together, we can define a function that computes the xth power of e. Since the function requires two auxiliaries, we use a local:
(define (e-power x)
(local ((define (e-taylor i)
(/ (expt x i) (! i)))
(define (! n)
(cond
[(= n 0) 1]
[else (* n (! (sub1 n)))])))
(series 10 e-taylor)))
Exercise 23.3.6
Replace 10 by 3 in the definition of e-power and evaluate (e-power 1) by hand. Show only those lines that contain new applications of e-taylor to a number.
The results of e-power are fractions with large numerators and denominators. In contrast, Scheme's built-in exp function produces an inexact number. We can turn exact fractions into inexact numbers with the following function:
;; exact->inexact : number [exact] -> number [inexact]Test the function and add it to e-power's body. Then compare the results of exp and e-power. Increase the number of items in the series until the difference between the results is small. Solution
Exercise 23.3.7
Develop the function ln, which computes the Taylor series for the natural logarithm. The mathematical definition of the series is
This Taylor series has a value for all x that are greater than 0.
DrScheme also provides log, a primitive for computing the natural
logarithm. Compare the results for ln and log. Then use
exact->inexact (see exercise
) to get results
that are easier to compare. Solution
Exercise 23.3.8
Develop the function my-sin, which computes the Taylor series for sin, one of the trigonometric functions. The Taylor series is defined as follows:
It is defined for all x.
Hint: The sign of a term is positive if the index is even and negative
otherwise. Mathematicians compute
to determine the sign;
programmers can use cond instead. Solution
Exercise 23.3.9
Mathematicians have used series to determine the value of
for many
centuries. Here is the first such sequence, discovered by Gregory (1638-1675):
Define the function greg, which maps a natural number to the
corresponding term in this sequence. Then use series to determine
approximations of the value of
.
Note on
: The approximation improves as we increase the number of items
in the series. Unfortunately, it is not practical to compute
with
this definition. Solution