MathGroup Archive 2003

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

Search the Archive

Re: FindRoot in 5.0

  • To: mathgroup at smc.vnet.net
  • Subject: [mg42843] Re: FindRoot in 5.0
  • From: Paul Abbott <paul at physics.uwa.edu.au>
  • Date: Wed, 30 Jul 2003 19:30:40 -0400 (EDT)
  • Organization: The University of Western Australia
  • References: <bg7vqq$hj5$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

In article <bg7vqq$hj5$1 at smc.vnet.net>,
 emm10 at psu.edu (Eric Mockensturm) wrote:

> I realize that FindRoot has changed but there seems to be a
> fundamental difference in it between pre-5.0 versions and 5.0 that
> breaks most of my notebooks.  Maybe I've been following bad
> 'programming' practices all these years, but....
> 
> Anyway, a simple illustration follows.
> 
> In 4.2:
> 
> In[7]:=func[x_]:=(y=x;NIntegrate[BesselY[1/2,s],{s,1/2,y}])
> In[8]:=FindRoot[(Print[x];func[x]),{x,2,4}]
> Out[8]={x\[Rule]3.04729}
> 
> In 5.0:
> 
> In[1]:=func[x_]:=(y=x;NIntegrate[BesselY[1/2,s],{s,1/2,y}])
> In[2]:=FindRoot[(Print[x];func[x]),{x,2,4}]
> Out[2]={x\[Rule]4.}
> 
> with errors...
> 
> NIntegrate::nlim: s = x is not a valid limit of integration
> 
> It seems that 5.0 is trying to evaluate func[x] symbolically before
> start to search for a root.  How do I make a global change to this
> behavior.  Does it have something to do with the HoldAll attribute? 
> FindRoot was been HoldAll in 4.2, too.

The following works in both 4.2 and 5.0:

 func[x_?NumericQ]:=(Print[x]; NIntegrate[BesselY[1/2, s], {s, 1/2, x}])

 FindRoot[func[x], {x, 2, 4}]

In 5.0 you could instead use 

 func[x_?NumericQ] := (Sow[x]; NIntegrate[BesselY[1/2, s], {s, 1/2, x}])

 Reap[FindRoot[func[x], {x, 2, 4}]]

to track the convergence. However, this does not print out results as 
FindRoot runs. A neat alternative (from The Mathematica Journal 7(3)) is 
to use ShowStatus:

 ShowStatus[status_]:= FrontEndExecute[SetNotebookStatusLine[
   FrontEnd`EvaluationNotebook[], ToString[status]]]

and then print the intermediate values to the bottom left-hand corner of 
the evaluation Notebook:

 func[x_?NumericQ]:=(ShowStatus[x];NIntegrate[BesselY[1/2, s],{s,1/2,x}])

 FindRoot[func[x], {x, 2, 4}]

Cheers,
Paul

-- 
Paul Abbott                                   Phone: +61 8 9380 2734
School of Physics, M013                         Fax: +61 8 9380 1014
The University of Western Australia      (CRICOS Provider No 00126G)         
35 Stirling Highway
Crawley WA 6009                      mailto:paul at physics.uwa.edu.au 
AUSTRALIA                            http://physics.uwa.edu.au/~paul


  • Prev by Date: Re: FindRoot in 5.0
  • Next by Date: RE: color of an implicit plot
  • Previous by thread: Re: FindRoot in 5.0
  • Next by thread: Re: FindRoot in 5.0