Re: roots
- To: mathgroup at smc.vnet.net
- Subject: [mg29399] Re: [mg29380] roots
- From: BobHanlon at aol.com
- Date: Sat, 16 Jun 2001 22:43:48 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 2001/6/16 2:54:32 AM, maarten.vanderburgt at icos.be writes: >What is the easiest solution to finding the 13 (real) roots of 0.05 x + >Cos[x] == 0. >I can be easily seen that there are 13 roots by plotting: >Plot[{0.05 x,-Cos[x]},{x,-30,30}] >but this is not sufficient for finding accurate numerical values. > >FindRoot only gives one value and it is hard to predict which root it will >find with a specified start value: > >In[83]:= FindRoot[0.05 x +Cos[x] == 0,{x,0}] > >Out[83]= {x -> -4.96317} > >There are three roots closer to 0 then x == -4.96317 > >An NSolve only works for polynomials. > >Is there no simple way to find all roots of such an equation, eventually >within a specified range. > Here is an approach findAllRoots[eqn_, {x_Symbol, sp_?NumericQ}, Interval[{xmin_?NumericQ, xmax_?NumericQ}], n_Integer?Positive, opts___?OptionQ] := Module[ {soln = {FindRoot[eqn, {x, sp}, opts]}, cr}, While[Length[soln] < n, cr = FindRoot[eqn, {x, Random[Real, {xmin, xmax}]}, opts]; If[xmin <= (x/.cr) <= xmax, soln = Union[Append[soln, cr], SameTest -> (Abs[(x/.#1)-(x/.#2)]<10^-5&)]]]; Sort[soln]]; expr = 0.05 x +Cos[x]; nbrRoots = 13; ans = findAllRoots[expr==0, {x, 0}, Interval[{-20, 18}], nbrRoots]; The maximum absolute error is Max[Abs[expr /. ans]] 4.5600301290527057*^-7 Plot[expr, {x, -26, 23}, PlotStyle -> RGBColor[0, 0, 1], Epilog -> {AbsolutePointSize[4], RGBColor[1, 0, 0], Point[{x,expr}] /.ans}]; Bob Hanlon Chantilly, VA USA