-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex1-40-cubic.scm
52 lines (41 loc) · 1.36 KB
/
ex1-40-cubic.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
; Ex. 1.40 Cubic
(define (cubic a b c)
(lambda (x) (+ (* x x x) (* a x x) (* b x) c)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Newton's method re-cast as: If x |--> g(x) is a
; differentiable function, then the solution of the equation
; g(x) = 0 is a fixed point of the function x |--> f(x) where
; f(x) = x - g(x)/Dg(x) and Dg(x) is the derivative of g
; evaluated at x.
(define (deriv g)
(lambda (x)
(/ (- (g (+ x dx)) (g x))
dx)))
; Where dx is defined as
(define dx 0.00001)
; Newton's transform for f(x) = x - g(x)/Dg(x)
(define (newtons-transform g)
(lambda (x)
(- x (/ (g x) ((deriv g) x)))))
; Newton's method implemented using newtons-transform
(define (newtons-method g guess)
(fixed-point (newtons-transform g) guess))
; tolerance intended for fixed-point procedure
(define tolerance 0.00001)
; procedure to compute fixed-point f(x) = x
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
(display "Guess: ")
(display guess)
(newline)
(display "Next: ")
(display next)
(newline)
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
(define (average x y) (/ (+ x y) 2))