Posted on 2007-05-08 17:08
dennis 阅读(1063)
评论(0) 编辑 收藏 所属分类:
计算机科学与基础
综合了习题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))
测试一下:
> (simple_curt 27)
3.0000000000000975834575646
> (simple_curt 8)
2.0000000000120622386311755
> (simple_curt 9)
2.0800838232385225245408740