Re: Re: FindRoot cannot find obvious solution

*To*: mathgroup at smc.vnet.net*Subject*: [mg48056] Re: [mg48050] Re: FindRoot cannot find obvious solution*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Sun, 9 May 2004 03:02:30 -0400 (EDT)*References*: <200404270847.EAA18892@smc.vnet.net> <c6o3lc$cd0$1@smc.vnet.net> <c6qags$s56$1@smc.vnet.net> <200405080524.BAA11690@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On 8 May 2004, at 14:24, Maxim wrote: > > Because of this, the safest way is still to use f[x_?NumericQ], even > with "EvaluateNumericalFunctionArgument"->False, to block the > evaluation of f'...and then to hope that it doesn't give some > unexpected effect in combination with Compiled->True (which is the > default setting for all numerical functions that use Compiled). > There seem to be no point in using "EvaluateNumericalFunctionArgument"->False together with f[x_?NumericQ] since using the first option you loose all the performance benefits of the new approach in Mathemtica 5.0 (which you can see in the examples on the Wolfram support page). And secondly: I have not observed any problems with using f[x_?NumericQ] except when one chooses to do something unnatural like: 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] ] Just changing it to Module[ { f}, f[x_?NumericQ] := Cos[x]; FindRoot[f[x], {x, 1, 2}, Compiled -> True, MaxIterations -> 15, StepMonitor :> Print["step: ", {x, f[x]}]] ] makes everything work perfectly. This seems to be the case with almost every "problem" you have reported for as long as I can remember. (To deal with the remaining ones, like why 1`2 == 2 gives True, etc, requires only careful reading of the documentation). Andrzej > The recommendation to simply set "EvaluateNumericalFunctionArgument" > to False ( > http://support.wolfram.com/mathematica/mathematics/numerics/ > nsumerror.html > ) can be a bad idea. The reason is that with the default settings for > FindRoot the jacobian is still pre-evaluated. Hence it may happen that > we are evaluating one function and using the derivative of an entirely > different function: > > In[1]:= > Developer`SetSystemOptions["EvaluateNumericalFunctionArgument" -> > False]; > Module[{f = If[TrueQ[# > 0], #, -#] &}, > FindRoot[f[x] == 2, {x, 1}] > ] > > FindRoot::lstol: The line search decreased the step size to within > tolerance specified by AccuracyGoal and PrecisionGoal but was > unable to find a sufficient decrease in the merit function. > You may need more than MachinePrecision digits of working > precision to meet these tolerances. > > Out[2]= > {x -> 1.} > > One way is to use RuleDelayed for Jacobian: > > In[3]:= > Developer`SetSystemOptions["EvaluateNumericalFunctionArgument" -> > False]; > Module[{f = If[TrueQ[# > 0], #, -#] &}, > FindRoot[f[x] == 2, {x, 1}, Jacobian :> {{f'[x]}}] > ] > > Out[4]= > {x -> 2.} > > This method isn't guaranteed to work either, because the first step in > evaluation of f'[x] is to evaluate f', and the question of how > Mathematica does that is equally unclear: > > In[5]:= > Module[{f, g}, > f = If[TrueQ[# > 0], #, -#] &; > g[x_] := If[TrueQ[x > 0], x, -x]; > {f', g'} > ] > > Out[5]= > {If[TrueQ[#1 > 0], 1, -1] &, -1 &} > > The output for g' agrees with what the documentation says: g' is > evaluated as Evaluate[D[g[#],#]]& (actually the reference for > Derivative says D[f[#]&,{#,n}], which doesn't make any sense. So I > assume it was supposed to mean Evaluate[D[f[#],{#,n}]]&). But f' is > evaluated differently, because there are special rules for the > derivatives of pure functions (or maybe specifically for control > structures like If). > > Because of this, the safest way is still to use f[x_?NumericQ], even > with "EvaluateNumericalFunctionArgument"->False, to block the > evaluation of f'...and then to hope that it doesn't give some > unexpected effect in combination with Compiled->True (which is the > default setting for all numerical functions that use Compiled). > > Maxim Rytin > m.r at prontomail.com > >

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