diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-05-08 08:48:12 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-05-08 08:48:12 +0200 |
commit | 1925c123d7d8184a1d2ddc0a413e0fd2776e1083 (patch) | |
tree | fad79f909d9c3be53d68e6ebd67202650536d387 /packages/hmatrix/examples/integrate.hs | |
parent | eb3f702d065a4a967bb754977233e6eec408fd1f (diff) |
empty hmatrix-base
Diffstat (limited to 'packages/hmatrix/examples/integrate.hs')
-rw-r--r-- | packages/hmatrix/examples/integrate.hs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/packages/hmatrix/examples/integrate.hs b/packages/hmatrix/examples/integrate.hs new file mode 100644 index 0000000..3a03da6 --- /dev/null +++ b/packages/hmatrix/examples/integrate.hs | |||
@@ -0,0 +1,24 @@ | |||
1 | -- Numerical integration | ||
2 | import Numeric.GSL | ||
3 | |||
4 | quad f a b = fst $ integrateQAGS 1E-9 100 f a b | ||
5 | |||
6 | -- A multiple integral can be easily defined using partial application | ||
7 | quad2 f y1 y2 g1 g2 = quad h y1 y2 | ||
8 | where | ||
9 | h y = quad (flip f y) (g1 y) (g2 y) | ||
10 | |||
11 | volSphere r = 8 * quad2 (\x y -> sqrt (r*r-x*x-y*y)) | ||
12 | 0 r (const 0) (\x->sqrt (r*r-x*x)) | ||
13 | |||
14 | -- wikipedia example | ||
15 | exw = quad2 f 7 10 (const 11) (const 14) | ||
16 | where | ||
17 | f x y = x**2 + 4*y | ||
18 | |||
19 | main = do | ||
20 | print $ quad (\x -> 4/(x^2+1)) 0 1 | ||
21 | print pi | ||
22 | print $ volSphere 2.5 | ||
23 | print $ 4/3*pi*2.5**3 | ||
24 | print $ exw | ||