Re: Why doesn't this work?
- To: mathgroup at smc.vnet.net
- Subject: [mg20376] Re: Why doesn't this work?
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Sun, 17 Oct 1999 02:45:47 -0400
- References: <7u9098$qg5@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Joe, FindRoot has the attribute HoldAll, so, usually, the evaluation of FindRoot[ body, {t,n}] begins by matching the unevaluated body to the first possible one of the forms {lhs==rhs},{expr},lhs == rhs, expr. Whichever one matches it will then proceed on the assumption that lhs-rhs, respectively expr, will give a numerical value when t is assigned a numerical value. In your case the match of (Dog[t]==0)/.{tau1->13.7,tau2->24.8} is with {expr} (it is not an equation), so FindRoot assumes that, for example, Dog[t]==0)/.{tau1->13.7,tau2->24 will give a numerical value when t is given the value 20 - and it does not - it gives False. We can get round this by, in effect, forcing FindRoot to evaluate body before matching. FindRoot[Evaluate[(Dog[t] == 0) /. {tau1 -> 13.7, tau2 -> 24.8}], {t, 20}] {0.11 Second, {t -> 18.1648}} The correct assumption is also made with the following (note the parentheses). FindRoot[(Dog[t] /. {tau1 -> 13.7, tau2 -> 24.8}) == 0, {t, 20}] {0.11 Second, {t -> 18.1648}} Your method Dog2[t_] = Dog[t] /. {tau1 -> 13.7, tau2 -> 24.8}; FindRoot[Dog2[t] == 0, {t, 20}] {t -> 18.1648} also works because the matching leads to the correct assumption is made: that Dog2[t] will be numeric for numeric t. Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 Joe Strout <joe at strout.net> wrote in message news:7u9098$qg5 at smc.vnet.net... > 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 > > P.S. How do I copy a notebook in plain text format? I tried "Copy As > > Plain Text" (and many others in that submenu) but it always gives me > text befuddled by lots of markup tags. I had to paste the above line > by line, and fix the "->" and "==" symbols which came through as > foreign characters. > > -- > ,------------------------------------------------------------------. > | 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 >