MathGroup Archive 2007

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

Search the Archive

Re: problem using Ersek's RootSearch

  • To: mathgroup at
  • Subject: [mg80246] Re: problem using Ersek's RootSearch
  • From: Bill Rowe <readnewsciv at>
  • Date: Wed, 15 Aug 2007 04:27:10 -0400 (EDT)

On 8/14/07 at 6:55 AM, pystab at (Tim Birks) wrote:

>I define a function f[x] in one of a number of ways, all of which
>are mathematically the same, and then plot the function and (attempt
>to) find the roots with Plot[f[x], {x, -5, 5}] RootSearch[f[x] == 0,
>{x, -5, 5}]

>So, here are two definitions of f[x] that fail:

>f[x_] := Module[{p},
>p = x^2;
>Return[If[x > 0., Cos[p], Cos[p]]]

This code is needlessly complex. There is no need for the local
variable p nor a need for Module. I assume this is a toy example
since the result of this code is will be no different than


Assuming you are trying to define a piecewise continuous
function to work with RootSearch or other Mathematica commands,
probably the best choice is to use the command Piecewise which
seems to work fine with RootSearch. That is:

In[10]:= << Enhancements`RootSearch`;
          f = Piecewise[{{Cos[x^2], x > 0}, {Cos[x^2], x <= 0}}];

In[12]:= RootSearch[f == 0, {x, -5, 5}]

Out[12]= {{x->-4.85406},{x->-4.51889},{x->-4.15677},{x->-3.75994},{x->\

Note, I specifically use Set and not SetDelayed since this will
be more efficient in this case. In general you don't want to use
SetDelayed whenever the resulting expression has to be
repeatedly evaluated in a numerical operation such as FindRoot
or RootSearch.
To reply via email subtract one hundred and four

  • Prev by Date: Re: TraditionalForm ordering
  • Next by Date: Re: Integrated Data Sources
  • Previous by thread: problem using Ersek's RootSearch
  • Next by thread: Re: problem using Ersek's RootSearch