• incanter

1.2.3-SNAPSHOT

# integrate

## incanter.optimize

• (integrate f a b)
Integrate a function f from a to b

Examples:
(defn f1 [x] 1)
(defn f2 [x] (Math/pow x 2))
(defn f3 [x] (* x (Math/exp (Math/pow x 2))))

(integrate f1 0 5)
(integrate f2 0 1)
(integrate f3 0 1)

;; normal distribution
(def std 1)
(def mu 0)
(defn normal [x]
(/ 1
(* (* std (Math/sqrt (* 2 Math/PI)))
(Math/exp (/ (Math/pow (- (- x mu)) 2)
(* 2 (Math/pow std 2)))))))

(integrate normal 1.96 10)

Reference:
http://jng.imagine27.com/articles/2009-04-09-161839_integral_calculus_in_lambda_calculus_lisp.html
http://iam.elbenshira.com/archives/151_integral-calculus-in-haskell/

### 0 Examples top

Log in to add / edit an example.

### See Also top

Log in to add a see also.

### Source incanter/optimize.clj:25 top

```(defn integrate
" Integrate a function f from a to b

Examples:
(defn f1 [x] 1)
(defn f2 [x] (Math/pow x 2))
(defn f3 [x] (* x (Math/exp (Math/pow x 2))))

(integrate f1 0 5)
(integrate f2 0 1)
(integrate f3 0 1)

;; normal distribution
(def std 1)
(def mu 0)
(defn normal [x]
(/ 1
(* (* std (Math/sqrt (* 2 Math/PI)))
(Math/exp (/ (Math/pow (- (- x mu)) 2)
(* 2 (Math/pow std 2)))))))

(integrate normal 1.96 10)

Reference:
http://jng.imagine27.com/articles/2009-04-09-161839_integral_calculus_in_lambda_calculus_lisp.html
http://iam.elbenshira.com/archives/151_integral-calculus-in-haskell/

"
([f a b]
(let [small-dx 0.0001
integrate-gen (fn [f x b dx sum]
(if (> x b)
sum
(recur f (+ x dx) b dx (+ sum (* (f x) dx)))))]
(integrate-gen f a b small-dx 0))))```
Vars in incanter.optimize/integrate: defn let
Used in 0 other vars