(defn derivative
"
Returns a function that approximates the derivative of the given function.
Options:
:dx (default 0.0001)
Examples:
(use '(incanter core optimize charts stats))
(defn cube [x] (* x x x))
(def cube-deriv (derivative cube))
(cube-deriv 2) ; value: 12.000600010022566
(cube-deriv 3) ; value: 27.00090001006572
(cube-deriv 4) ; value: 48.00120000993502
(def x (range -3 3 0.1))
(def plot (xy-plot x (map cube x)))
(view plot)
(add-lines plot x (map cube-deriv x))
;; get the second derivative function
(def cube-deriv2 (derivative cube-deriv))
(add-lines plot x (map cube-deriv2 x))
;; plot the normal pdf and its derivatives
(def plot (xy-plot x (pdf-normal x)))
(view plot)
(def pdf-deriv (derivative pdf-normal))
(add-lines plot x (pdf-deriv x))
;; plot the second derivative function
(def pdf-deriv2 (derivative pdf-deriv))
(add-lines plot x (pdf-deriv2 x))
"
([f & options]
(let [opts (when options (apply assoc {} options))
dx (or (:dx opts) 0.0001)
f-prime (fn [x] (div (minus (f (plus x dx)) (f x)) dx))]
f-prime)))
Vars in
incanter.optimize/derivative:
defn
let
Used in 0 other vars
Comments top
No comments for derivative. Log in to add a comment.