1.40

Exercise 1.40: 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 x3+ax2+bx+c x^3 + a x^2 + bx +c .

fixed-point

(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))

deriv

(define dx 0.00001)

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

newton-transform

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

newtons-method

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

Now let's define cubic

f(x)=x3+ax2+bx+cf(x)=x^3 + a x^2 + bx +c

(define (cubic a b c) (lambda (x) (+ (* x x x) (* a (* x x)) (* b x) c)))
((cubic 1 1 1) 1)
((cubic 1 1 1) -1)

Finally let's verify the result

(newtons-method (cubic 1 1 1) 1)

results matching ""

    No results matching ""