-
Notifications
You must be signed in to change notification settings - Fork 4
/
half-interval.scm
27 lines (22 loc) · 917 Bytes
/
half-interval.scm
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
(define (average a b) (/ (+ a b) 2))
(define (close-enough? a b) (< (abs (- a b)) 0.001))
(define (search f neg-point pos-point)
(let ((mid-point (average neg-point pos-point)))
(if (close-enough? neg-point pos-point) mid-point
(let ((test-value (f mid-point)))
(cond
((positive? test-value) (search f neg-point mid-point))
((negative? test-value) (search f mid-point pos-point))
(else mid-point))))))
(define (half-interval f a b)
(let ((a-value (f a))
(b-value (f b)))
(cond ((and (positive? a-value) (negative? b-value))
(search f b a))
((and (positive? b-value) (negative? a-value))
(search f a b))
(else (error "Values are not of opposite sign" a b)))))
(display (half-interval sin 2.0 4.0))
(newline)
; x^3 - 2x -3 = 0
(display (half-interval (lambda (x) (- (* x x x) (* 2 x) 3)) 1.0 2.0))