Approximating derivatves and second derivatives of numerical functions in Common Lisp - not working as expected

99 Views Asked by At

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.

2

There are 2 best solutions below

0
PaulM On

LOL. good news bad news.... The good news is that the lisp I wrote is doing what I thought I told it to do. The bad news is I'm an idiot.

(defparameter *delta-x* 1/10000)

makes it work as expected. Floating point arithmetic 101.

0
Svante On

I think it's ok, but you're at the limit of granularity of single floats. You can see that with trace.

Try it with double floats or ratios.