综合了习题1.6提出的误差过大问题,采用相对误差进行求值,题目是要求使用牛顿近似求立方根公式写出scheme过程:
(define (square x) (
*
x x))
(define (divided_by_3 x y)( / ( + x y) 3 ))
(define (improve guess x)
(divided_by_3 ( / x (square guess)) ( * 2 guess)))
(define constant 0.0001 )
(define (good_enough ? old_guess guess)
( < ( abs ( / ( - guess old_guess) guess)) constant))
(define (curt old_guess guess x)
( if (good_enough ? old_guess guess)
guess
(curt guess (improve guess x) x)))
(define (simple_curt x)(curt 0.1 1 x))
测试一下:
(define (divided_by_3 x y)( / ( + x y) 3 ))
(define (improve guess x)
(divided_by_3 ( / x (square guess)) ( * 2 guess)))
(define constant 0.0001 )
(define (good_enough ? old_guess guess)
( < ( abs ( / ( - guess old_guess) guess)) constant))
(define (curt old_guess guess x)
( if (good_enough ? old_guess guess)
guess
(curt guess (improve guess x) x)))
(define (simple_curt x)(curt 0.1 1 x))
>
(simple_curt
27
)
3.0000000000000975834575646
> (simple_curt 8 )
2.0000000000120622386311755
> (simple_curt 9 )
2.0800838232385225245408740
3.0000000000000975834575646
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
> (simple_curt 8 )
2.0000000000120622386311755
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
> (simple_curt 9 )
2.0800838232385225245408740
![dot.gif](https://i-blog.csdnimg.cn/blog_migrate/9b8a8a44dd1c74ae49c20a7cd451974e.gif)
![115994.html](https://i-blog.csdnimg.cn/blog_migrate/7ea33453bd0b0e73f2ade0fbb1282d6a.jpeg)