This shows you the differences between two versions of the page.
| — |
exercise_1.35 [2009/03/02 01:46] (current) 204.14.159.185 created |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | // | ||
| + | Show that the golden ratio ϕ (section 1.2.2) is a fixed point of the transformation x ↦ 1 + 1/x, and use this fact to compute ϕ by means of the fixed-point procedure. | ||
| + | // | ||
| + | <code scheme> | ||
| + | (define ϕ (/ (+ 1 (sqrt 5)) 2)) | ||
| + | (+ 1 (/ 1 ϕ)) ; => ϕ = 1.618033988749895 | ||
| + | |||
| + | (define tolerance 0.00001) | ||
| + | (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)) | ||
| + | |||
| + | (fixed-point (lambda (x) (+ 1 (/ 1 x))) 1) ; => 987 / 610 = 1.6180327869 | ||
| + | </code> | ||