1.39

Exercise 1.39: A continued fraction representation of the tangent function was published in 1770 by the German mathematician J.H. Lambert:

tanx=x1x23x25 tan x = \frac{x}{1 - \frac {x^2}{3 - \frac{x^2}{5-\cdots}}}

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:

f=N1D1+N2D2+N3D3+ f = \frac{N_1}{D_1 + \frac{N_2}{D_2 + \frac{N_3}{D_3 + \cdots}}}

Here, f=tan,f(x)=tan(x)f = tan, f(x) = tan(x). And D1=1,D2=3,D3=5,D_1 = 1, D_2 = 3, D_3 = 5, \cdots,

while N1(x)=x1,N2(x)=x2,N3(x)=x2,N_1(x) = x^1, N_2(x)=-x^2, N_3(x)=-x^2, \cdots.

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)

results matching ""

    No results matching ""