Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #4561 > unrolled thread

FindRoot repeatedly evaluating function

Started bySimon Pearce <Simon.Pearce@nottingham.ac.uk>
First post2011-08-25 11:08 +0000
Last post2011-08-26 09:33 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  FindRoot repeatedly evaluating function Simon Pearce <Simon.Pearce@nottingham.ac.uk> - 2011-08-25 11:08 +0000
    Re: FindRoot repeatedly evaluating function Peter Pein <petsie@dordos.net> - 2011-08-26 09:33 +0000

#4561 — FindRoot repeatedly evaluating function

FromSimon Pearce <Simon.Pearce@nottingham.ac.uk>
Date2011-08-25 11:08 +0000
SubjectFindRoot repeatedly evaluating function
Message-ID<j35afa$oe3$1@smc.vnet.net>
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.

[toc] | [next] | [standalone]


#4592

FromPeter Pein <petsie@dordos.net>
Date2011-08-26 09:33 +0000
Message-ID<j37pa4$4lq$1@smc.vnet.net>
In reply to#4561
Am 25.08.2011 13:08, schrieb Simon Pearce:
> 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.

Hi!

Sorry, I do not know why FindRoot behaves like this but the following is 
fast enough IMHO:

In[1]:= a0 =a /. First[Solve[ x[10] == 0 /.
           First[DSolve[
             { x''[S] - x'[S] + x[S] == 0, x[0] == 1, x'[0] == a},
           x, S]],
         a]] // FullSimplify

         N[a0,20]

Out[1]= 1/2 (1 - Sqrt[3] Cot[5 Sqrt[3]])
Out[2]= 1.4029569306878377766


Regards,

   Peter

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web