summaryrefslogtreecommitdiff
path: root/examples/sundials.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/sundials.hs')
-rw-r--r--examples/sundials.hs48
1 files changed, 48 insertions, 0 deletions
diff --git a/examples/sundials.hs b/examples/sundials.hs
new file mode 100644
index 0000000..1643540
--- /dev/null
+++ b/examples/sundials.hs
@@ -0,0 +1,48 @@
1{-# LANGUAGE ViewPatterns #-}
2import Numeric.Sundials.ARKode.ODE
3import Numeric.LinearAlgebra
4import Graphics.Plot
5
6vanderpol mu = do
7 let xdot mu t [x,v] = [v, -x + mu * v * (1-x^2)]
8 ts = linspace 1000 (0,50)
9 sol = toColumns $ odeSolve (xdot mu) [1,0] ts
10 mplot (ts : sol)
11 mplot sol
12
13
14harmonic w d = do
15 let xdot w d t [x,v] = [v, a*x + b*v] where a = -w^2; b = -2*d*w
16 ts = linspace 100 (0,20)
17 sol = odeSolve (xdot w d) [1,0] ts
18 mplot (ts : toColumns sol)
19
20
21kepler v a = mplot (take 2 $ toColumns sol) where
22 xdot t [x,y,vx,vy] = [vx,vy,x*k,y*k]
23 where g=1
24 k=(-g)*(x*x+y*y)**(-1.5)
25 ts = linspace 100 (0,30)
26 sol = odeSolve xdot [4, 0, v * cos (a*degree), v * sin (a*degree)] ts
27 degree = pi/180
28
29
30main = do
31 vanderpol 2
32 harmonic 1 0
33 harmonic 1 0.1
34 kepler 0.3 60
35 kepler 0.4 70
36 vanderpol' 2
37
38-- example of odeSolveV with jacobian
39vanderpol' mu = do
40 let xdot mu t (toList->[x,v]) = fromList [v, -x + mu * v * (1-x^2)]
41 jac t (toList->[x,v]) = (2><2) [ 0 , 1
42 , -1-2*x*v*mu, mu*(1-x**2) ]
43 ts = linspace 1000 (0,50)
44 hi = pure $ (ts!1 - ts!0) / 100.0
45 sol = toColumns $ odeSolveV (SDIRK_5_3_4 jac) hi 1E-8 1E-8 (xdot mu) (fromList [1,0]) ts
46 mplot sol
47
48