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.

[Graphics:../Images/tricks_gr_85.gif]

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.

[Graphics:../Images/tricks_gr_88.gif]
[Graphics:../Images/tricks_gr_89.gif]
[Graphics:../Images/tricks_gr_90.gif]

Alternatively, we could use

[Graphics:../Images/tricks_gr_91.gif]

or even

[Graphics:../Images/tricks_gr_92.gif]

These three methods yield equivalent results for this example.

[Graphics:../Images/tricks_gr_93.gif]
[Graphics:../Images/tricks_gr_94.gif]

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.

[Graphics:../Images/tricks_gr_101.gif]
[Graphics:../Images/tricks_gr_102.gif]
[Graphics:../Images/tricks_gr_103.gif]
[Graphics:../Images/tricks_gr_104.gif]

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.

[Graphics:../Images/tricks_gr_106.gif]
[Graphics:../Images/tricks_gr_107.gif]

These steps are easily turned into a procedure.

[Graphics:../Images/tricks_gr_108.gif]
[Graphics:../Images/tricks_gr_109.gif]

Here is an example:

[Graphics:../Images/tricks_gr_110.gif]
[Graphics:../Images/tricks_gr_111.gif]
[Graphics:../Images/tricks_gr_112.gif]

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.

[Graphics:../Images/tricks_gr_116.gif]
[Graphics:../Images/tricks_gr_117.gif]

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

[Graphics:../Images/tricks_gr_121.gif]
[Graphics:../Images/tricks_gr_122.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_131.gif]
[Graphics:../Images/tricks_gr_132.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]:

[Graphics:../Images/tricks_gr_138.gif]
[Graphics:../Images/tricks_gr_139.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

[Graphics:../Images/tricks_gr_142.gif]
[Graphics:../Images/tricks_gr_143.gif]

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