Blob


1 ;; (define (search f neg-point pos-point)
2 ;; (let ((midpoint (average neg-point pos-point)))
3 ;; (if (close-enough? neg-point pos-point)
4 ;; midpoint
5 ;; (let ((test-value (f midpoint)))
6 ;; (cond ((positive? test-value)
7 ;; (search f neg-point midpoint))
8 ;; ((negative? test-value)
9 ;; (search f midpoint pos-point))
10 ;; (else midpoint))))))
11 ;; (define (close-enough? x y)
12 ;; (< (abs (- x y)) 0.001))
14 ;; (define (half-interval-method f a b)
15 ;; (let ((a-value (f a))
16 ;; (b-value (f b)))
17 ;; (cond ((and (negative? a-value) (positive? b-value))
18 ;; (search f a b))
19 ;; ((and (negative? b-value) (positive? a-value))
20 ;; (search f b a))
21 ;; (else
22 ;; (error "Values are not of opposite sign" a b)))))
23 ;; (define tolerance 0.00001)
25 ;; (define (fixed-point f first-guess)
26 ;; (define (close-enough? v1 v2)
27 ;; (< (abs (- v1 v2)) tolerance))
28 ;; (define (try guess)
29 ;; ;; (display guess)
30 ;; ;; (newline)
31 ;; (let ((next (f guess)))
32 ;; (if (close-enough? guess next)
33 ;; ;; (begin (display next)
34 ;; ;; next)
35 ;; next
36 ;; (try next))))
37 ;; ;; (newline)
38 ;; (try first-guess))
40 ;;(fixed-point (lambda (y) (+ (sin y) (cos y)))
41 ;; 1.0)
43 ;; (define (sqrt x)
44 ;; (fixed-point (lambda (y) (average y (/ x y)))
45 ;; 1.0))
47 ;; (define (average x y)
48 ;; (/ (+ x y) 2))
50 (define (test-case actual expected)
51 (load-option 'format)
52 (newline)
53 (format #t "Actual: ~A Expected: ~A" actual expected))
55 ;; (test-case golden-ratio (/ (+ 1.0 (sqrt 5.0)) 2.0))
57 ;; Then find a solution to x^x = 1000 by finding a fixed point of x-->log(1000)/log(x). (Use Scheme's primitive log procedure, which computes natural logarithms.) Compare the number of steps this takes with and without average damping. (Note that you cannot start fixed-point with a guess of 1, as this would cause division by log(1) = 0.)
59 ;; (define golden-ratio (fixed-point (lambda (x) (+ 1 (/ 1 x)))
60 ;; 1.0))
62 ;; (newline)
63 ;; (newline)
64 ;; (display "Finding solution to x^x = 1000 without average damping:")
65 ;; (fixed-point (lambda (x) (/ (log 1000) (log x)))
66 ;; 2.0)
67 ;; 35 iterations
69 ;; (newline)
70 ;; (display "Finding solution to x^x = 1000 with average damping:")
71 ;; (fixed-point (lambda (x) (average x (/ (log 1000) (log x))))
72 ;; 2.0)
73 ;; 10 iterations
75 ;; Average damping helps it converge much faster!
77 ;; Suppose that n and d are procedures of one argument (the term index i) that return the Ni and Di of the terms of the continued fraction. Define a procedure cont-frac such that evaluating (cont-frac n d k) computes the value of the k-term finite continued fraction. Check your procedure by approximating 1/golden-ratio using
79 ;; (define (cont-frac n d k)
80 ;; (define (cont-frac-rec i)
81 ;; (if (> i k)
82 ;; 0
83 ;; (/ (n i) (+ (d i) (cont-frac-rec (1+ i))))))
84 ;; (cont-frac-rec 1))
86 ;; (test-case (cont-frac (lambda (i) 1.0)
87 ;; (lambda (i) 1.0)
88 ;; 10)
89 ;; (/ 1.0 (/ (+ 1.0 (sqrt 5)) 2.0)))
91 ;; (test-case (cont-frac (lambda (i) 1.0)
92 ;; (lambda (i) 1.0)
93 ;; 100)
94 ;; (/ 1.0 (/ (+ 1.0 (sqrt 5)) 2.0)))
96 ;; (test-case (cont-frac (lambda (i) 1.0)
97 ;; (lambda (i) 1.0)
98 ;; 1000)
99 ;; (/ 1.0 (/ (+ 1.0 (sqrt 5)) 2.0)))
101 ;; for successive values of k. How large must you make k in order to get an approximation that is accurate to 4 decimal places?
103 ;; k has to be somewhere between 10-100
105 ;; b. If your cont-frac procedure generates a recursive process, write one that generates an iterative process. If it generates an iterative process, write one that generates a recursive process.
107 (define (cont-frac-iter n d k)
108 (define (iter i result)
109 (if (= i 0)
110 result
111 (iter (- i 1) (/ (n i) (+ (d i) result)))))
112 (iter k 0.0))
114 ;; (test-case (cont-frac-iter (lambda (i) 1.0)
115 ;; (lambda (i) 1.0)
116 ;; 1000)
117 ;; (/ 1.0 (/ (+ 1.0 (sqrt 5)) 2.0)))
119 (test-case (+ 2.0
120 (cont-frac-iter (lambda (i) 1)
121 (lambda (i) (if (= (remainder i 3) 2)
122 (* (/ (+ i 1) 3) 2)
123 1))
124 100))
125 2.7182818284590452353602874)