Re: Interactive input? and sorting FindRoot list

• To: mathgroup at christensen.cybernetics.net
• Subject: [mg727] Re: Interactive input? and sorting FindRoot list
• From: rubin at msu.edu (Paul A. Rubin)
• Date: 11 Apr 1995 21:18:55 GMT
• Organization: Michigan State University

```In article <3m4ogd\$e47 at news0.cybernetics.net>,
REECE_D <REECE+_D%A1%Electromagnetic_Sciences at mcimail.com> wrote:
->
->   Two questions:
->
->   1)  Is there a way to have a module execute up to a point, ask the
user
->for input and then continue?

Yes.  Input["prompt"] does it.

->   2)  How does one take a list of roots returned from
->   Table[FindRoot[eq, i], {i,x0,x1,di}] and return only the unique roots?
->
->The problem I am encountering is that FindRoot will return two or more
->versions of the same root so Union sees them as distinct while in reality
->they are approximations to the same root.  I've tried N[] and
->SetPrecision[] with no luck.
->

You can specify your own comparison function for the option SameTest in
Union.  For example:

In[1]:=  s = Table[ FindRoot[ x^3 - 2x == 0, {x, i} ], {i, -3, 3} ]
Out[1]=  {{x -> -1.41421}, {x -> -1.41421}, {x -> -1.41421},
{x -> 0.}, {x -> 1.41421}, {x -> 1.41421},  {x -> 1.41421}}
(*note repeated roots*)
In[2]:=  stest[ {Rule[ x_, n_ ]}, {Rule[ y_, m_ ]} ] :=  x === y && n == m
In[3]:=  Union[ s, SameTest -> stest ]
Out[3]=  {{x -> -1.41421}, {x -> 0.}, {x -> 1.41421}} (*repetitions gone*)

->   I am trying to write a module which will calculate a characteristic
->equation, plot it, prompt the user for approximate distance between roots
->and then return the first n roots.  I could eliminate the plot and prompt
->if I knew of a robust method to calculate the first n roots of the
->characteristic equation.

I take it from context that (a) you are only interested in real roots and
(b) Solve[] isn't doing the job for you.  If you have a priori knowledge of
a finite interval containing all roots, you could eliminate the plot/prompt
cycle by doing a grid search of the interval until you find either a sign
change (indicating a root of odd multiplicity) or a function value "near"
zero (indicating a root, possibly of even multiplicity), then applying
FindRoot to that interval to nail a root, then dividing x - root out of the
characteristic polynomial (reducing the degree by one), and repeating until
either the degree gets down to 4 (Solve does quartics exactly, I think) or
you fail to find a sign change before hitting some minimal mesh size.
(BTW, in the mesh search, I would suggest progressively bisecting all
subintervals rather than doing a left-to-right search on the final mesh; I
suspect successive bisection would be quicker, although I wouldn't swear to
it.)  This is not fool-proof:  if two roots are very close together, or
worse yet you have a repeated root with even multiplicity, you could miss
them (it?), and a function value "near" 0 does not guarantee a root nearby.

Paul

**************************************************************************
* Paul A. Rubin                                  Phone: (517) 432-3509   *
* Department of Management                       Fax:   (517) 432-1111   *
* Eli Broad Graduate School of Management        Net:   RUBIN at MSU.EDU    *
* Michigan State University                                              *
* East Lansing, MI  48824-1122  (USA)                                    *
**************************************************************************
Mathematicians are like Frenchmen:  whenever you say something to them,
they translate it into their own language, and at once it is something
entirely different.                                    J. W. v. GOETHE

```

• Prev by Date: Statistical Analysis of Data through Mma Functions:
• Next by Date: [no subject]
• Previous by thread: Interactive input? and sorting FindRoot list
• Next by thread: Explain weird behavior of Solve