Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: FindRoot cannot find obvious solution

  • To: mathgroup at smc.vnet.net
  • Subject: [mg47867] Re: FindRoot cannot find obvious solution
  • From: ab_def at prontomail.com (Maxim)
  • Date: Thu, 29 Apr 2004 03:05:28 -0400 (EDT)
  • References: <200404270847.EAA18892@smc.vnet.net> <c6o3lc$cd0$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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


  • Prev by Date: Re: bug in IntegerPart ?
  • Next by Date: Re: bug in IntegerPart ?
  • Previous by thread: Re: FindRoot cannot find obvious solution
  • Next by thread: Re: Re: FindRoot cannot find obvious solution