Re: Why doesn't this work?

*To*: mathgroup at smc.vnet.net*Subject*: [mg20360] Re: [mg20352] Why doesn't this work?*From*: David Withoff <withoff at wolfram.com>*Date*: Sun, 17 Oct 1999 02:45:36 -0400*Sender*: owner-wri-mathgroup at wolfram.com

> I'm trying to understand why a temporary is needed in the following > case. I've got a function of one parameter and two constants. I > define the constants on the fly with "/.". I want to find a root of > this equation. When I use "/." in FindRoot, it doesn't work, but if I > create another function that has these as true constants (rather than > symbols), FindRoot works. I don't understand why these two cases are > different. > > Here's my function (a difference of gaussians): > > Dog[t_] := (t/tau1)^6 * Exp[-6 * (t/tau1 - 1)] - (t/tau2)^6 * Exp[-6 * > (t/tau2 - 1)] > > Here's my first attempt to find a root, and the result: > > FindRoot[(Dog[t]==0) /. { tau1->13.7, tau2->24.8}, {t, 20}] > FindRoot::frnum: Function {False} is not a length 1 list of numbers at > {t} = {20.}. > > Here, I define a temporary function, and a FindRoot which should come > out to the exact same thing as above -- but this one works: > > Dog2[t_] = Dog[t] /. { tau1->13.7, tau2->24.8} > FindRoot[Dog2[t]==0, {t,20}] > {t->18.1648} > > Why is this? I've looked at it every way I can think of, thrown in > extra parens for grouping, checked that Dog2[t]==0 is exactly the same > as (Dog[t]==0) /. { tau1->13.7, tau2->24.8}, etc., but I can't figure > it out. Any clues? > > Thanks, > -- Joe > > ,------------------------------------------------------------------. > | Joseph J. Strout Biocomputing -- The Salk Institute | > | joe at strout.net http://www.strout.net | > `------------------------------------------------------------------' > Check out the Mac Web Directory! http://www.strout.net/macweb.cgi The first argument in FindRoot is expected to be an equation (an expression of the form lhs==rhs): In[1]:= ?FindRoot FindRoot[lhs==rhs, {x, x0}] searches for a numerical solution to the equation lhs==rhs, starting with x=x0. The expression (Dog[t]==0) /. { tau1->13.7, tau2->24.8} is not an equation. It contains an equation, and if it is evaluated in a particular way (before FindRoot assigns a numerical value to t) it will evaluate to an equation, but that's not quite good enough. In most cases the best solution is to follow the documentation and enter something that is actually an equation, as in your second example. Another possibility is to force the evaluation to be done in a way that leads to an equation, such as In[2]:= Dog[t_] := (t/tau1)^6 Exp[-6 (t/tau1 - 1)] - (t/tau2)^6 Exp[-6 (t/tau2 - 1)] In[3]:= FindRoot[Evaluate[(Dog[t]==0) /. { tau1->13.7, tau2->24.8}], {t, 20}] Out[3]= {t -> 18.1648} or In[4]:= With[{eq = (Dog[t]==0) /. { tau1->13.7, tau2->24.8}}, FindRoot[eq, {t, 20}]] Out[4]= {t -> 18.1648} Dave Withoff Wolfram Research