This is the mail archive of the gsl-discuss@sources.redhat.com mailing list for the GSL project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: all the roots of an equation


On Tue, Jun 22, 2004 at 03:30:34PM -0400, Rajarshi Guha wrote:
> Hi,
>   I have an equation which consists of a trigonometric series and I need
> to find all the roots of this equation within a given range (usually -Pi
> to Pi). Looking through the GSL manual I see that it says the root
> finding functions will only find one root at a time.
> 
> Has anybody used GSL to evaluate all the roots of a given function?
> I know Mathematica does this, but I can't seem to find a description of
> the algorithm used for this task. However, what I really need is a C (or
> somethign accessible from C) routine that would allow me to do something
> like this. If this is not possible via GSL could anybody provide some
> pointers as to how I could achieve it?

Well, hate to state the obvious, but ... if you don't have a-priori 
knowledge of the interval a root could occur in, then you might not 
find all the roots.  (True even in mathematica).   There's no magic 
here.  Thats just how it is.

Suppose your algo is:

for (x=-pi; x < pi; x+= 0.1) {
  if (f(x) * f(x+0.1) < 0.0) {
     z = gsl_find_just_one_plain_root_in_an_interval (f, x, x+0.1);
     printf ("a root was found at %f\n", z);
   }
}

Well, if you have two roots closer than 0.1 apart, this algo will miss 
them.  Sure, a 'smarter' algo will help find most roots for most
textbook examples, but there will always be pathological cases that
will be missed.  If you don't really care, then implement the above
trivial algorithm with a step size of 0.0001 and go home happy :)  

--linas


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]