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 ;; Exercise 1.36. Modify fixed-point so that it prints the sequence of approximations it generates, using the newline and display primitives shown in exercise 1.22. 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.)
27 (define (fixed-point f first-guess)
28 (define (close-enough? v1 v2)
29 (< (abs (- v1 v2)) tolerance))
30 (define (try guess)
31 ;; (display guess)
32 ;; (newline)
33 (let ((next (f guess)))
34 (if (close-enough? guess next)
35 ;; (begin (display next)
36 ;; next)
37 next
38 (try next))))
39 ;; (newline)
40 (try first-guess))
42 ;;(fixed-point (lambda (y) (+ (sin y) (cos y)))
43 ;; 1.0)
45 (define (sqrt x)
46 (fixed-point (lambda (y) (average y (/ x y)))
47 1.0))
49 (define (average x y)
50 (/ (+ x y) 2))
52 (define (test-case actual expected)
53 (load-option 'format)
54 (newline)
55 (format #t "Actual: ~A Expected: ~A" actual expected))
57 ;; (test-case golden-ratio (/ (+ 1.0 (sqrt 5.0)) 2.0))
59 ;; 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.)
61 ;; (define golden-ratio (fixed-point (lambda (x) (+ 1 (/ 1 x)))
62 ;; 1.0))
64 ;; (newline)
65 ;; (newline)
66 ;; (display "Finding solution to x^x = 1000 without average damping:")
67 ;; (fixed-point (lambda (x) (/ (log 1000) (log x)))
68 ;; 2.0)
69 ;; 35 iterations
71 ;; (newline)
72 ;; (display "Finding solution to x^x = 1000 with average damping:")
73 ;; (fixed-point (lambda (x) (average x (/ (log 1000) (log x))))
74 ;; 2.0)
75 ;; 10 iterations
77 ;; Average damping helps it converge much faster!
79 ;; 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
81 (define (cont-frac n d k)
82 (define (cont-frac-rec i)
83 (if (> i k)
84 0
85 (/ (n i) (+ (d i) (cont-frac-rec (1+ i))))))
86 (cont-frac-rec 1))
88 (test-case (cont-frac (lambda (i) 1.0)
89 (lambda (i) 1.0)
90 10)
91 (/ 1.0 (/ (+ 1.0 (sqrt 5)) 2.0)))
93 (test-case (cont-frac (lambda (i) 1.0)
94 (lambda (i) 1.0)
95 100)
96 (/ 1.0 (/ (+ 1.0 (sqrt 5)) 2.0)))
98 (test-case (cont-frac (lambda (i) 1.0)
99 (lambda (i) 1.0)
100 1000)
101 (/ 1.0 (/ (+ 1.0 (sqrt 5)) 2.0)))
103 ;; for successive values of k. How large must you make k in order to get an approximation that is accurate to 4 decimal places?
105 ;; k has to be somewhere between 10-100
107 ;; 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.