Re: problem using Ersek's RootSearch
- To: mathgroup at smc.vnet.net
- Subject: [mg80294] Re: problem using Ersek's RootSearch
- From: "Tim Birks" <pystab at hotmail.com>
- Date: Thu, 16 Aug 2007 07:25:31 -0400 (EDT)
- Organization: School of Physics, University of Bath, UK
- References: <f9uegi$bci$1@smc.vnet.net>
"Bill Rowe" <readnewsciv at sbcglobal.net> wrote in message news:f9uegi$bci$1 at smc.vnet.net... > On 8/14/07 at 6:55 AM, pystab at hotmail.com (Tim Birks) wrote: >>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] Indeed, this was a cut-down version of something more complicated. I guess you missed the part where I said so. > 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}} Your suggestion also works well for the functions I'm really interested in, so many thanks for a practical solution to my problem! However, as is often my experience with Mathematica, I now have a working method without really understanding why it works and why the previous attempt doesn't. What's wrong with defining a function using If? Is the reason obvious to those in the know, or is it hocus pocus? T.