開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: MIT/GNU Scheme
計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション)の1(手続きによる抽象の構築)、1.3(高階手続きによる抽象)、1.3.3(一般的方法としての手続き)の問1.35、問1.36を解いてみる。
その他参考書籍
問題 1.35.
黄金比が問題の変換の不動点であるか確認。
1 + 2/(1 + √5) = (3 + √5)/(1 + √5) = (-2 - 2√5)/(-4) = (1 + √5)/2
ということで、黄金比Φは問題の変換の不動点。
黄金比Φの近似値を求める。
コード
sample.scm
(define (fixed-point f first-guess) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next (f guess))) (if (close-enough? guess next) next (try next)))) (try first-guess)) (define tolerance 0.00001) (define (f x) (+ 1 (/ 1 x)))
入出力結果(Terminal, REPL(Read, Eval, Print, Loop))
1 ]=> (* 1.0 (fixed-point f 1)) ;Value: 1.618032786885246
問題 1.36.
コード
sample.scm
(define (fixed-point f first-guess) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess n) (newline) (display n) (display " *** ") (display guess) (let ((next (f guess))) (if (close-enough? guess next) next (try next (+ n 1))))) (try first-guess 1)) (define tolerance 0.00001) (define (f n) (fixed-point (lambda (x) (/ (log n) (log x))) 2.0)) (define (g n) (fixed-point (lambda (x) (average x (/ (log n) (log x)))) 2.0)) (define (average a b) (/ (+ a b) 2))
入出力結果(Terminal, REPL(Read, Eval, Print, Loop))
1 ]=> (f 1000) 1 *** 2. 2 *** 9.965784284662087 3 *** 3.004472209841214 4 *** 6.279195757507157 5 *** 3.759850702401539 6 *** 5.215843784925895 7 *** 4.182207192401397 8 *** 4.8277650983445906 9 *** 4.387593384662677 10 *** 4.671250085763899 11 *** 4.481403616895052 12 *** 4.6053657460929 13 *** 4.5230849678718865 14 *** 4.577114682047341 15 *** 4.541382480151454 16 *** 4.564903245230833 17 *** 4.549372679303342 18 *** 4.559606491913287 19 *** 4.552853875788271 20 *** 4.557305529748263 21 *** 4.554369064436181 22 *** 4.556305311532999 23 *** 4.555028263573554 24 *** 4.555870396702851 25 *** 4.555315001192079 26 *** 4.5556812635433275 27 *** 4.555439715736846 28 *** 4.555599009998291 29 *** 4.555493957531389 30 *** 4.555563237292884 31 *** 4.555517548417651 32 *** 4.555547679306398 33 *** 4.555527808516254 34 *** 4.555540912917957 ;Value: 4.555532270803653 1 ]=> (g 1000) 1 *** 2. 2 *** 5.9828921423310435 3 *** 4.922168721308343 4 *** 4.628224318195455 5 *** 4.568346513136242 6 *** 4.5577305909237005 7 *** 4.555909809045131 8 *** 4.555599411610624 9 *** 4.5555465521473675 ;Value: 4.555537551999825 1 ]=> (f 10) 1 *** 2. 2 *** 3.3219280948873626 3 *** 1.9179492575050479 4 *** 3.535603900583148 5 *** 1.8232750396958857 6 *** 3.8335887194852516 7 *** 1.7134861964081292 8 *** 4.275685783797153 9 *** 1.5847715327324579 10 *** 5.000833604849827 11 *** 1.4305283826150665 12 *** 6.431013788836577 13 *** 1.2371958874249085 14 *** 10.818007102229766 15 *** .9669802711122343 16 *** -68.57588066275477 ;The object -68.92676055452102+.260723061602394i, passed as an argument to abs, is not the correct type. ;To continue, call RESTART with an option number: ; (RESTART 1) => Return to read-eval-print level 1. 2 error> (restart 1) ;Abort! 1 ]=> (g 10) 1 *** 2. 2 *** 2.6609640474436813 3 *** 2.5068446445819106 4 *** 2.506155094695938 5 *** 2.506185430248098 ;Value: 2.5061840887933005 1 ]=> (f 2) 1 *** 2. 2 *** 1. 3 *** #[+inf] 4 *** 0. ;Value: 0. 1 ]=> (g 2) 1 *** 2. 2 *** 1.5 3 *** 1.6047556456757275 4 *** 1.5351356968072742 5 *** 1.576150223446228 6 *** 1.5497999090993433 7 *** 1.5659366047729995 8 *** 1.5557350049007783 9 *** 1.5620627182198243 10 *** 1.558089639457978 11 *** 1.5605656210774552 12 *** 1.5590152862213529 13 *** 1.5599831750444104 14 *** 1.5593777959694683 15 *** 1.5597560027533146 16 *** 1.5595195500827392 17 *** 1.5596673123980125 18 *** 1.5595749478346872 19 *** 1.5596326737339847 20 *** 1.5595965923011024 21 *** 1.5596191433636224 22 *** 1.5596050482399235 ;Value: 1.5596138578993317 1 ]=>
平均緩和を使ったら、平均緩和を使わなかったときと比べて、x^x = 1000 の解の近似値を求めるためのステップ数はだいぶ少なくなった。おまけに、x^x = 10など、平均緩和を使わないと求めることができない解の近似値も見つけることが出来るようになった。(初めて見た#[+inf]ってどういう意味だろう。。)
0 コメント:
コメントを投稿