summaryrefslogtreecommitdiff
path: root/packages/hmatrix/examples/minimize.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/hmatrix/examples/minimize.hs')
-rw-r--r--packages/hmatrix/examples/minimize.hs50
1 files changed, 0 insertions, 50 deletions
diff --git a/packages/hmatrix/examples/minimize.hs b/packages/hmatrix/examples/minimize.hs
deleted file mode 100644
index 19b2cb3..0000000
--- a/packages/hmatrix/examples/minimize.hs
+++ /dev/null
@@ -1,50 +0,0 @@
1-- the multidimensional minimization example in the GSL manual
2import Numeric.GSL
3import Numeric.LinearAlgebra
4import Graphics.Plot
5import Text.Printf(printf)
6
7-- the function to be minimized
8f [x,y] = 10*(x-1)^2 + 20*(y-2)^2 + 30
9
10-- exact gradient
11df [x,y] = [20*(x-1), 40*(y-2)]
12
13-- a minimization algorithm which does not require the gradient
14minimizeS f xi = minimize NMSimplex2 1E-2 100 (replicate (length xi) 1) f xi
15
16-- Numerical estimation of the gradient
17gradient f v = [partialDerivative k f v | k <- [0 .. length v -1]]
18
19partialDerivative n f v = fst (derivCentral 0.01 g (v!!n)) where
20 g x = f (concat [a,x:b])
21 (a,_:b) = splitAt n v
22
23disp = putStrLn . format " " (printf "%.3f")
24
25allMethods :: (Enum a, Bounded a) => [a]
26allMethods = [minBound .. maxBound]
27
28test method = do
29 print method
30 let (s,p) = minimize method 1E-2 30 [1,1] f [5,7]
31 print s
32 disp p
33
34testD method = do
35 print method
36 let (s,p) = minimizeD method 1E-3 30 1E-2 1E-4 f df [5,7]
37 print s
38 disp p
39
40testD' method = do
41 putStrLn $ show method ++ " with estimated gradient"
42 let (s,p) = minimizeD method 1E-3 30 1E-2 1E-4 f (gradient f) [5,7]
43 print s
44 disp p
45
46main = do
47 mapM_ test [NMSimplex, NMSimplex2]
48 mapM_ testD allMethods
49 testD' ConjugateFR
50 mplot $ drop 3 . toColumns . snd $ minimizeS f [5,7]