I tried a very naive approach to aproximate the first derivative of a function in Lisp, and came up with something like this:
(defparameter *delta-x* 0.00001)
(defun diff (f x)
(/ (- (funcall f (+ x *delta-x*)) (funcall f x))
*delta-x*))
So that, for example
(diff #'(lambda (x) (* x x x)) 2)
; 12.016296
which is not bad (12 being the actual result). So I thought... why not? and tried to extend this to the second derivative approximation, using
(defun diff2 (f x)
(diff #'(lambda (z) (diff f z)) x))
Realizing it may be a numerically inferior algorithm, I still expected some kind of results but I get 0.0 as the answer for everything I try, so my guess is the Lisp I wrote is not what I think it is...
(diff2 #'(lambda (x) (* x x x)) 2)
; 0.0
Any hints greatly appreciated!
Thanks.