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

using FindRoot to find multiple answers in a domain?

846 views
Skip to first unread message

akil

unread,
Aug 29, 2006, 3:52:09 AM8/29/06
to
I want to find all answers that exist in a certain domain, but FindRoot
gives only the first answer it finds (Using other methods like Solve,Reduce
etc takes too long, so Im stuck with using FindRoot). I give the problem I
have by example, the functions I use are really complex, but by solving the
problem for the example, I could solve t for the complex part.

Example:

Finding all intersections of Cos[x] and Sin[x] in the domain [0,10Pi]

Doing:
FindRoot[Cos[antwoord] == Sin[antwoord], {antwoord, 0, 0, 10Pi}]

FindRoot[Cos[antwoord] == Sin[antwoord], {antwoord, %, %, 10Pi}]

does not work the second FindRoot gives the exact same answer back. So using
this in a loop to find all intersections does not work.

Doing FindRoot[Cos[antwoord] == Sin[antwoord], {antwoord, %+0.01, %+0.01,
10Pi}] instead, also does not work.

Any way to find all intersections in a specified domain?

Akil


Jean-Marc Gulliet

unread,
Aug 30, 2006, 6:53:12 AM8/30/06
to
Hi Akil,

Have a look at Ted Ersek's _RootSearch_ package.

"RootSearch looks for all roots of an equation between xmin and xmax. A
package is provided which defines a function called RootSearch.
RootSearch[lhs==rhs,{x,xmin,xmax}] tries to find all roots in the
specified range. The algorithm is very robust as demonstrated in the
notebook (RootSearchExamples.nb). Options are provided to allow
arbitrary precision convergence to roots. Installation instructions are
provided in the notebook (RootSearchExamples.nb)."

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

Best regards,
Jean-Marc

David Park

unread,
Aug 30, 2006, 7:01:19 AM8/30/06
to
Akil,

Download and install the Ted Ersek RootSearch package from MathSource.
Although it works only on 1D cases, in those cases it is much better and
more convenient than FindRoot.

Needs["Ersek`RootSearch`"]

RootSearch[Cos[antwoord] == Sin[antwoord], {antwoord, 0, 10Pi}]

{{antwoord -> 0.785398}, {antwoord -> 3.92699}, {antwoord ->
7.06858}, {antwoord -> 10.2102}, {antwoord -> 13.3518}, {antwoord ->
16.4934}, {antwoord -> 19.635}, {antwoord -> 22.7765}, {antwoord ->
25.9181}, {antwoord -> 29.0597}}

David Park
dj...@earthlink.net
http://home.earthlink.net/~djmp/

Carl K. Woll

unread,
Aug 30, 2006, 7:07:23 AM8/30/06
to
akil wrote:
> I want to find all answers that exist in a certain domain, but FindRoot
> gives only the first answer it finds (Using other methods like Solve,Reduce
> etc takes too long, so Im stuck with using FindRoot). I give the problem I
> have by example, the functions I use are really complex, but by solving the
> problem for the example, I could solve t for the complex part.
>
> Example:
>
> Finding all intersections of Cos[x] and Sin[x] in the domain [0,10Pi]
>
> Doing:
> FindRoot[Cos[antwoord] == Sin[antwoord], {antwoord, 0, 0, 10Pi}]
>
> FindRoot[Cos[antwoord] == Sin[antwoord], {antwoord, %, %, 10Pi}]
>
> does not work the second FindRoot gives the exact same answer back. So using
> this in a loop to find all intersections does not work.
>
> Doing FindRoot[Cos[antwoord] == Sin[antwoord], {antwoord, %+0.01, %+0.01,
> 10Pi}] instead, also does not work.
>
> Any way to find all intersections in a specified domain?
>
> Akil
>

If the only special functions contained in your equation are
trigonometric and exponential, then you can try interval methods from
the IntervalRoots package. For your example:

Needs["NumericalMath`IntervalRoots`"]

In[2]:=
IntervalNewton[Cos[x]-Sin[x],x,Interval[{0,10Pi}],10^-6,MaxRecursion->10]

Out[2]=
Interval[{0.785398, 0.785398}, {3.92699, 3.92699}, {7.06858, 7.06858},
{10.2102, 10.2102}, {13.3518, 13.3518}, {16.4934, 16.4934}, {19.635,
19.635}, {22.7765, 22.7765}, {25.9181, 25.9181}, {29.0597, 29.0597}]

The nice thing about interval methods is that all roots are guaranteed
to lie in the returned Interval object, although some of the
subintervals may not contain any roots.

Carl Woll
Wolfram Research

akil

unread,
Aug 30, 2006, 7:11:26 AM8/30/06
to
IntervalRoots package and RootSearch do not work for the kind of formulas I
have.

For example in the domain [0,Pi/2] we want to solve (I only used FindRoot
because this was the fastest):

I put the package I test in on
http://home.wanadoo.nl/akomur/testRootSearch.nb so that you can see the
formulas I use.

Copying them In here ruins the formulas.

Akil


dh

unread,
Aug 30, 2006, 7:27:39 AM8/30/06
to

Hello Akil,
finding all the roots af a general function is no trivial task.
The best you can do, is to solve the problem analytically. Try
exploiting analytical properties of your function. If this is out of
question, you may try FindRoot with different starting values. Make a
plot for rough values of the roots. To automate, chose the starting
values on a grid over the given region.
For your example, the plot:
Plot[{Sin[x], Cos[x]}, {x, 0, 10Pi}]
gives you an idea about the roots, starting values for FindRoot. We
expect to find 10 roots.
If you want to use a grid of starting values, you could use:
d= x/. FindRoot[Cos[x] == Sin[x], {x, #}] & /@ Range[0, 10Pi, 0.1]
this gives values outside our region and multiple copies of some roots.
To restrict the values to the region and to get unique values:
d = Select[d, 0 <= # <= 10Pi &];
Union[d, SameTest -> (Abs[#1 - #2] < 10^-6 &)]
now we have the expected 10 values.

Daniel

Paul Abbott

unread,
Sep 4, 2006, 2:45:40 AM9/4/06
to
In article <ed3rou$rv5$1...@smc.vnet.net>, "akil" <ako...@wanadoo.nl>
wrote:

> IntervalRoots package and RootSearch do not work for the kind of formulas I
> have.

Are you sure?

> For example in the domain [0,Pi/2] we want to solve (I only used FindRoot
> because this was the fastest):
>
> I put the package I test in on
> http://home.wanadoo.nl/akomur/testRootSearch.nb so that you can see the
> formulas I use.
>
> Copying them In here ruins the formulas.

Copy as Plain text. Your first formula is

nlv = Max[-81.24275115593154 Cot[beta] - 48.489352280270914,
Min[-46.844746272761526 Cot[beta] - 71.4213555357176,
-46.480384751324436 Cot[beta] - 71.66426321667566]]

Note that PiecewiseExpand can be applied to such formulae. If you
simplify the result

Simplify[PiecewiseExpand[nlv]]

you deduce that nlv is just

-81.24275115593154 Cot[beta] - 48.489352280270914 if Cot[beta] <= 2/3

-46.844746272761526 Cot[beta] - 71.4213555357176 if Cot[beta] >= 2/3

You can check that these values are consistent when Cot[beta] == 2/3.
Moreover, the catch-all condition

-46.480384751324436 Cot[beta] - 71.66426321667566

is redundant for real beta since it only applies when Cot[beta] == 2/3.
A brief examination of nuvb shows that it involves ratios of (powers of)
expressions similar to nlv. I expect that simplification of these
expressions prior to constructing nuvb would lead to a final form that
is not that hard to handle.

I expect that if you step back and tell us more about the original
problem, then a more elegant approach would present itself.

Cheers,
Paul

_______________________________________________________________________
Paul Abbott Phone: 61 8 6488 2734
School of Physics, M013 Fax: +61 8 6488 1014
The University of Western Australia (CRICOS Provider No 00126G)
AUSTRALIA http://physics.uwa.edu.au/~paul

0 new messages