Re: Find many Roots

• To: mathgroup at smc.vnet.net
• Subject: [mg32181] Re: [mg32098] Find many Roots
• From: Daniel Lichtblau <danl at wolfram.com>
• Date: Fri, 4 Jan 2002 05:03:56 -0500 (EST)
• References: <200112240832.DAA15698@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Yoram Pollack wrote:
>
> Hello,
> I am trying to find all roots of the Equation: Tan[x]==1/x, {x,0,12}].
> What is the best way?
> Thanks
> Yoram

Several nice methods have been posted. I thought I would draw attention
to a similar question from last June.

http://library.wolfram.com/mathgroup/archive/2001/Jun/msg00226.html

The biggest difference is that the present function has poles. One can
instead find zeroes of x*Sin[x]-Cos[x] to avoid this issue.

My own reply to the June post (URL below) gave one method for attacking
such problems, using the Cauchy integral formula to count roots in an
interval, bisection to isolate them, and CIF again to approximate
individually isolated roots.

http://library.wolfram.com/mathgroup/archive/2001/Jun/msg00226.html

Since the code was not too long I just include it below. The actual note
and potential extensions of this approach.

epsilon = 10^(-1);
Off[NIntegrate::slwcon]

segmentIntegrate[func_,a_,b_,offset_,z_, f2_:1] :=
Module[{t1,t2,t3,integrand=f2*D[func,z]/func},
t1 = NIntegrate[
Evaluate[(integrand/.z->z-I*offset)-
(integrand/.z->z+I*offset)], {z,a,b},
PrecisionGoal->3, MaxRecursion->10];
t2 = NIntegrate[Evaluate[integrand], {z,a+I*offset,a-I*offset},
PrecisionGoal->3, MaxRecursion->10];
t3 = NIntegrate[Evaluate[integrand], {z,b-I*offset,b+I*offset},
PrecisionGoal->3, MaxRecursion->10];
(t1+t2+t3)/(2*Pi*I)]

isolateRoots[func_,lower_,upper_,z_,eps_] :=
Module[{numroots,avg=(lower+upper)/2,leftroots},
numroots = Round[segmentIntegrate[func,lower,upper,eps,z]];
If[numroots==0, Return[{}]];
If[numroots==1, Return[{Interval[{lower,upper}]}]];
leftroots = isolateRoots[func,lower,avg,z,eps];
If[Length[leftroots]==numroots,Return[leftroots]];
Join[leftroots,isolateRoots[func,avg,upper,z,eps]]
]

refineRoots[func_,intervals_,eps_,z_]:=
Map[Re[segmentIntegrate[func,#[[1,1]],#[[1,2]],eps,z,z]]&, intervals]

Now we try it out.

f[x_] := x*Sin[x]-Cos[x]

InputForm[Timing[rootintervals =
isolateRoots[f[z],0,12,z,epsilon]]]

This gives some useless messages regarding loss of precision, followed
by

Out//InputForm=
{0.03999999999999915*Second, {Interval[{0, 3}], Interval[{3, 6}],
Interval[{6, 9}], Interval[{9, 12}]}}

In:= Timing[rts=refineRoots[f[z],rootintervals,epsilon,z]]
Out= {0.02 Second, {0.860334, 3.42562, 6.4373, 9.52933}}

We see that the residuals are not fairly small.

In:= InputForm[f[x] /. x->rts]
Out//InputForm=
{-3.984138952084493*^-8, 3.80665711174899*^-8, 3.887810040570372*^-8,
7.042598237916309*^-8}

We could of course do better either by higher precision quadrature or,
more sensibly, by taking these as starting values for a high
precision/accuracy FindRoot.

Other replies to the June query gave code to implement entirely
different methods e.g. polynomial approximation, interval search, and
maybe others as well; these all could be applied to this example as
well.

Daniel Lichtblau
Wolfram Research

```

• Prev by Date: Re: ORDINARY DIFFERENTIAL EQUATION
• Next by Date: Re: Front end problems!
• Previous by thread: Re: Solutions that are not solutions
• Next by thread: Re: Find many Roots
{"ft_lang_label":"__label__en","ft_lang_prob":0.71346414,"math_prob":0.95608133,"size":3261,"snap":"2022-27-2022-33","text_gpt3_token_len":999,"char_repetition_ratio":0.09855695,"word_repetition_ratio":0.0,"special_character_ratio":0.32842687,"punctuation_ratio":0.2446352,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9913461,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-04T04:07:52Z\",\"WARC-Record-ID\":\"<urn:uuid:f2d7ba3e-1313-4ff2-b866-2ef6d2053c90>\",\"Content-Length\":\"46950\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8ee9028e-87a7-44ea-8241-022f5e93254b>\",\"WARC-Concurrent-To\":\"<urn:uuid:e43fa3af-49c6-45d8-b3ec-ea2ec2c1c622>\",\"WARC-IP-Address\":\"140.177.205.73\",\"WARC-Target-URI\":\"https://forums.wolfram.com/mathgroup/archive/2002/Jan/msg00011.html\",\"WARC-Payload-Digest\":\"sha1:4BRZJLOMREX4MCF7NIWXZEHM2K23ENKV\",\"WARC-Block-Digest\":\"sha1:QF433EMNJKUGUEZOTVIYXONYGV4BIEXM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104293758.72_warc_CC-MAIN-20220704015700-20220704045700-00676.warc.gz\"}"}