diff options
Diffstat (limited to 'src/approx')
-rw-r--r-- | src/approx/derivative.lisp | 10 | ||||
-rw-r--r-- | src/approx/maceps.lisp | 6 | ||||
-rw-r--r-- | src/approx/package.lisp | 3 |
3 files changed, 14 insertions, 5 deletions
diff --git a/src/approx/derivative.lisp b/src/approx/derivative.lisp index 02fcb4c..8aa171a 100644 --- a/src/approx/derivative.lisp +++ b/src/approx/derivative.lisp @@ -1,9 +1,17 @@ (in-package :lizfcm.approx) -(defun derivative-at (f x &optional (delta 0.01)) +(defun central-derivative-at (f x &optional (delta 0.01)) (let* ((x2 (+ x delta)) (x1 (- x delta)) (y2 (apply f (list x2))) (y1 (apply f (list x1)))) (/ (- y2 y1) (- x2 x1)))) + +(defun fwd-derivative-at (f x &optional (delta 0.01)) + (let* ((x2 (+ x delta)) + (x1 x) + (y2 (apply f (list x2))) + (y1 (apply f (list x1)))) + (/ (- y2 y1) + (- x2 x1)))) diff --git a/src/approx/maceps.lisp b/src/approx/maceps.lisp index debaa67..e2738e4 100644 --- a/src/approx/maceps.lisp +++ b/src/approx/maceps.lisp @@ -3,10 +3,10 @@ (defun compute-maceps (f a init) (let ((h init) (err init)) - (loop while (> err 0) + (loop collect (list a h err) do (setf h (/ h 2) err (abs (- (funcall f (+ a h)) (funcall f a)))) - when (> err 0) - collect (list a h err)))) + while (> err 0)))) + diff --git a/src/approx/package.lisp b/src/approx/package.lisp index c11eb60..67c6a90 100644 --- a/src/approx/package.lisp +++ b/src/approx/package.lisp @@ -1,5 +1,6 @@ (in-package :cl-user) (defpackage lizfcm.approx (:use :cl) - (:export :derivative-at + (:export :central-derivative-at + :fwd-derivative-at :compute-maceps)) |