problem using Ersek's RootSearch
- To: mathgroup at smc.vnet.net
- Subject: [mg80159] problem using Ersek's RootSearch
- From: "Tim Birks" <pystab at hotmail.com>
- Date: Tue, 14 Aug 2007 06:55:33 -0400 (EDT)
- Organization: School of Physics, University of Bath, UK
I have downloaded the RootSearch package by Ted Ersek from MathSource http://library.wolfram.com/infocenter/MathSource/4482/ and it looks great. I could use it to greatly improve much of what I do with Mathematica. Unfortunately, it doesn't work consistently for reasons that seem to have nothing to do with the quality of the algorithm. Here's an example, following installation of the package and starting my notebook with Needs["Ersek`RootSearch`"] 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]]] ]; (I shan't attempt to transcribe the error message but the output is $Failed) and f[x_] := Module[{p}, p = x; Return[If[x > 0., Cos[p^2], Cos[p^2]]] ]; (the output is an empty list), and another two that succeed, giving a list of 16 replacement rules for x as expected (and thus showing that the RootSearch algorithm isn't the problem): f[x_] := Module[{p}, p = x; Return[If[x > 0., Cos[x^2], Cos[x^2]]] ]; and f[x_] := Module[{p}, p = x^2; Return[Cos[p]] ]; In all 4 cases the function plots OK. Replacing RootSearch with FindRoot in all 4 cases returns a single solution without reporting an error. I'm running version 6.0.1.0 in Windows XP. I really don't see the pattern in which cases works and which doesn't, other than a general tendency to fall over the more complications I put into the definition. I know that if I really just needed the roots of Cos[x^2] I could use much better-written functions and the use of p is redundant, but I'm using the above definitions to represent the more complicated functions that I really am interested in. Any clues as to what's happening and how it can be fixed? (Thanks BTW to all who have answered my previous questions, your comments have been very useful.) T