Log in Page Discussion History Go to the site toolbox

Exercise 3.59

From BluWiki

Section 3.5 Exercises

Exercise 3.59

part (a)

Define a procedure integrate-series that takes as input a stream a0, a1, a2, ... representing a power series and returns the stream a0, (1/2)a1, (1/3)a2, ... of coefficients of the non-constant terms of the integral of the series. (Since the result has no constant term, it doesn't represent a power series; when we use integrate-series, we will cons on the appropriate constant.)

The nth element (starting from 0) of (integrate-series stream) should be equal to the nth element of stream, divided by <math>(n+1)</math>.

(define (div-streams s1 s2)
  (stream-map / s1 s2))

(define (integrate-series stream)
  (div-streams stream (integers-starting-from 1)))
part (b)

Show how to generate the series for sine and cosine, starting from the facts that the derivative of sine is cosine and the derivative of cosine is the negative of sine:

;; cosine-series should be (1, 0, -1/2, 0, 1/24, ...)
(define cosine-series
  (cons-stream 1
               (stream-map -
                           (integrate-series sine-series))))
;; sine-series should be (0, 1, 0, -1/6, 1/120, ...)
(define sine-series
  (cons-stream 0
               (integrate-series cosine-series)))

A simple check of the results.

(for-each (lambda (n)
            (display n)
            (display ": ")
            (display (stream-ref sine-series n))
            (newline))
          (list 0 1 2 3 4 5))
(newline)
(for-each (lambda (n)
            (display n)
            (display ": ")
            (display (stream-ref cosine-series n))
            (newline))
          (list 0 1 2 3 4 5))

The output is:

0: 0
1: 1
2: 0
3: -1/6
4: 0
5: 1/120

0: 1
1: 0
2: -1/2
3: 0
4: 1/24
5: 0

Site Toolbox:

Personal tools
GNU Free Documentation License 1.2
This page was last modified on 20 October 2007, at 06:02.
Disclaimers - About BluWiki