MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: roots

  • To: mathgroup at smc.vnet.net
  • Subject: [mg29646] Re: roots
  • From: "Alan Mason" <amason2 at austin.rr.com>
  • Date: Fri, 29 Jun 2001 01:36:25 -0400 (EDT)
  • References: <9gevki$7k8$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Here is another way to accomplish what you want.  Example of usage:
ZerosAll[cos[x] + 0.05 x, x, -30, 30, 0.08], where mesh size = 0.08 here is
arbitrary.  Make changes to taste.  The Zeros routine uses the so-called
"Illinois algorithm" and works quite well.

Code (uses procedural program style) follows:



Zeros[expr_, var_,range_List, prec_: 16, eps_:10^-12] :=
  Module[{x0, x1, y0, y1,xold, yold,x, y, h, f,func, ran = range}, i=0;
    f[arg_] := N[expr //. var->arg, prec];
    x0 = ran[[1]]; x1 = ran[[2]];
   y0 = f[x0]; y1 = f[x1];
    If[y0 y1 > 0, Print["y0 is ", y0];
                                     Print["y1 is ", y1];
    Print["Try another initial range"];  Return[]];
    While[Abs[x0-x1] > eps,i++;
      If[i > 69,Print["MaxIter = 70 Exceeded"]; Return[]];
                        If[y0 y1 <= 0, h = -y1 (x1-x0)/(y1-y0);
                                              xold = x0;x0=x1;     yold =
y0;
        y0=y1;x1+= h;y1=f[x1],
         While[y0 y1 > 0, i++;
          If[i > 69,Print["MaxIter = 70 Exceeded"]; Return[]];
                                    yold=yold/2;
          h=-y1 (x1-xold)/(y1-yold);
                                    x0 = x1;x1+=h; y=f[x1]; y0=y1; y1=y]]
  ];
   {N[x0, prec], N[y0, prec]}]




ZerosAll[expr_, var_,  lower_, upper_, mesh_, prec_:16, eps_:10^-12] :=
  Module[{f, sgn=1, i,L={}, n = Floor[(upper-lower)/mesh]},
  f[arg_] := N[expr //. var->arg, prec];
   For[i=0, i< n, i++,sgn =Sign[f[lower + i* mesh] f[lower + (i+1)*mesh]];
     If[sgn < 0,
        L =  Append[L,
            Zeros[expr, var, {lower+i*mesh, lower+(i+1)*mesh}, prec, eps]]];
   ];
Return[L];
  ]


roots = First[#]& /@ L;
errors = Last[#]& /@ L;

<maarten.vanderburgt at icos.be> wrote in message
news:9gevki$7k8$1 at smc.vnet.net...
> hallo,
>
> 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.
>
> thanks for your help
>
> Maarten van der Burgt
> Leuven
>
>
>
>
>



  • Prev by Date: Re: Exponential Equations
  • Next by Date: Re: Exponential Equations
  • Previous by thread: Re: roots
  • Next by thread: Re: Can Mathematica evaluate step by step