SICP Exercise 1.39
Question
A continued fraction representation of the tangent function was published in 1770 by the German mathematician J.H. Lambert:
\[\tan x=\frac{x}{1-\frac{x^2}{3-\frac{x^2}{5-\ddots}}}\]
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.
Answer
Our expected result for \(\tan(1)\) with 4 decimal places accuracy is \(1.5574\). Amazingly, we only need 4 iterations to get close enough (but as always, more is better).
(define (square-unless-one x)
(if (= x 1) x
(* x x)))
(define (get-n x) (- (* 2 x) 1))
(define (tan-cf x k)
(define (iterate counter)
(if (= k counter) (/ (square-unless-one x) (get-n counter))
(/ (square-unless-one x) (- (get-n counter) (iterate (+ counter 1))))))
(iterate 1))
(tan-cf 1.0 4)
Results:
1.5573770491803278