RE: Behaviour of FindRoot
- To: mathgroup at smc.vnet.net
- Subject: [mg65147] RE: [mg65096] Behaviour of FindRoot
- From: Hartmut.Wolf at t-systems.com
- Date: Wed, 15 Mar 2006 06:30:44 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
> -----Original Message----- > From: dh [mailto:dh at metrohm.ch] To: mathgroup at smc.vnet.net > Subject: [mg65147] [mg65096] Behaviour of FindRoot > > Hi, > in a previous thread: "optimization nested in root-finding" the > following as yet unanswered problem poped up (simplified): > > Remove[f1]; > f1[x1_] := (Print["x1=",x1]; > x1^2 > ); > FindRoot[f1[x] == 2., {x, 1}] > > This will print "x1=x" showing that f1 was called with an > unevaluated x, > despite FindRoot having the Attribute "HoldAll". There is no other > output from the Print statement > > However, if we replace f1[x1_] by f1[x1_Real] > we get the expected: > > x1=1. > ... > > I think this has to do with compilation. Can anybody explain this? > > Daniel > Daniel, not with compilation, but with use of FindRoot itself, look: In[30]:= lhs = x^3; In[31]:= rhs = 2x; In[32]:= FindRoot[lhs == rhs, {x, 1}] Out[32]= {x -> 1.41421} FindRoot will first do a symbolic evaluation of the expression (the equation), and such try transformation rules (which might simplify the expression considerably). Such, with symbolic x, evaluation of f1[x] gives In[21]:= f1[x] From In[21]:= "x1="x Out[21]= x^2 so no printing thing goes to the FindRoot machinery. In contrast In[34]:= Remove[f1] In[35]:= f1[x1_Real] := (Print["x1=", x1]; x1^2); In[36]:= f1[x] Out[36]= f1[x] as symbolic x isn't Real, doesn't transform your expression (smuggling your printing spy) -- Hartmut