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.
![[Graphics:../Images/tricks_gr_85.gif]](../Images/tricks_gr_85.gif)
We use to extract the
object and hence the points that generate the curve.
![[Graphics:../Images/tricks_gr_88.gif]](../Images/tricks_gr_88.gif)
![[Graphics:../Images/tricks_gr_89.gif]](../Images/tricks_gr_89.gif)
![[Graphics:../Images/tricks_gr_90.gif]](../Images/tricks_gr_90.gif)
Alternatively, we could use
![[Graphics:../Images/tricks_gr_91.gif]](../Images/tricks_gr_91.gif)
or even
![[Graphics:../Images/tricks_gr_92.gif]](../Images/tricks_gr_92.gif)
These three methods yield equivalent results for this example.
![[Graphics:../Images/tricks_gr_93.gif]](../Images/tricks_gr_93.gif)
![[Graphics:../Images/tricks_gr_94.gif]](../Images/tricks_gr_94.gif)
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.
![[Graphics:../Images/tricks_gr_101.gif]](../Images/tricks_gr_101.gif)
![[Graphics:../Images/tricks_gr_102.gif]](../Images/tricks_gr_102.gif)
![[Graphics:../Images/tricks_gr_103.gif]](../Images/tricks_gr_103.gif)
![[Graphics:../Images/tricks_gr_104.gif]](../Images/tricks_gr_104.gif)
Now we use to
get accurate values for the zeros of the function in the interior of the plot range.
![[Graphics:../Images/tricks_gr_106.gif]](../Images/tricks_gr_106.gif)
![[Graphics:../Images/tricks_gr_107.gif]](../Images/tricks_gr_107.gif)
These steps are easily turned into a procedure.
![[Graphics:../Images/tricks_gr_108.gif]](../Images/tricks_gr_108.gif)
![[Graphics:../Images/tricks_gr_109.gif]](../Images/tricks_gr_109.gif)
Here is an example:
![[Graphics:../Images/tricks_gr_110.gif]](../Images/tricks_gr_110.gif)
![[Graphics:../Images/tricks_gr_111.gif]](../Images/tricks_gr_111.gif)
![[Graphics:../Images/tricks_gr_112.gif]](../Images/tricks_gr_112.gif)
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.
![[Graphics:../Images/tricks_gr_116.gif]](../Images/tricks_gr_116.gif)
![[Graphics:../Images/tricks_gr_117.gif]](../Images/tricks_gr_117.gif)
Both and
are
,
![[Graphics:../Images/tricks_gr_121.gif]](../Images/tricks_gr_121.gif)
![[Graphics:../Images/tricks_gr_122.gif]](../Images/tricks_gr_122.gif)
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 .)
![[Graphics:../Images/tricks_gr_131.gif]](../Images/tricks_gr_131.gif)
![[Graphics:../Images/tricks_gr_132.gif]](../Images/tricks_gr_132.gif)
has
a default tolerance of . Hence
increasing the
allows us to use to
verify that these values are roots of :
![[Graphics:../Images/tricks_gr_138.gif]](../Images/tricks_gr_138.gif)
![[Graphics:../Images/tricks_gr_139.gif]](../Images/tricks_gr_139.gif)
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
![[Graphics:../Images/tricks_gr_142.gif]](../Images/tricks_gr_142.gif)
![[Graphics:../Images/tricks_gr_143.gif]](../Images/tricks_gr_143.gif)
For another example of the use of and
,
see "Adding Options to Plot" (below).
Document converted by Mathematica of Wolfram
Research |