-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise 1.45 multi average damp.rkt
executable file
·250 lines (222 loc) · 5.64 KB
/
Exercise 1.45 multi average damp.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#lang racket
; Exercise 1.45. We saw in section 1.3.3 that attempting to compute square roots by naively
; finding a fixed point of y -> x/y does not converge, and that this can be fixed by average
; damping. The same method works for finding cube roots as fixed points of the average-damped
; y -> x/y^2. Unfortunately, the process does not work for fourth roots -- a single average
; damp is not enough to make a fixed-point search for y -> x/y^3 converge. On the other hand,
; if we average damp twice (i.e., use the average damp of the average damp of y -> x/y^3) the
; fixed-point search does converge. Do some experiments to determine how many average damps
; are required to compute nth roots as a fixed-point search based upon repeated average
; damping of y -> x/{y^(n-1)}. Use this to implement a simple procedure for computing nth
; roots using fixed-point, average-damp, and the repeated procedure of exercise 1.43. Assume
; that any arithmetic operations you need are available as primitives.
; SOLUTION
(define (sqrt x)
(fixed-point (average-damp (lambda (y) (/ x y))) 1.0)
)
(define (cube-root x)
(fixed-point (average-damp (lambda (y) (/ x (square y)))) 1.0)
)
(define (fourth-root-one-damp x)
(fixed-point (average-damp (lambda (y) (/ x (cube y)))) 1.0)
)
(define (fourth-root-two-damp x)
(fixed-point (average-damp (average-damp (lambda (y) (/ x (cube y))))) 1.0)
)
(define (nth-root x n m)
; x is the number for which the nth root is to be computed
; m is the number of times average damping should be done
(fixed-point ((repeated average-damp m) (lambda (y) (/ x (expt y (- n 1))))) 1.0)
)
(define (fixed-point f first-guess)
(define iters 0)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance)
)
(define (try guess)
(set! iters (inc iters))
; (display guess)
; (newline)
(let ((next (f guess)))
; (if (close-enough? guess next)
; next
; (try next)
; )
(cond
((close-enough? guess next) (display "Number of iterations: ") (display iters) (newline) next)
(else
(try next)
)
)
)
)
(try first-guess)
)
(define tolerance 0.00000001)
(define (inc x)
(+ x 1)
)
(define (average-damp f)
(lambda (x) (average x (f x)))
)
(define (average x y)
(/ (+ x y) 2)
)
(define (repeated f n)
(cond
((> n 1) (compose f (repeated f (- n 1))))
((= n 1) f)
(else
identity
)
)
)
(define (compose f g)
(lambda (w) (f (g w)))
)
(define (cube x)
(* x x x)
)
(define (square x)
(* x x)
)
(define (identity x)
x
)
; Tests
; Observations from the test below:
; 1. Fourth and Fifth roots need two average dampings. Three dampings also works but
; interestingly, it takes more iterations to converge to the same tolerance
; 2. I tried the 8th root of a number and found that even 3 average dampings were not enough
; but 4 dampings worked. 5 worked too but again, took more iterations than 4 dampings
> (cube-root 1)
Number of iterations: 1
1.0
> (cube-root 2)
Number of iterations: 27
1.2599210520301471
> (cube-root 3)
Number of iterations: 27
1.4422495724833264
> (cube-root 4)
Number of iterations: 25
1.5874010540873353
> (cube-root 5)
Number of iterations: 27
1.7099759448228977
> (cube-root 6)
Number of iterations: 28
1.81712059492772
> (cube-root 7)
Number of iterations: 28
1.9129311857760343
> (cube-root 8)
Number of iterations: 29
1.9999999982250738
> (sqrt 1)
Number of iterations: 1
1.0
> (sqrt 4)
Number of iterations: 6
2.0
> (sqrt 9)
Number of iterations: 6
3.0
> (sqrt 10)
Number of iterations: 6
3.162277660168379
> (sqrt 25)
Number of iterations: 7
5.0
> (sqrt 15)
Number of iterations: 7
3.872983346207417
> (sqrt 50)
Number of iterations: 8
7.0710678118654755
> (cube-root 50)
Number of iterations: 30
3.684031500961882
> (fourth-root-two-damp 17)
Number of iterations: 9
2.0305431848689306
> (fourth-root-two-damp 16)
Number of iterations: 9
2.0
> (fourth-root-two-damp 81)
Number of iterations: 13
3.0
> (fourth-root-two-damp 90)
Number of iterations: 13
3.080070288241023
> (fourth-root-two-damp 50)
Number of iterations: 12
2.6591479484724942
> (fourth-root-two-damp 256)
Number of iterations: 16
4.0
> (fourth-root-two-damp 250)
Number of iterations: 16
3.976353643835253
> (nth-root 9 2 1)
Number of iterations: 6
3.0
> (sqrt 9)
Number of iterations: 6
3.0
> (nth-root 100 2 1)
Number of iterations: 8
10.0
> (sqrt 100)
Number of iterations: 8
10.0
> (nth-root 100 3 1)
Number of iterations: 31
4.641588830678613
> (cube-root 100)
Number of iterations: 31
4.641588830678613
> (nth-root 100 4 2)
Number of iterations: 13
3.1622776601683795
> (nth-root 100 4 3)
Number of iterations: 39
3.1622776656035585
> (nth-root 99999999 8 4)
Number of iterations: 235
9.9999999961324
> (nth-root 99999999 8 5)
Number of iterations: 461
10.000000013823481
> (nth-root 7648593 7 4)
Number of iterations: 201
9.624292058144878
> (nth-root 7648593 6 4)
Number of iterations: 203
14.036653922334388
> (nth-root 7648593 5 4)
Number of iterations: 206
23.807638017946097
> (nth-root 7648593 8 4)
Number of iterations: 200
7.251831331163606
> (nth-root 7648593 9 4)
Number of iterations: 199
5.818920115008623
> (nth-root 7648593 10 4)
Number of iterations: 198
4.879307127651792
> (nth-root 7648593 11 4)
Number of iterations: 198
4.224546951830381
> (nth-root 7648593 12 4)
Number of iterations: 197
3.7465522715410797
> (nth-root 7648593 13 4)
Number of iterations: 197
3.384591607884878
> (nth-root 7648593 13 3)
Number of iterations: 131
3.3845916101906384
> (nth-root 7648593 13 2)
(DOES NOT CONVERGE!!)