Exercise 1.8
The Question
Newton's method for cube roots is based on the fact that if \(y\) is an approximation to the cube root of \(x\), then a better approximation is given by the value \[\frac{x/y^2 + 2y}{3}\] Use this formula to implement a cube-root procedure analogous to the square-root procedure.
The Answer
This exercise essentially requires us to rewrite the improve
implementation from
the previous exercise for cube roots:
(define (improve guess x)
(/ (+ (/ x (* guess guess)) (* 2 guess)) 3))
Copying the rest with few modifications, we get:
(define (cbrt x)
(cbrt-iter 1.0 (improve 1.0 x) x))
(define (cbrt-iter guess new-guess x)
(if (good-enough? guess new-guess)
guess
(cbrt-iter new-guess (improve new-guess x) x)))
(define (good-enough? guess new-guess)
(< (/ (abs (- new-guess guess)) guess) 0.0001))
Trying cbrt
out:
(cbrt 8)
2.000004911675504
(cbrt 343)
7.000001795382107
(cbrt 0.33)
0.6910423430026196
(cbrt -8)
-2.0