Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname |27.3|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 (define TOLERANCE 0.000000001)
6 ;; poly : number -> number
7 (define (poly x)
8 (* (- x 2) (- x 4)))
10 ;midpoint : number number -> number
11 ;Returns the average of the two numbers.
12 (define (midpoint n1 n2)
13 (/ (+ n1 n2) 2))
15 ;find-root : (number -> number) number number -> number
16 ;Given f, left and right, approximate a root by using a binary search. find-root returns the left interval once the interval size is less than tolerance. find-root uses generative recursion to find the root--it splits the interval in half and finds the root for one half of the interval.
17 ;Assume that (<= (f left) 0 (f right)) or (<= (f right) 0 (f left)).
18 ;Termination argument: Each interval is half the size of the previous interval. Hence, the nth recursive call has an interval of initial * (1/2)^n, where initial is the size of the initial interval. As n-> infinity, the interval approaches zero, which implies that this function must terminate for any value of tolerance greater than zero.
20 (define (find-root f left right)
21 (local ((define mid (/ (+ left right) 2))
22 (define f-mid (f mid)))
23 (cond
24 [(<= (- right left) TOLERANCE) left]
25 [(or (<= (f left) 0 f-mid)
26 (>= (f left) 0 f-mid)) (find-root f left mid)]
27 [else (find-root f mid right)])))
29 #|
31 (define (find-root f left right)
32 (local ((define mid (/ (+ left right) 2)))
33 (cond
34 [(<= (- right left) TOLERANCE) left]
35 [(or (<= (f left) 0 (f mid))
36 (>= (f left) 0 (f mid))) (find-root f left mid)]
37 [(or (<= (f right) 0 (f mid))
38 (>= (f right) 0 (f mid))) (find-root f mid right)]
39 [else (error 'find-root "not guaranteed root")])))
41 |#
43 ;find-root-aux : (number -> number) number number number number -> number
44 ;Same as find-root except it takes in two more arguments, f-left and f-right, in order to avoid calculating (f mid) twice.
46 (define (find-root-aux f left right f-left f-right)
47 (local ((define mid (/ (+ left right) 2))
48 (define f-mid (f mid)))
49 (cond
50 [(<= (- right left) TOLERANCE) left]
51 [(or (<= f-left 0 f-mid)
52 (>= f-left 0 f-mid)) (find-root-aux f left mid f-left f-mid)]
53 [else (find-root-aux f mid right f-mid f-right)])))
55 ;find-root-aux : (number -> number) number number -> number
56 ;Same as find-root except it avoid calculating (f mid) twice to speed up calculations.
58 (define (find-root2 f left right)
59 (find-root-aux f left right (f left) (f right)))
61 (define (poly2 x)
62 (+ (- (log (+ (* (expt x 3) (exp x))
63 (sin (* x
64 (exp (* x (log x))))))) (abs x))
65 (- (+ (expt 2 x)
66 (expt 2 (sin x)))
67 (/ (expt x x) (+ x 3)))))