1 #!/usr/bin/octave --silent
3 function root = bisect(f, a, b, epsilon)
4 ## Find a root of the function `f` on the closed interval [a,b].
6 ## It is assumed that there are an odd number of roots within [a,b].
7 ## The intermediate value theorem is used to determine whether or not
8 ## there is a root on a given interval. So, for example, cos(-2) and
9 ## cos(2) are both less than zero; the I.V.T. would not conclude that
10 ## cos(x) has a root on -2<=x<=2.
12 ## If `f` has more than one root on [a,b], the smallest root will be
13 ## returned. This is an implementation detail.
15 ## Store these so we don't have to recompute them.
19 ## We first check for roots at a,b before bothering to bisect the
31 ## Bisect the interval.
34 ## If we're within the prescribed tolerance, we're done.
40 if (has_root(fa,f(c)))
41 root = bisect(f,a,c,epsilon);
43 root = bisect(f,c,b,epsilon);