Re: Re: FindRoot cannot find obvious solution

*To*: mathgroup at smc.vnet.net*Subject*: [mg47896] Re: [mg47867] Re: FindRoot cannot find obvious solution*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Thu, 29 Apr 2004 19:39:43 -0400 (EDT)*References*: <200404270847.EAA18892@smc.vnet.net> <c6o3lc$cd0$1@smc.vnet.net> <200404290705.DAA28269@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On 29 Apr 2004, at 16:05, Maxim wrote: > I think that with all these nice workarounds it went unnoticed that > there *was* something wrong with Mukhtar's example. Consider: > > In[1]:= > Module[ > {y = Cos[x], f}, > f[x_] := y; > FindRoot[f[x], {x, 1, 2}, > Compiled -> True, MaxIterations -> 15, > StepMonitor :> Print["step: ", {x, f[x]}]] > ] > > Out[1]= > {x -> 1.5708} > > In[2]:= > Module[ > {y = Cos[x], f}, > f[x_?NumericQ] := y; > FindRoot[f[x], {x, 1, 2}, > Compiled -> True, MaxIterations -> 15, > StepMonitor :> Print["step: ", {x, f[x]}], > EvaluationMonitor -> 0] > ] > > > FindRoot::cvmit: Failed to converge to the requested > accuracy or precision within 15 iterations. > > Out[2]= > {x -> 2.} > > This is similar to the issue that was discussed at > > http://forums.wolfram.com/mathgroup/archive/2003/Dec/msg00401.html > > Just as with the Plot examples, f[1] here is not numerical because x > in Cos[x] will remain unbound. But if we evaluate Block[{x=1}, f[x]], > then the result will be numerical, and this is how FindRoot works. And > then there's the question of how it will interact with Compiled->True. > > But here we have a new twist, because, according to Trace, FindRoot > does obtain numerical values evaluating f[x] in both cases, so it's > not clear what happens in the second example, when we use > f[x_?NumericQ]. > > Oh, and EvaluationMonitor/StepMonitor won't help us here, because they > perform true delayed evaluation, while what FindRoot seems to do is to > evaluate the first argument once and then to perform all computations > on what it obtains (why does it have HoldAll attribute then? Simple: > to add extra confusion if FindRoot is used inside Module or With and > there is a name conflict). As an additional bonus, if you omit > EvaluationMonitor in the second example, then StepMonitor won't print > anything either. Lovely. > > Maxim Rytin > m.r at prontomail.com > > > I really fail to see what point are you trying to make? Certainly, your use of Module has nothing to do with the problem which is entirely due to the setting Compile->True. If you set Compile->False, which is only natural since the function defined in this way obviously can't be compiled, then everything, including StepMonitor works exactly as it should. Why on earth you keep insisting that Mathematica should behave correctly when you give her nonsensical input to work on? y = Cos[x]; f[(z_)?NumericQ] := y; FindRoot[f[x], {x, 1, 2}, Compiled -> False, MaxIterations -> 15, StepMonitor :> Print["step: ", {x, f[x]}]] "step: "{2., -0.4161468365471424} "step: "{1.5649043758915777, 0.005891916813448233} "step: "{1.570978574535018, -0.00018224773911250164} "step: "{1.570796325773051, 1.0218455583390503*^-9} "step: "{1.5707963267948966, 6.123233995736766*^-17} {x -> 1.5707963267948966} Andrzej Kozlowski Chiba, Japan http://www.mimuw.edu.pl/~akoz/

**References**:**FindRoot cannot find obvious solution***From:*"Mukhtar Bekkali" <mbekkali@iastate.edu>

**Re: FindRoot cannot find obvious solution***From:*ab_def@prontomail.com (Maxim)