通过半间隔法求方程的根

问题描述 投票:0回答:1

我正在追踪sicp Finding root of equations

#+begin_src emacs-lisp :session sicp :lexical t
(defun close-enoughp(x y)
  (< (abs (- x y)) 0.001))

(defun search(f neg-point pos-point)
  (let ((midpoint (average neg-point pos-point)))
    (if (close-enoughp neg-point pos-point)
        midpoint
      (let ((test-value (funcall f midpoint)))
        (cond ((posp test-value)
               (search f neg-point midpoint))
              ((negp test-value)
               (search f midpoint pos-point))
              (t midpoint))))))

(defun half-interval-method(f a b)
  (let ((a-value (funcall f a))
        (b-value (funcall f b)))
    (cond ((and (negp a-value) (posp b-value))
           (search f a b))
          ((and (negp b-value) (posp a-value))
           (search f b a))
          (t
           (error "Values are not of opposite sign" a b)))))

(defun negp(x)
  (< x 0))
(defun posp(x)
  (> x 0))

(defun average(a b)
              (/ (+ a b) 2))
#+end_src

测试

#+begin_src emacs-lisp :session sicp :lexical t
(half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3))
                      0
                      20.0)
#+end_src
#+RESULTS:
: 1.89300537109375

但是当尝试找到3的平方根时>

#+begin_src emacs-lisp :session sicp :lexical t
(half-interval-method  (lambda (x) (- (* x x) 3)
                       1
                       3.0)
)
#+end_src

报告错误:

progn: Wrong number of arguments: ((t) (f a b) (let ((a-value (funcall f a)) (b-value (funcall f b))) (cond ((and (negp a-value) (posp b-value)) (search f a b)) ((and (negp b-value) (posp a-value)) (search f b a)) (t (error "Values are not of opposite sign" a b))))), 1

该功能如此脆弱的原因是什么?

我现在遵循sicp查找方程式的根#+ begin_src emacs-lisp:session sicp:lexical t(defun close-enoughp(xy)(

您不正确地调用该函数。替换

(half-interval-method  (lambda (x) (- (* x x) 3)
                       1
                       3.0)
)

(half-interval-method  (lambda (x) (- (* x x) 3))
                       1
                       3.0)
elisp sicp
1个回答
0
投票

您不正确地调用该函数。替换

© www.soinside.com 2019 - 2024. All rights reserved.