Finding Roots in an Interval

Code for finding the extrema of functions over a domain using [Graphics:../Images/tricks_gr_82.gif] and [Graphics:../Images/tricks_gr_83.gif] was presented in "Maxima and Minima over an Interval" (TMJ 6(4): 26, 1996). [Graphics:../Images/tricks_gr_84.gif] was used to help find the sign changes of a function in "The Vibrating Ellipse-shaped Drum," by Michael Trott (TMJ 6(4): 59, 1996). Here is a similar technique for finding the zeros of a function in an interval.

First we plot the function over the interval.


We use [Graphics:../Images/tricks_gr_86.gif] to extract the [Graphics:../Images/tricks_gr_87.gif] object and hence the points that generate the curve.


Alternatively, we could use


or even


These three methods yield equivalent results for this example.


However, the first method is better for several reasons. Finding and projecting out the appropriate level using [Graphics:../Images/tricks_gr_95.gif] is not always obvious. The implementation using [Graphics:../Images/tricks_gr_96.gif] is clearer. If the output order of [Graphics:../Images/tricks_gr_97.gif] changes in future versions of Mathematica, the methods that rely on [Graphics:../Images/tricks_gr_98.gif] may break.

[Graphics:../Images/tricks_gr_99.gif] is a natural tool to check each pair of points and test whether a change in sign occurs between them. [Graphics:../Images/tricks_gr_100.gif] then identifies the points on either side of each sign change.


Now we use [Graphics:../Images/tricks_gr_105.gif] to get accurate values for the zeros of the function in the interior of the plot range.


These steps are easily turned into a procedure.


Here is an example:


The code passes options to both the [Graphics:../Images/tricks_gr_113.gif] and [Graphics:../Images/tricks_gr_114.gif] functions. Since these functions have different sets of options, we use [Graphics:../Images/tricks_gr_115.gif] to filter out the options appropriate to each of them.


Both [Graphics:../Images/tricks_gr_118.gif] and [Graphics:../Images/tricks_gr_119.gif] are [Graphics:../Images/tricks_gr_120.gif],


so we need to wrap [Graphics:../Images/tricks_gr_123.gif] about the [Graphics:../Images/tricks_gr_124.gif] calls.

[Graphics:../Images/tricks_gr_125.gif] uses adaptive sampling, that is, it automatically increases the number of plot points where the curvature of the graph increases. However, we can manually increase the number of sampled points to avoid missing roots. For the example above, if we increase the value of the [Graphics:../Images/tricks_gr_126.gif] option from the default setting of 25, we find two additional roots. (Here, the [Graphics:../Images/tricks_gr_127.gif] option is passed to [Graphics:../Images/tricks_gr_128.gif], while the [Graphics:../Images/tricks_gr_129.gif] option is passed to [Graphics:../Images/tricks_gr_130.gif].)


[Graphics:../Images/tricks_gr_133.gif] has a default tolerance of [Graphics:../Images/tricks_gr_134.gif].  Hence increasing the [Graphics:../Images/tricks_gr_135.gif] allows us to use [Graphics:../Images/tricks_gr_136.gif] to verify that these values are roots of [Graphics:../Images/tricks_gr_137.gif]:


The principle of using [Graphics:../Images/tricks_gr_140.gif] and pattern-matching is quite general and it works with any graphics object. Instead of the pattern [Graphics:../Images/tricks_gr_141.gif], we could use a more general operation (which could include conditional testing) such as


For another example of the use of [Graphics:../Images/tricks_gr_144.gif] and [Graphics:../Images/tricks_gr_145.gif], see "Adding Options to Plot" (below).

Document converted by Mathematica of Wolfram Research