Re: FindRoot repeatedly evaluating function

• To: mathgroup at smc.vnet.net
• Subject: [mg121108] Re: FindRoot repeatedly evaluating function
• From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
• Date: Sat, 27 Aug 2011 08:19:07 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• References: <201108251105.HAA24905@smc.vnet.net>

```On Thu, 25 Aug 2011, Simon Pearce 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.

Simon, here is the answer to the remaining question:

This is the case because the Jacobian and FindRoot both need to evaluate
at those points

ClearAll[f]
With[{eqns = seqns, vars = svars}, Clear[f, J, ysol];
f[y_?NumericQ] := Part[ysol[y], 1];
J[y_?NumericQ] := (Print["Jackpot"]; {{Part[ysol[y], 2]}});
ysol[y_?NumericQ] := Module[{sol, yvars}, yvars = vars /. p -> y;
sol = First[NDSolve[eqns /. p -> y, yvars, {S, 10, 10}]];
sol = (yvars /. sol) /. S -> 10;
Print[InputForm[{y, sol}]];
sol]]

s = 0; e = 0; j = 0;
FindRoot[f[y], {y, 6}, Jacobian -> {J[y], EvaluationMonitor :> j++}
, StepMonitor :> s++, EvaluationMonitor :> e++]

but j == 3; this is why the caching is useful.

Oliver

>
> 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.
>

```

• Prev by Date: Re: Incompletely simplified Square root.
• Next by Date: Re: Just another Mathematica "Gotcha", and HoldForm bug
• Previous by thread: Re: FindRoot repeatedly evaluating function
• Next by thread: Re: FindRoot repeatedly evaluating function