summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2009-06-04 09:01:56 +0000
committerAlberto Ruiz <aruiz@um.es>2009-06-04 09:01:56 +0000
commit6e0dd472ef8c570ec1924ac641e5872db30ac142 (patch)
tree64963c6af75cdbc02336de82b51136964f36dc73 /examples
parentf49ac4def26b38d3d084375007715156be347412 (diff)
added some root finding algorithms
Diffstat (limited to 'examples')
-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