summaryrefslogtreecommitdiff
path: root/examples/root.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/root.hs')
-rw-r--r--examples/root.hs30
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
2import Numeric.GSL
3import Numeric.LinearAlgebra
4import Graphics.Plot
5import Text.Printf(printf)
6
7rosenbrock a b [x,y] = [ a*(1-x), b*(y-x^2) ]
8
9disp = putStrLn . format " " (printf "%.3f")
10
11-- Numerical estimation of the gradient
12gradient f v = [partialDerivative k f v | k <- [0 .. length v -1]]
13
14partialDerivative 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
18test 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
26main = do
27 test Hybrids
28 test Hybrid
29 test DNewton
30 test Broyden