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