計算機プログラムの構造と解釈 第二版 P40 問題1.39
問題をよむ。
とりあえず、
正接関数の近似値を計算する手続き(tan-cf x k)を定義するのが目的
正接関数って、なんだよ。
三角関数
http://ja.wikipedia.org/wiki/%E4%B8%89%E8%A7%92%E9%96%A2%E6%95%B0
という6つの値が定まる。それぞれ正弦(サイン/sine)・余弦(コサイン/cosine)・正接(タンジェント/tangent)・余割(コセカント/cosecant)・正割(セカント/secant)・余接(コタンジェント/cotangent)と呼ばれ、まとめて三角比と呼ばれる。
なるほどなるほど。
tan
のことね。
前の問題で言うところのDiは
1, 3, 5, ....
だから、
1 + ((i - 1) * 2)
でいけてると思う。
んで、前の問題で言うところの
Niは
i=1の時
x
で、それ以外の時は
-(x^2)
を返せばいいのだ。
そしたらもう後はソースで。
#!/usr/local/bin/gosh ;; -*- coding: utf-8 -*- ;;反復的(イテレーシブ)プロセスヴァージョン (define (cont-frac-i n d k) (define (cont-frac i result) (if (= i 0) result (cont-frac (- i 1) (/ (n i) (+ (d i) result))))) (cont-frac (- k 1) (/ (n k) (d k)))) ;;tan-cf (define (tan-cf x k) (cont-frac-i (lambda (i) (if (= i 1) x (- (* x x)))) (lambda (i) (+ (* 2.0 (- i 1)) 1.0)) k)) ;; main (define (main args) (display "(tan-cf 1 12) : ") (display (tan-cf 1 12)) (newline) (display "(tan 1) : ") (display (tan 1)) (newline) (display "(tan-cf 2 12) : ") (display (tan-cf 2 12)) (newline) (display "(tan 2) : ") (display (tan 2)) (newline) (display "(tan-cf 3 12) : ") (display (tan-cf 3 12)) (newline) (display "(tan 3) : ") (display (tan 3)) (newline) 0)
実行
(tan-cf 1 12) : 1.557407724654902 (tan 1) : 1.5574077246549023 (tan-cf 2 12) : -2.185039863261519 (tan 2) : -2.185039863261519 (tan-cf 3 12) : -0.1425465430745178 (tan 3) : -0.1425465430742778
できたでしょ、こりゃ!!