-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-40.scm
41 lines (31 loc) · 878 Bytes
/
1-40.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
#lang sicp
(define tolerance 0.0001)
; from the book
(define (fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
(cond ((close-enough? guess next) next)
(else
(display next)
(newline)
(try next)))))
(try first-guess))
(define dx 0.0001)
(define (deriv g)
(lambda (x)
(/ (- (g (+ x dx)) (g x))
dx)))
(define (newtons-transform g)
(lambda (x)
(- x (/ (g x) ((deriv g) x)))))
(define (newtons-method g guess)
(fixed-point (newtons-transform g) guess))
; define cubic so it can be used with newtons method
(define (cubic a b c)
(lambda (x)
(+ (cube x) (* a (square x)) (* b x) c)))
(define (cube x) (* x x x))
(define (square x) (* x x))
(newtons-method (cubic 1 1 1) 1.0) ; should be -1