]> gitweb.michael.orlitzky.com - numerical-analysis.git/blob - src/Roots/Simple.hs
Make numbers == 3000.1.* required.
[numerical-analysis.git] / src / Roots / Simple.hs
1 -- | The Roots.Simple module contains root-finding algorithms. That
2 -- is, procedures to (numerically) find solutions to the equation,
3 --
4 -- > f(x) = 0
5 --
6 -- where f is assumed to be continuous on the interval of interest.
7 --
8
9 module Roots.Simple
10 where
11
12 import Data.List (find)
13
14 import qualified Roots.Fast as F
15
16 -- | Does the (continuous) function @f@ have a root on the interval
17 -- [a,b]? If f(a) <] 0 and f(b) ]> 0, we know that there's a root in
18 -- [a,b] by the intermediate value theorem. Likewise when f(a) >= 0
19 -- and f(b) <= 0.
20 --
21 -- Examples:
22 --
23 -- >>> let f x = x**3
24 -- >>> has_root f (-1) 1 Nothing
25 -- True
26 --
27 -- This fails if we don't specify an @epsilon@, because cos(-2) ==
28 -- cos(2) doesn't imply that there's a root on [-2,2].
29 --
30 -- >>> has_root cos (-2) 2 Nothing
31 -- False
32 -- >>> has_root cos (-2) 2 (Just 0.001)
33 -- True
34 --
35 has_root :: (Fractional a, Ord a, Ord b, Num b)
36 => (a -> b) -- ^ The function @f@
37 -> a -- ^ The \"left\" endpoint, @a@
38 -> a -- ^ The \"right\" endpoint, @b@
39 -> Maybe a -- ^ The size of the smallest subinterval
40 -- we'll examine, @epsilon@
41 -> Bool
42 has_root f a b epsilon =
43 F.has_root f a b epsilon Nothing Nothing
44
45
46
47
48 -- | We are given a function @f@ and an interval [a,b]. The bisection
49 -- method checks finds a root by splitting [a,b] in half repeatedly.
50 --
51 -- If one is found within some prescribed tolerance @epsilon@, it is
52 -- returned. Otherwise, the interval [a,b] is split into two
53 -- subintervals [a,c] and [c,b] of equal length which are then both
54 -- checked via the same process.
55 --
56 -- Returns 'Just' the value x for which f(x) == 0 if one is found,
57 -- or Nothing if one of the preconditions is violated.
58 --
59 -- Examples:
60 --
61 -- >>> bisect cos 1 2 0.001
62 -- Just 1.5712890625
63 --
64 -- >>> bisect sin (-1) 1 0.001
65 -- Just 0.0
66 --
67 bisect :: (Fractional a, Ord a, Num b, Ord b)
68 => (a -> b) -- ^ The function @f@ whose root we seek
69 -> a -- ^ The \"left\" endpoint of the interval, @a@
70 -> a -- ^ The \"right\" endpoint of the interval, @b@
71 -> a -- ^ The tolerance, @epsilon@
72 -> Maybe a
73 bisect f a b epsilon =
74 F.bisect f a b epsilon Nothing Nothing
75
76
77
78 -- | The sequence x_{n} of values obtained by applying Newton's method
79 -- on the function @f@ and initial guess @x0@.
80 --
81 -- Examples:
82 --
83 -- Atkinson, p. 60.
84 -- >>> let f x = x^6 - x - 1
85 -- >>> let f' x = 6*x^5 - 1
86 -- >>> tail $ take 4 $ newton_iterations f f' 2
87 -- [1.6806282722513088,1.4307389882390624,1.2549709561094362]
88 --
89 newton_iterations :: (Fractional a, Ord a)
90 => (a -> a) -- ^ The function @f@ whose root we seek
91 -> (a -> a) -- ^ The derivative of @f@
92 -> a -- ^ Initial guess, x-naught
93 -> [a]
94 newton_iterations f f' x0 =
95 iterate next x0
96 where
97 next xn =
98 xn - ( (f xn) / (f' xn) )
99
100
101
102 -- | Use Newton's method to find a root of @f@ near the initial guess
103 -- @x0@. If your guess is bad, this will recurse forever!
104 --
105 -- Examples:
106 --
107 -- Atkinson, p. 60.
108 --
109 -- >>> let f x = x^6 - x - 1
110 -- >>> let f' x = 6*x^5 - 1
111 -- >>> let Just root = newtons_method f f' (1/1000000) 2
112 -- >>> root
113 -- 1.1347241385002211
114 -- >>> abs (f root) < 1/100000
115 -- True
116 --
117 -- >>> import Data.Number.BigFloat
118 -- >>> let eps = 1/(10^20) :: BigFloat Prec50
119 -- >>> let Just root = newtons_method f f' eps 2
120 -- >>> root
121 -- 1.13472413840151949260544605450647284028100785303643e0
122 -- >>> abs (f root) < eps
123 -- True
124 --
125 newtons_method :: (Fractional a, Ord a)
126 => (a -> a) -- ^ The function @f@ whose root we seek
127 -> (a -> a) -- ^ The derivative of @f@
128 -> a -- ^ The tolerance epsilon
129 -> a -- ^ Initial guess, x-naught
130 -> Maybe a
131 newtons_method f f' epsilon x0
132 = find (\x -> abs (f x) < epsilon) x_n
133 where
134 x_n = newton_iterations f f' x0
135
136
137
138 -- | Takes a function @f@ of two arguments and repeatedly applies @f@
139 -- to the previous two values. Returns a list containing all
140 -- generated values, f(x0, x1), f(x1, x2), f(x2, x3)...
141 --
142 -- Examples:
143 --
144 -- >>> let fibs = iterate2 (+) 0 1
145 -- >>> take 15 fibs
146 -- [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377]
147 --
148 iterate2 :: (a -> a -> a) -- ^ The function @f@
149 -> a -- ^ The initial value @x0@
150 -> a -- ^ The second value, @x1@
151 -> [a] -- ^ The result list, [x0, x1, ...]
152 iterate2 f x0 x1 =
153 x0 : x1 : (go x0 x1)
154 where
155 go prev2 prev1 =
156 let next = f prev2 prev1 in
157 next : go prev1 next
158
159 -- | The sequence x_{n} of values obtained by applying the secant
160 -- method on the function @f@ and initial guesses @x0@, @x1@.
161 --
162 -- The recursion more or less implements a two-parameter 'iterate',
163 -- although one list is passed to the next iteration (as opposed to
164 -- one function argument, with iterate). At each step, we peel the
165 -- first two elements off the list and then compute/append elements
166 -- three, four... onto the end of the list.
167 --
168 -- Examples:
169 --
170 -- Atkinson, p. 67.
171 -- >>> let f x = x^6 - x - 1
172 -- >>> take 4 $ secant_iterations f 2 1
173 -- [2.0,1.0,1.0161290322580645,1.190577768676638]
174 --
175 secant_iterations :: (Fractional a, Ord a)
176 => (a -> a) -- ^ The function @f@ whose root we seek
177 -> a -- ^ Initial guess, x-naught
178 -> a -- ^ Second initial guess, x-one
179 -> [a]
180 secant_iterations f x0 x1 =
181 iterate2 g x0 x1
182 where
183 g prev2 prev1 =
184 let x_change = prev1 - prev2
185 y_change = (f prev1) - (f prev2)
186 in
187 (prev1 - (f prev1 * (x_change / y_change)))
188
189
190 -- | Use the secant method to find a root of @f@ near the initial guesses
191 -- @x0@ and @x1@. If your guesses are bad, this will recurse forever!
192 --
193 -- Examples:
194 --
195 -- Atkinson, p. 67.
196 -- >>> let f x = x^6 - x - 1
197 -- >>> let Just root = secant_method f (1/10^9) 2 1
198 -- >>> root
199 -- 1.1347241384015196
200 -- >>> abs (f root) < (1/10^9)
201 -- True
202 --
203 secant_method :: (Fractional a, Ord a)
204 => (a -> a) -- ^ The function @f@ whose root we seek
205 -> a -- ^ The tolerance epsilon
206 -> a -- ^ Initial guess, x-naught
207 -> a -- ^ Second initial guess, x-one
208 -> Maybe a
209 secant_method f epsilon x0 x1
210 = find (\x -> abs (f x) < epsilon) x_n
211 where
212 x_n = secant_iterations f x0 x1
213
214
215
216 fixed_point_iterations :: (a -> a) -- ^ The function @f@ to iterate.
217 -> a -- ^ The initial value @x0@.
218 -> [a] -- ^ The resulting sequence of x_{n}.
219 fixed_point_iterations f x0 =
220 iterate f x0
221
222
223 -- | Find a fixed point of the function @f@ with the search starting
224 -- at x0. This will find the first element in the chain f(x0),
225 -- f(f(x0)),... such that the magnitude of the difference between it
226 -- and the next element is less than epsilon.
227 --
228 fixed_point :: (Num a, Ord a)
229 => (a -> a) -- ^ The function @f@ to iterate.
230 -> a -- ^ The tolerance, @epsilon@.
231 -> a -- ^ The initial value @x0@.
232 -> a -- ^ The fixed point.
233 fixed_point f epsilon x0 =
234 fst winning_pair
235 where
236 xn = fixed_point_iterations f x0
237 xn_plus_one = tail $ fixed_point_iterations f x0
238
239 abs_diff v w =
240 abs (v - w)
241
242 -- The nth entry in this list is the absolute value of x_{n} -
243 -- x_{n+1}.
244 differences = zipWith abs_diff xn xn_plus_one
245
246 -- A list of pairs, (xn, |x_{n} - x_{n+1}|).
247 pairs = zip xn differences
248
249 -- The pair (xn, |x_{n} - x_{n+1}|) with
250 -- |x_{n} - x_{n+1}| < epsilon. The pattern match on 'Just' is
251 -- "safe" since the list is infinite. We'll succeed or loop
252 -- forever.
253 Just winning_pair = find (\(_, diff) -> diff < epsilon) pairs