Is there an elegant way to nest a function in a similar fashion to tree graphs; i.e., create and follow branches until some criterion is met (until we reach a leaf)? An example:
We want to numerically find all roots of some function within a specific interval, for instance all roots (between -4 and 4) of the polynomial
poly[x_] := (x - 3) (x - 3.2) (x - 2) (x - 1) (x) (x + 1) (x + 2) (x + 3) We search for a root via
FindRoot[poly@x, {x ,0 ,-4, 4}] (* {x -> 0.} *) We now split our search interval into two intervals [-4,0] and [0,-4] and search again for roots
FindRoots[poly@x, {x,-2, -4, 0}] FindRoots[poly@x, {x, 2, 0, 4}] (* {x->-2}*) (* {x->2}*) Repeating the search within [-4, -2], [-2, 0], [0, 2] and [2, 4] yields the additional roots: -3, -1 , 1, 3
Yet another round reveals the additional root at 3.2 via FindRoot[poly@x, {x, 3.5, 3, 4 }].
If we now check the new intervals [2, 3.2] and [3.2, 4] we don't get any new roots we didn't already know. To look for roots that we might have missed due to picking the wrong initial search values we could now vary all search intervals for instance by excluding already found roots, e.g searching for roots between [3.21, 3.99] which yields a warning from FindRoot indicating that there are no more roots inside the interval. (In this case there are no additional roots inside the other intervals as well)
FindRoot[poly@x, {x, 3.5, 3.21, 3.99}] (* The point {3.21} is at the edge of the search region {3.21,3.99} in coordinate 1 and the computed search direction points outside the region. >>*) How does one implement such an algorithm in an elegant way?
Roots[poly[x] == 0, x]? $\endgroup$Rootswould only work for polynomials and not for any arbitrary functions ) $\endgroup$