| 1 | ;; Numerical differentiation. |
|---|
| 2 | ;; Liam Healy Mon Nov 12 2007 - 22:07 |
|---|
| 3 | ;; Time-stamp: <2008-02-17 18:36:19EST numerical-differentiation.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | ;;; The example at the end needs to be made into a regression test. |
|---|
| 9 | ;;; GSL function "callback" passing is identical to |
|---|
| 10 | ;;; numerical-integration, so those definitions have been used. |
|---|
| 11 | ;;; Some improvement could be made in naming/organization. |
|---|
| 12 | |
|---|
| 13 | (defmfun central-derivative (function x step) |
|---|
| 14 | "gsl_deriv_central" |
|---|
| 15 | ((function :pointer) (x :double) (step :double) |
|---|
| 16 | (result :double) (abserr :double)) |
|---|
| 17 | :documentation ; FDL |
|---|
| 18 | "Compute the numerical derivative of the function |
|---|
| 19 | at the point x using an adaptive central difference algorithm with |
|---|
| 20 | a step-size of step. The derivative and an |
|---|
| 21 | estimate of its absolute error is returned. |
|---|
| 22 | |
|---|
| 23 | The initial value of step is used to estimate an optimal step-size, |
|---|
| 24 | based on the scaling of the truncation error and round-off error in the |
|---|
| 25 | derivative calculation. The derivative is computed using a 5-point rule |
|---|
| 26 | for equally spaced abscissae at x-step, x-step/2, x, |
|---|
| 27 | x+step/2, x, with an error estimate taken from the difference |
|---|
| 28 | between the 5-point rule and the corresponding 3-point rule x-step, |
|---|
| 29 | x, x+step. Note that the value of the function at x |
|---|
| 30 | does not contribute to the derivative calculation, so only 4-points are |
|---|
| 31 | actually used.") |
|---|
| 32 | |
|---|
| 33 | (defmfun forward-derivative (function x step) |
|---|
| 34 | "gsl_deriv_forward" |
|---|
| 35 | ((function :pointer) (x :double) (step :double) |
|---|
| 36 | (result :double) (abserr :double)) |
|---|
| 37 | :documentation ; FDL |
|---|
| 38 | "Compute the numerical derivative of the function |
|---|
| 39 | at the point x using an adaptive forward difference algorithm with |
|---|
| 40 | a step-size of step. The function is evaluated only at points greater |
|---|
| 41 | than x and never at x itself. The derivative is returned in |
|---|
| 42 | result and an estimate of its absolute error is returned as the |
|---|
| 43 | second value. This function should be used if f(x) has a |
|---|
| 44 | discontinuity at x, or is undefined for values less than x. |
|---|
| 45 | |
|---|
| 46 | The initial value of step is used to estimate an optimal step-size, |
|---|
| 47 | based on the scaling of the truncation error and round-off error in |
|---|
| 48 | the derivative calculation. The derivative at x is computed |
|---|
| 49 | using an ``open'' 4-point rule for equally spaced abscissae at |
|---|
| 50 | x+step/4, x+step/2, x+3step/4, x+step, |
|---|
| 51 | with an error estimate taken from the difference between the 4-point |
|---|
| 52 | rule and the corresponding 2-point rule x+step/2, |
|---|
| 53 | x+step.") |
|---|
| 54 | |
|---|
| 55 | (defmfun backward-derivative (function x step) |
|---|
| 56 | "gsl_deriv_backward" |
|---|
| 57 | ((function :pointer) (x :double) (step :double) |
|---|
| 58 | (result :double) (abserr :double)) |
|---|
| 59 | :documentation ; FDL |
|---|
| 60 | "Compute the numerical derivative of the function at the point x |
|---|
| 61 | using an adaptive backward difference algorithm with a step-size of |
|---|
| 62 | step. The function is evaluated only at points less than x, and never |
|---|
| 63 | at x itself. The derivative is returned in result and an estimate of |
|---|
| 64 | its absolute error is returned as the second value. This function |
|---|
| 65 | should be used if f(x) has a discontinuity at x, or is undefined for |
|---|
| 66 | values greater than x. This function is equivalent to calling |
|---|
| 67 | #'forward-derivative with a negative step-size.") |
|---|
| 68 | |
|---|
| 69 | ;;;; Examples and unit test |
|---|
| 70 | |
|---|
| 71 | ;;; This is the example given in the GSL manual, Sec. 27.2. |
|---|
| 72 | (defun-single 3/2-power (x) (expt x 3/2)) |
|---|
| 73 | ;;; (3/2-power 2.0d0) |
|---|
| 74 | |
|---|
| 75 | #| |
|---|
| 76 | (make-tests numerical-differentiation |
|---|
| 77 | ;; Compare to (* 3/2 (sqrt 2.0d0)) |
|---|
| 78 | (central-derivative 3/2-power 2.0d0 1.d-8)) |
|---|
| 79 | |# |
|---|
| 80 | |
|---|
| 81 | (LISP-UNIT:DEFINE-TEST NUMERICAL-DIFFERENTIATION |
|---|
| 82 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 83 | (LIST 2.121320312002221d0 4.0642813729715275d-7) |
|---|
| 84 | (MULTIPLE-VALUE-LIST |
|---|
| 85 | (CENTRAL-DERIVATIVE 3/2-POWER 2.0d0 1.d-8)))) |
|---|
| 86 | |
|---|