### Finding Roots in an Interval

Code for finding the extrema of functions over a domain using and was presented in "Maxima and Minima over an Interval" (TMJ 6(4): 26, 1996). 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 to extract the 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 is not always obvious. The implementation using is clearer. If the output order of changes in future versions of Mathematica, the methods that rely on may break.

is a natural tool to check each pair of points and test whether a change in sign occurs between them. then identifies the points on either side of each sign change.

Now we use 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 and functions. Since these functions have different sets of options, we use to filter out the options appropriate to each of them.

Both and are ,

so we need to wrap about the calls.

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 option from the default setting of 25, we find two additional roots. (Here, the option is passed to , while the option is passed to .)

has a default tolerance of .  Hence increasing the allows us to use to verify that these values are roots of :

The principle of using and pattern-matching is quite general and it works with any graphics object. Instead of the pattern , we could use a more general operation (which could include conditional testing) such as

For another example of the use of and , see "Adding Options to Plot" (below).

Document converted by Mathematica of Wolfram Research