Blob


1 (defvar *tolerance* 0.0001)
2 (defun fixed-point (f first-guess)
3 (labels (
4 (close-enough? (v1 v2)
5 (< (abs (- v1 v2)) *tolerance*))
6 (try (guess)
7 (let ((next (funcall f guess)))
8 (if (close-enough? guess next)
9 next
10 (try next)))))
11 (try first-guess)))
12 (defvar *dx* 0.00001)
13 (defun deriv (g)
14 (lambda (x)
15 (/ (- (funcall g (+ x *dx*))
16 (funcall g x))
17 *dx*)))
18 (defun newton-transform (g)
19 (lambda (x)
20 (- x (/ (funcall g x)
21 (funcall (deriv g) x)))))
22 (defun newtons-method (g guess)
23 (fixed-point (newton-transform g) guess))
25 (defun cubic (a b c)
26 (lambda (x)
27 (+ (cube x)
28 (* a (square x))
29 (* b x)
30 c)))