3

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.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
PaulM
  • 305
  • 1
  • 8

2 Answers2

3

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.

PaulM
  • 305
  • 1
  • 8
2

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.

Svante
  • 50,694
  • 11
  • 78
  • 122