MathGroup Archive 2007

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

Search the Archive

Re: problem using Ersek's RootSearch

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

On 8/14/07 at 6:55 AM, pystab at hotmail.com (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

f[x_]:=Cos[x^2]

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->\
-3.31596},{x->-2.8025},{x->-2.1708},{x->-1.25331},{x->1.25331},{x->2.\
1708},{x->2.8025},{x->3.31596},{x->3.75994},{x->4.15677},{x->4.51889},\
{x->4.85406}}

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