diff options
Diffstat (limited to 'examples/root.hs')
-rw-r--r-- | examples/root.hs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/examples/root.hs b/examples/root.hs new file mode 100644 index 0000000..9a674fd --- /dev/null +++ b/examples/root.hs | |||
@@ -0,0 +1,30 @@ | |||
1 | -- root finding examples | ||
2 | import Numeric.GSL | ||
3 | import Numeric.LinearAlgebra | ||
4 | import Graphics.Plot | ||
5 | import Text.Printf(printf) | ||
6 | |||
7 | rosenbrock a b [x,y] = [ a*(1-x), b*(y-x^2) ] | ||
8 | |||
9 | disp = putStrLn . format " " (printf "%.3f") | ||
10 | |||
11 | -- Numerical estimation of the gradient | ||
12 | gradient f v = [partialDerivative k f v | k <- [0 .. length v -1]] | ||
13 | |||
14 | partialDerivative n f v = fst (derivCentral 0.01 g (v!!n)) where | ||
15 | g x = f (concat [a,x:b]) | ||
16 | (a,_:b) = splitAt n v | ||
17 | |||
18 | test method = do | ||
19 | print method | ||
20 | let (s,p) = root method 1E-7 30 (rosenbrock 1 10) [-10,-5] | ||
21 | print s -- solution | ||
22 | disp p -- evolution of the algorithm | ||
23 | -- let [x,y] = tail (toColumns p) | ||
24 | -- mplot [x,y] -- path from the starting point to the solution | ||
25 | |||
26 | main = do | ||
27 | test Hybrids | ||
28 | test Hybrid | ||
29 | test DNewton | ||
30 | test Broyden | ||