MathGroup Archive 2011

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

Search the Archive

Re: FindRoot repeatedly evaluating function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121107] Re: FindRoot repeatedly evaluating function
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Sun, 28 Aug 2011 04:06:23 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201108251105.HAA24905@smc.vnet.net>
  • Reply-to: drmajorbob at yahoo.com

Storing the result that way is called "memoization" or sometimes "dynamic  
programming". (I don't know why it's not called "memorization", since  
that's exactly what it is.)

If it makes things faster, do it. If it doesn't, don't.

I don't know why it would call ONLY twice near the same value if you do  
that but three times if you don't; possibly because two of the values are  
the same to machine precision. That may lead to a less-exact estimate of  
the derivative.

Bobby

On Fri, 26 Aug 2011 04:00:14 -0500, Simon Pearce  
<Simon.Pearce at nottingham.ac.uk> wrote:

> Hi Bobby,
>
> Thanks for your quick response. Why then if I store the result using  
> f[a_]:=f[a] does it only do it twice? The first two results are being  
> done at the same value (at least to machine precision I assume), with a  
> third being done very slightly away.
>
> My actual problem is not amenable to symbolic solving.
>
> Thanks,
> Simon
>
> -----Original Message-----
> From: DrMajorBob [mailto:btreat1 at austin.rr.com]
> Sent: 25 August 2011 18:10
> To: mathgroup at smc.vnet.net; Simon Pearce
> Subject: Re: FindRoot repeatedly evaluating function
>
> FindRoot makes several evaluations near each guess to estimate the
> derivative numerically, and uses that to compute another guess. There's
> nothing odd or surprising about it.
>
> To avoid all that (where possible), use symbolic solvers instead:
>
> Solve[x[10] == 0 /.
>    First@DSolve[{x''[S] - x'[S] + x[S] == 0, x[0] == 1, x'[0] == a}, x,
>       S], a]
>
> {{a -> (Csc[5 Sqrt[3]] (-3 Cos[5 Sqrt[3]] + Sqrt[3] Sin[5 Sqrt[3]]))/(
>     2 Sqrt[3])}}
>
> Bobby
>
> On Thu, 25 Aug 2011 06:05:19 -0500, Simon Pearce
> <Simon.Pearce at nottingham.ac.uk> wrote:
>
>> Hi Mathgroup,
>>
>> When I use FindRoot[f[y],y] I find that the inner function f is
>> evaluated 3 or 4 times at each value of y (or at least very similar
>> values), even if y is far from the root. This has obvious implications
>> to the speed of my code.
>> Can anyone explain why this is the case, and tell me any way to stop it
>> from repeatedly evaluating f? If I use f[a]:=f[a]=... then it uses the
>> stored result, but I don't want to store thousands of such real valued
>> expressions.
>>
>> The following simple code shows the essence of the problem, using Print
>> to show where the function is evaluated and its value there.
>>
>> f[a_?NumericQ]:=Module[{sol},
>>   sol=NDSolve[{x''[S]-x'[S]+x[S]==0,x[0]==1,x'[0]==a},x,{S,0,10}][[1]];
>>   Print[{a,x[10]/.sol}]; x[10]/.sol ]
>> FindRoot[f[y],{y,6}]
>>
>> Thanks,
>> Simon Pearce
>> Postdoctoral Researcher
>> The Centre for Plant Integrative Biology
>> School of Biosciences
>> University of Nottingham
>>
>>
>> This message and any attachment are intended solely for the addressee
>> and may contain confidential information. If you have received this
>> message in error, please send it back to me, and immediately delete
>> it.   Please do not use, copy or disclose the information contained in
>> this message or in any attachment.  Any views or opinions expressed by
>> the author of this email do not necessarily reflect the views of the
>> University of Nottingham.  This message has been checked for viruses but
>> the contents of an attachment may still contain software viruses which
>> could damage your computer system: you are advised to perform your own
>> checks. Email communications with the University of Nottingham may be
>> monitored as permitted by UK legislation.
>
>


-- 
DrMajorBob at yahoo.com




  • Prev by Date: Re: CDF files now on Gyre&Gimble (math blog)
  • Next by Date: Re: FindRoot repeatedly evaluating function
  • Previous by thread: Re: FindRoot repeatedly evaluating function
  • Next by thread: Re: FindRoot repeatedly evaluating function