Approximate zeros

Define a procedure cubic that can be used together with the newtons-method procedure in expressions of the form

(newtons-method (cubic a b c) 1)

to approximate zeros of the cubic x³ + ax² + bx + c.


There are no comments yet.

Authentication required

You must log in to post a comment.

Login
(define tolerance 0.00001)

(define (fixed-point f first-guess)
  (define (close-enough? v1 v2)
    (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (let ((next (f guess)))
      (if (close-enough? guess next)
          next
          (try next))))
  (try first-guess))


(define (deriv g)
  (lambda (x)
    (/ (- (g (+ x tolerance)) (g x))
       tolerance)))

(define (newton-transform g)
  (lambda (x)
    (- x (/ (g x) ((deriv g) x)))))

(define (newtons-method g guess)
  (fixed-point (newton-transform g) guess))


(check-equal? (round (newtons-method (cubic 0 0 0) 1)) 0.0)
(check-equal? (round (newtons-method (cubic 0 0 (- 27)) 10)) 3.0)
(check-equal? (round (newtons-method (cubic 0 0 (- 81)) 10)) 4.0)
(check-equal? (round (newtons-method (cubic 2 8 (- 32)) 10)) 2.0)