File: rootfind.tcl

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (343 lines) | stat: -rw-r--r-- 8,839 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# rootfind.tcl --
#     Root-finding procedures:
#     - Bisection
#     - Secant
#     - Brent
#     - Chandrupatla
#
#     TODO: f(root), number of steps, converged or not
#
#     TOMS748? Seems quite complicated
#     Brent in stead of secant - more robust, Chandrupatla is supposed to be faster
#

namespace eval ::math::calculus {}

# root_bisection --
#     Find a root of a function of one variable via bisection
#
# Arguments:
#     f                Procedure implementing the function
#     a                Left point of the interval
#     b                Right point of the interval
#     tol              Tolerance (optional)
#
# Returns:
#     The approximation of the root
#
# Note:
#     The interval [a,b] must enclose an odd number of roots,
#     that is: f(a)*f(b) < 0
#
proc ::math::calculus::root_bisection {f a b {tol 1.0e-7}} {

    if { $tol <= 0.0 } {
        return -code error "The tolerance must be a small positive value"
    }

    set fa [$f $a]
    set fb [$f $b]

    if { ($fa < 0.0 && $fb < 0.0) || ($fa > 0.0 && $fb > 0.0) } {
        return -code error "The given interval does not enclose an odd number of roots: f($a) = $fa, f($b) = $fb"
    }

    set steps    0

    set reduction [expr {min( 2.0e16, abs($b-$a)/$tol )}]
    set maxsteps [expr {int( log($reduction)/log(2.0) )}] ;# Maximum number of halvings that makes sense

    while { $steps < $maxsteps } {
        incr steps

        set c  [expr {($a + $b)/ 2.0}]
        set fc [$f $c]

        if { ($fc <= 0.0 && $fa <= 0.0) || ($fc >= 0.0 && $fa >= 0.0) } {
            set a  $c
            set fa $fc
        } else {
            set b $c
            set fb $fc
        }

        # Special case ...
        if { $fc == 0.0 } {
            return $c
        }
    }

    return $c
}

# root_secant --
#     Find a root of a function of one variable via the secant method
#
# Arguments:
#     f                Procedure implementing the function
#     a                Left point of the interval
#     b                Right point of the interval
#     tol              Tolerance (optional)
#
# Returns:
#     The approximation of the root
#
# Note:
#     The method is not guranteed to converge, but if it does, it does so
#     quicker than linear. The maximum number of steps is derived from the
#     idea that the interval will be roughly halved in length.
#
proc ::math::calculus::root_secant {f a b {tol 1.0e-7}} {

    if { $tol <= 0.0 } {
        return -code error "The tolerance must be a small positive value"
    }

    set fa [$f $a]
    set fb [$f $b]

    if { ($fa < 0.0 && $fb < 0.0) || ($fa > 0.0 && $fb > 0.0) } {
        return -code error "The given interval does not enclose an odd number of roots: f($a) = $fa, f($b) = $fb"
    }

    set steps    0

    set reduction [expr {min( 2.0e16, abs($b-$a)/$tol )}]
    set maxsteps [expr {int( log($reduction)/log(2.0) )}] ;# Maximum number of halvings that makes sense


    while { $steps < $maxsteps } {
        incr steps

        set c  [expr {($a * $fb - $b * $fa) / double($fb - $fa)}]
        set fc [$f $c]

        if { abs($c - $b) <= $tol } {
            break
        }

        set a  $b
        set fa $fb
        set b  $c
        set fb $fc
    }

    return $c
}

# root_chandrupatla --
#     Find a root of a function of one variable via the method by Chandrupatla (variation on Brent's method)
#
# Arguments:
#     f                Procedure implementing the function
#     x0               Left point of the interval
#     x1               Right point of the interval
#     tol              Tolerance (optional)
#
# Returns:
#     The approximation of the root
#
# Note:
#     The method brackets a root, like the bisection method.
#
#     Adopted from www.embeddedrelated.com/showarticle/855.php
#
proc ::math::calculus::root_chandrupatla {f x0 x1 {tol 1.0e-7}} {

    if { $tol <= 0.0 } {
        return -code error "The tolerance must be a small positive value"
    }

    set b $x0
    set a $x1
    set c $x1

    set fa [$f $a]
    set fb [$f $b]
    set fc $fa

    if { ($fa < 0.0 && $fb < 0.0) || ($fa > 0.0 && $fb > 0.0) } {
        return -code error "The given interval does not enclose an odd number of roots: f($a) = $fa, f($b) = $fb"
    }

    set steps 0

    set t 0.5

    set eps_m $tol
    set eps_a [expr {2.0 * $tol}] ;# TODO

    while { 1 } {
        incr steps

        #
        # Get a new estimate of the root via interpolation
        #
        set xt [expr {$a + $t * ($b - $a)}]
        set ft [$f $xt]

        #
        # Update the three points we keep track of
        #
        if { ($ft < 0.0 && $fa < 0.0) || ($ft > 0.0 && $fa > 0.0) } {
            set c  $a
            set fc $fa
        } else {
            set c  $b
            set fc $fb
            set b  $a
            set fb $fa
        }

        set a  $xt
        set fa $ft

        #
        # Determine the point with the smallest function value
        #
        if { abs($fa) < abs($fb) } {
            set xm $a
            set fm $fa
        } else {
            set xm $b
            set fm $fb
        }

        if { $fm == 0.0 } {
            return $xm
        }

        #
        # Critical values xi and phi (decisions on how to proceed)
        #
        set phtol [expr {2.0*$eps_m * abs($xm) + $eps_a}]
        set tlim  [expr {$phtol / abs($b-$c)}]

        if { $tlim > 0.5 } {
            return $xm
        }

        set xi     [expr {($a - $b) / ($c - $b)}]
        set phi    [expr {($fa - $fb) / ($fc - $fb)}]
        set do_iqi [expr {$phi**2 < $xi && (1.0 - $phi)**2 < 1.0 - $xi}]

        if { $do_iqi } {
            #
            # Inverse quadratic interpolation
            #
            set t [expr {$fa / ($fb-$fa) * $fc / ($fb-$fc) +
                         ($c-$a) / ($b-$a) * $fa / ($fc-$a) * $fb / ($fc-$fb)}]
        } else {
            #
            # Bisection
            #
            set t 0.5
        }

        #
        # Limit t between (tlim,1-tlim)
        #
        set t [expr {min( 1.0 -$tlim, max($tlim, $t) )}]
    }
}

# root_brent --
#     Find a root of a function of one variable via Brent's method
#
# Arguments:
#     f                Procedure implementing the function
#     x0               Left point of the interval
#     x1               Right point of the interval
#     tol              Tolerance (optional)
#
# Returns:
#     The approximation of the root
#
# Note:
#     The method brackets a root, like the bisection method but combines it with the secant method and
#     inverse quadratic interpolation.
#
#     Adopted from Wikipedia
#
proc ::math::calculus::root_brent {f x0 x1 {tol 1.0e-7}} {

    if { $tol <= 0.0 } {
        return -code error "The tolerance must be a small positive value"
    }

    set b $x0
    set a $x1
    set c $x1

    #
    # Minimal distance between points at which to calculate the function
    #
    set delta [expr {1.0e-8 * (abs($x0) + abs($x1)) + $tol/3.0}]

    set fa [$f $a]
    set fb [$f $b]

    if { ($fa < 0.0 && $fb < 0.0) || ($fa > 0.0 && $fb > 0.0) } {
        return -code error "The given interval does not enclose an odd number of roots: f($a) = $fa, f($b) = $fb"
    }

    if { abs($fa) < abs($fb) } {
        set tmp $a    ; set tmpf $fa
        set a   $b    ; set fa   $fb
        set b   $tmp  ; set fb   $tmpf
    }

    set c    $a
    set fc   $fa

    set flag 1
    set d    0.0 ;# Dummy for the first iteration step

    set s    $b  ;# Make sure s is defined
    set fs   $fb

    #set step 0

    while { abs($b - $a) > $tol && $fb != 0.0 && $fs != 0.0 } {
        #incr step
        if { $fa != $fc && $fb != $fc } {
            set s [expr { $a * $fb * $fc / (($fa-$fb)*($fa-$fc)) +
                          $b * $fa * $fc / (($fb-$fa)*($fb-$fc)) +
                          $c * $fa * $fb / (($fc-$fa)*($fc-$fb)) }]
        } else {
            set s [expr {$b - $fb * ($b-$a) / ($fb -$fa)}]
        }

        # Check the conditions for the next step
        if { ( ((3.0*$a+$b) / 4.0 - $s) * ($s - $b) < 0.0 ) ||
             ( $flag  && abs($s-$b) >= abs($b-$c)/2.0 )     ||
             ( !$flag && abs($s-$b) >= abs($c-$d)/2.0 )     ||
             ( $flag  && abs($b-$c) <  abs($delta) )        ||
             ( !$flag && abs($c-$d) <  abs($delta) )           } {
            set s    [expr {($a + $b) / 2.0}]
            set flag 1
        } else {
            set flag 0
        }

        set fs [$f $s]
        set d  $c
        set c  $b

        if { $fa * $fs < 0.0 } {
            set b  $s
            set fb $fs
        } else {
            set a  $s
            set fa $fs
        }

        if { abs($a) < abs($fb) } {
            set tmp $a    ; set tmpf $fa
            set a   $b    ; set fa   $fb
            set b   $tmp  ; set fb   $tmpf
        }
    }

    return $s
}