1.39
Exercise 1.39: A continued fraction representation of the tangent function was published in 1770 by the German mathematician J.H. Lambert:
where x is in radians. Define a procedure (tan-cf x k) that computes an approximation to the tangent function based on Lambert’s formula. k specifies the number of terms to compute, as in Exercise 1.37.
Let's first review the general continued fraction:
Here, . And ,
while .
Firstly we can define d as:
(define (d i) (- (* 2 i) 1))
(d 3)
Then we can define n as:
(define (n i x)
(if (= i 1)
x
(- (* x x))))
(n 1 3)
(n 2 3)
(n 3 3)
(n 4 3)
Now, let's define tan-cf based on cont-frac:
(define (cont-frac n d k x)
(define (g i k)
(if (= i k) (/ (n k x) (d k))
(/ (n i x) (+ (d i) (g (+ i 1) k)))
)
)
(g 1 k)
)
(define (tan-cf x k)
(cont-frac n
d
k
x)
)
(tan-cf 3 30)
Compare the result with
(tan 3)