Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Find all roots with FindRoot

161 views
Skip to first unread message

Sam Takoy

unread,
Apr 20, 2012, 7:46:01 AM4/20/12
to
Hi,

I need to find all roots of a function in a certain interval. FindRoot
only finds one. I imagine that I could write a script that calls
FindRoot repeatedly, but I'm guessing this must be built-in
functionality or someone has written such a script in the past (better
than I ever could).

Can someone direct me to a command or a script like that?

Many thanks in advance,

Sam

Harvey P. Dale

unread,
Apr 21, 2012, 12:33:21 AM4/21/12
to
Sam:

Ted Ersek's RootSearch does the trick. It's available on the
Wolfram site.

Best,

Harvey

djmpark

unread,
Apr 21, 2012, 12:32:17 AM4/21/12
to
You might try Ted Ersek's RootSearch package. This returns all real roots
within an interval in order. Although this is a common usage, and some
combination of FindRoot, Solve or Reduce will sometimes suffice, there
doesn't seem to be a common Mathematica routine that is as convenient and
easy as Ted's routine. Ted's routine will also find zeros that do not cross
the axis, and I seem to recall it can be used to find roots defined on
curves defined by a single parameter.

http://library.wolfram.com/infocenter/MathSource/4482/


David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/index.html




From: Sam Takoy [mailto:sam....@yahoo.com]


Ted Sariyski

unread,
Apr 24, 2012, 5:34:39 AM4/24/12
to
Hi,
I have a data structure e.g. {a,{b,c},{d,{e,f},{g}}}, which I want to
check for NumericQ. For vectors and matrices I can use
VectorQ[x,NumericQ] or MatrixQ[x,NumericQ]. Is there a way to check an
arbitrary data structure that all elements are NumericQ or I have to
construct a function for each data structure?
Thanks in advance,
--Ted


W Craig Carter

unread,
Apr 25, 2012, 12:31:19 AM4/25/12
to
Hello All,
Regarding Ted Ersek's very useful RootSearch package: to get this to run in Mathematica 8, I had to change all instances of
$MinPrecision=-Infinity
to
$MinPrecision=Infinity

in RootSearch.m

W Craig Carter
Professor of Materials Science, MIT

djmpark

unread,
Apr 25, 2012, 12:38:28 AM4/25/12
to
data1 = {2, {Pi, 0.5}, {1/2, {I, 3 Sin[2]}, {6}}};
data2 = {a, {Pi, 0.5}, {1/2, {I, 3 Sin[2]}, {6}}};

VectorQ[Flatten[data1], NumericQ]
VectorQ[Flatten[data2], NumericQ]
True
False

Bob Hanlon

unread,
Apr 25, 2012, 12:41:08 AM4/25/12
to
myNumericQ[data_] := VectorQ[Flatten[data], NumericQ]

data = {a, {b, c}, {d, {e, f}, {g}}};

sub = Thread[{a, b, c, d, e, f, g} -> Hold[RandomInteger[{0, 9}]]] //
ReleaseHold

{a -> 6, b -> 9, c -> 1, d -> 5, e -> 1, f -> 1, g -> 4}

data // myNumericQ

False

data /. sub // myNumericQ

True


Bob Hanlon

Ted Sariyski

unread,
Apr 26, 2012, 5:29:15 AM4/26/12
to
Hi,

When I define a new function myFun in a package aaa` I define a variable
me="aaa`myFun".

BeginPackage["aaa`"]
...
myFun[]:=Module[{..., me="aaa`myFun"},
...

I wonder Is there an automatic variable, like $0 in perl, which
internally is assigned "aaa`myFun"?

Thanks in advance,
--Ted

Alexei Boulbitch

unread,
Apr 26, 2012, 5:30:47 AM4/26/12
to
Hi,
I have a data structure e.g. {a,{b,c},{d,{e,f},{g}}}, which I want to
check for NumericQ. For vectors and matrices I can use
VectorQ[x,NumericQ] or MatrixQ[x,NumericQ]. Is there a way to check an
arbitrary data structure that all elements are NumericQ or I have to
construct a function for each data structure?
Thanks in advance,
--Ted

Hi, Ted,

You did not specify, what exactly do you need to have as the result. You may want simply
to have
1) True, if all terms are numeric and False if any of them are non-numeric
or
2) you may want say, to preserve the structure of the original list, in which True and False
occupy the places of numeric and non-numeric items.

The first case is simple, since you can flatten the initial list
Try this function:

numericCheck[lst_List] := And @@ NumericQ /@ Flatten[lst];

For example, here are three lists with the structure you have specified:

lst1 = {a, {b, c}, {d, {e, f}, {g}}};
lst2 = {0.136, {0.683, 0.123}, {0.1867, {0.203, 0.493}, {0.5369}}};
lst3 = {0.136, {0.683, 0.123}, {0.1867, {a, 0.493}, {0.5369}}};

Among them only the lst2 is numeric, while lst3 has a variable "a" instead of the number in the position {3,2,1}. Let us check

numericCheck /@ {lst1, lst2, lst3}

{False, True, False}

Have fun, Alexei

Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44

e-mail: alexei.b...@iee.lu




Mikael Anderson

unread,
Apr 26, 2012, 5:31:18 AM4/26/12
to

I just wanted to give the RootSearch package a try but I wonder why
RootSearch misses some roots in the beginning of the solution when I change
the range as below:

>
> In[28]:= RootSearch[(Tan[Sqrt[theta]] - 2*Sqrt[theta]/(1 - theta)) ==
0, {theta, 0, 1000}]

Out[28]= {{theta -> 0.}, {theta -> 5.43413}, {theta ->
35.4046}, {theta -> 84.7952}, {theta -> 153.896}, {theta ->
242.729}, {theta -> 351.298}, {theta -> 479.605}, {theta ->
627.65}, {theta -> 795.435}, {theta -> 982.958}}

In[29]:= RootSearch[(Tan[Sqrt[theta]] - 2*Sqrt[theta]/(1 - theta)) ==
0, {theta, 0, 10000}]

Out[29]= {{theta -> 0.}, {theta -> 84.7952}, {theta ->
153.896}, {theta -> 242.729}, {theta -> 351.298}, {theta ->
479.605}, {theta -> 627.65}, {theta -> 795.435}, {theta ->
982.958}, {theta -> 1190.22}, {theta -> 1417.22}, {theta ->
1663.96}, {theta -> 1930.44}, {theta -> 2216.66}, {theta ->
2522.62}, {theta -> 2848.31}, {theta -> 3193.75}, {theta ->
3558.93}, {theta -> 3943.84}, {theta -> 4348.49}, {theta ->
4772.89}, {theta -> 5217.02}, {theta -> 5680.89}, {theta ->
6164.5}, {theta -> 6667.85}, {theta -> 7190.94}, {theta ->
7733.77}, {theta -> 8296.34}, {theta -> 8878.64}, {theta ->
9480.69}}

/Mikael


Christoph Lhotka

unread,
Apr 26, 2012, 5:25:10 AM4/26/12
to
Hi,

I would like to add an "old"-style way of programming to tackle the
problem:

data1 = {2, {Pi, 0.5}, {1/2, {I, 3 Sin[2]}, {6}}};
data2 = {a, {Pi, 0.5}, {1/2, {I, 3 Sin[2]}, {6}}};

testQ[expr_] := Block[{},
If[
Head[expr] === List,
Do[testQ[expr[[i]]], {i, 1, Length[expr]}],
If[! NumericQ[expr], Throw[False]]];
Throw[True]
]

TestQ[expr_] := Catch[testQ[expr]]


TestQ[data1]
True

TestQ[data2]
False


Note, that TestQ is needed to catch the False thrown by testQ if
!NumericQ returns True.

Best,

Christoph

da...@wolfram.com

unread,
Apr 27, 2012, 6:48:04 AM4/27/12
to
Any idea why this and a prior question are showing up in the thread "Find all roots with FindRoot"? I'm reading from Google Groups, where this threading is making some of these conversations tricky to follow.

[I don't see this in the groups as I get them back - Moderator]

Daniel Lichtblau
Wolfram Research

A Retey

unread,
Apr 27, 2012, 6:50:07 AM4/27/12
to
Hi Ted,
no, not AFAIK. What would you use it for? If you want, you can do
something like:

BeginPackage["aaa`"];

myPublic;

Begin["`Private`"];

myPublic[x_] := myPrivate[x];

(me : myPrivate)[x_] := (Print[me]; x^2);

End[];

EndPackage[];

then evaluating myPublic[5] will show the information you want. Without
knowing what you actually want to achieve I feel that this is probably
not really what you need, though...

hth,

albert

djmpark

unread,
Apr 27, 2012, 6:53:45 AM4/27/12
to
<< Ersek`RootSearch`

RootSearch[(Tan[Sqrt[theta]] - 2*Sqrt[theta]/(1 - theta)) ==
0, {theta, 0, 10000}, InitialSamples -> 3000]

{{theta -> 0.}, {theta -> 5.43413}, {theta -> 35.4046}, {theta ->
84.7952}, {theta -> 153.896}, {theta -> 242.729}, {theta ->
351.298}, {theta -> 479.605}, {theta -> 627.65}, {theta ->
795.435}, {theta -> 982.958}, {theta -> 1190.22}, {theta ->
1417.22}, {theta -> 1663.96}, {theta -> 1930.44}, {theta ->
2216.66}, {theta -> 2522.62}, {theta -> 2848.31}, {theta ->
3193.75}, {theta -> 3558.93}, {theta -> 3943.84}, {theta ->
4348.49}, {theta -> 4772.89}, {theta -> 5217.02}, {theta ->
5680.89}, {theta -> 6164.5}, {theta -> 6667.85}, {theta ->
7190.94}, {theta -> 7733.77}, {theta -> 8296.34}, {theta ->
8878.64}, {theta -> 9480.69}}


Sseziwa Mukasa

unread,
Apr 27, 2012, 6:54:15 AM4/27/12
to
Ted,

It's not necessary in this case, any references to myFun in the Module
are scoped to the package aaa already as long as myFun's declaration is
after the Begin[], which I can't tell since you elided code.

If you are using anonymous functions #0 refers to the function itself
and can be used for recursive anonymous functions.

Regards,
Ssezi

On Apr 26, 2012, at 5:27 AM, Ted Sariyski wrote:

> Hi,
>
> When I define a new function myFun in a package aaa` I define a variable
> me="aaa`myFun".
>
> BeginPackage["aaa`"]
> ...
> myFun[]:=Module[{..., me="aaa`myFun"},
> ...
>
> I wonder Is there an automatic variable, like $0 in perl, which
> internally is assigned "aaa`myFun"?
>
> Thanks in advance,
> --Ted
>


Ted Sariyski

unread,
Apr 27, 2012, 6:54:46 AM4/27/12
to
I missed to mention that I use "me" for primitive debugging .e.g.
Print[me, ...].
--Ted

Sseziwa Mukasa

unread,
Apr 27, 2012, 6:55:16 AM4/27/12
to
So you really just wanted the string? There isn't any automatic
variable I know of that you can use for that purpose.

Andrzej Kozlowski

unread,
Apr 28, 2012, 5:30:15 AM4/28/12
to

On 26 Apr 2012, at 11:28, Mikael Anderson wrote:

>
> I just wanted to give the RootSearch package a try but I wonder why
> RootSearch misses some roots in the beginning of the solution when I change
> the range as below:
>
>>
>> In[28]:= RootSearch[(Tan[Sqrt[theta]] - 2*Sqrt[theta]/(1 - theta)) ==
> 0, {theta, 0, 1000}]
>
> Out[28]= {{theta -> 0.}, {theta -> 5.43413}, {theta ->
> 35.4046}, {theta -> 84.7952}, {theta -> 153.896}, {theta ->
> 242.729}, {theta -> 351.298}, {theta -> 479.605}, {theta ->
> 627.65}, {theta -> 795.435}, {theta -> 982.958}}
>
> In[29]:= RootSearch[(Tan[Sqrt[theta]] - 2*Sqrt[theta]/(1 - theta)) ==
> 0, {theta, 0, 10000}]
>
> Out[29]= {{theta -> 0.}, {theta -> 84.7952}, {theta ->
> 153.896}, {theta -> 242.729}, {theta -> 351.298}, {theta ->
> 479.605}, {theta -> 627.65}, {theta -> 795.435}, {theta ->
> 982.958}, {theta -> 1190.22}, {theta -> 1417.22}, {theta ->
> 1663.96}, {theta -> 1930.44}, {theta -> 2216.66}, {theta ->
> 2522.62}, {theta -> 2848.31}, {theta -> 3193.75}, {theta ->
> 3558.93}, {theta -> 3943.84}, {theta -> 4348.49}, {theta ->
> 4772.89}, {theta -> 5217.02}, {theta -> 5680.89}, {theta ->
> 6164.5}, {theta -> 6667.85}, {theta -> 7190.94}, {theta ->
> 7733.77}, {theta -> 8296.34}, {theta -> 8878.64}, {theta ->
> 9480.69}}
>
> /Mikael
>
>

Is there any reason why you don't want to use Reduce:

In[166]:=
Reduce[(Tan[Sqrt[theta]] - 2*Sqrt[theta]/(1 - theta)) == 0 &&
0 <= theta <= 10000, theta] // N


theta == 0. || theta == 5.43413 || theta == 35.4046 ||
theta == 84.7952 || theta == 153.896 || theta == 242.729 ||
theta == 351.298 || theta == 479.605 || theta == 627.65 ||
theta == 795.435 || theta == 982.958 || theta == 1190.22 ||
theta == 1417.22 || theta == 1663.96 || theta == 1930.44 ||
theta == 2216.66 || theta == 2522.62 || theta == 2848.31 ||
theta == 3193.75 || theta == 3558.93 || theta == 3943.84 ||
theta == 4348.49 || theta == 4772.89 || theta == 5217.02 ||
theta == 5680.89 || theta == 6164.5 || theta == 6667.85 ||
theta == 7190.94 || theta == 7733.77 || theta == 8296.34 ||
theta == 8878.64 || theta == 9480.69

It gives you the complete answer (provably so) without the need to fiddle with any options and is pretty fast too. Why do you need an external package for this sort of thing?

Andrzej Koz=C5=82owski


Andrzej Kozlowski

unread,
Apr 29, 2012, 2:07:37 AM4/29/12
to


On 28 Apr 2012, at 13:02, Mikael Anderson wrote:

> Actually I was using NSolve and was happy with that (see below). Then
I happened to see the post on RootSerach and wanted to test it to see if
I get the same results as NSolve. I got surprised that by increasing the
range I did not get the same answer and hence posted the question.
>
> BTW Andrzej, do you see any advantage in using Reduce compared to
NSolve?
>

In Mathematica version 7 NSolve could only solve algebraic equations and
Reduce could also solve transcendental (analytic) ones as well as
inequalities. In version 8 NSolve can solve the same kind of equations
as Reduce (I think it actually calls on Reduce to solve non-algebraic
equations so you are actually using Reduce) and it even takes the
options WorkingPrecision->Infinity:

NSolve[x^2 - x + 1 == 0, x, WorkingPrecision -> Infinity]
{{x -> (-1)^(1/3)}, {x -> -(-1)^(2/3)}}

The differences, apart from the different form of output concerns
solving inequalities (NSolve can't do that) and equations with symbolic
parameters (NSolve can't do that but Solve can).

The reason why I used Reduced is that I still sometimes forget who much
Solve and NSolve were upgraded in Mathemaitca 8.


Andrzej Kozlowski



Mikael Anderson

unread,
Apr 29, 2012, 2:07:06 AM4/29/12
to

Actually I was using NSolve and was happy with that (see below). Then I
happened to see the post on RootSerach and wanted to test it to see if I
get the same results as NSolve. I got surprised that by increasing the
range I did not get the same answer and hence posted the question.

BTW Andrzej, do you see any advantage in using Reduce compared to NSolve?


In[2]:= NSolve[(Tan[Sqrt[theta]] - 2*Sqrt[theta]/(1 - theta)) == 0 &&
0 <= theta <= 10000, theta]

Out[2]= {{theta -> 0.}, {theta -> 5.43413}, {theta ->
35.4046}, {theta -> 84.7952}, {theta -> 153.896}, {theta ->
242.729}, {theta -> 351.298}, {theta -> 479.605}, {theta ->
627.65}, {theta -> 795.435}, {theta -> 982.958}, {theta ->
1190.22}, {theta -> 1417.22}, {theta -> 1663.96}, {theta ->
1930.44}, {theta -> 2216.66}, {theta -> 2522.62}, {theta ->
2848.31}, {theta -> 3193.75}, {theta -> 3558.93}, {theta ->
3943.84}, {theta -> 4348.49}, {theta -> 4772.89}, {theta ->
5217.02}, {theta -> 5680.89}, {theta -> 6164.5}, {theta ->
6667.85}, {theta -> 7190.94}, {theta -> 7733.77}, {theta ->
8296.34}, {theta -> 8878.64}, {theta -> 9480.69}}

/Mikael.
0 new messages