#|
find-root: (N -> N) N -> N u #f
(define (find-root f i) ...)
Purpose:
j in [0,i] such that (f i) = 0
otherwise #f
|#
(define (find-root f i)
(cond
((zero? i) (cond
((= (f i) 0) i)
(else #f)))
(else (cond
((= (f i) 0) i)
(else (find-root f (sub1 i)))))))
Here is an improvement, based on the laws of cond:
(define (find-root f i)
(cond
((= (f i) 0) i)
((zero? i) #f)
(else (find-root f (sub1 i)))))
Does this tell us that find-root recurs on N? (NOPE)
#|
find-root: (R -> R) R -> R
(define (find-root f a) ...)
Purpose:
x in [0,a] such that (abs (f a)) <= 0
|#
How do we know that f has a root? We don't. So let's assume a bit more:
f(0) < 0 and f(a) > 0.
Here is the graph:
The Eureka: Mean Value Theorem. If f(b) is below 0 and f(a) is above 0, then f(x) = 0 for some x between b and a. -- So we split the interval into two parts and focus on the one that again satisfies the condition. When the interval is small enough, we stop.
#|
find-root : (number -> number) number number -> number
the result $r$ is the left boundary of an interval [r,r+TOLERANCE]
in which f has a root (\ie, (f x) = 0 for some x in the interval
ASSUMPTION: f is continuous and monotone
|#
(define (find-root f left right)
(cond
((<= (- right left) TOLERANCE) left)
(else
(local ((define mid (/ (+ left right) 2)))
(cond
((<= (f left) 0 (f mid))
(find-root left mid))
(else
(find-root mid right)))))))
We will see another application of this idea somewhere down the road.
| General idea: consider the continuous counterpart, use knowledge from analysis, and go back to discrete world. |
| Matthias Felleisen | This page was generated on Fri Apr 9 09:17:38 CDT 1999. |