Re: A NDSolve within a FindMinimum/NMinimize

*To*: mathgroup at smc.vnet.net*Subject*: [mg90624] Re: [mg90588] A NDSolve within a FindMinimum/NMinimize*From*: DrMajorBob <drmajorbob at att.net>*Date*: Wed, 16 Jul 2008 06:30:39 -0400 (EDT)*References*: <22736969.1216118933296.JavaMail.root@m08>*Reply-to*: drmajorbob at longhorns.com

For instance, f[a_?NumericQ] := ell[1] /. First@NDSolve[{ell'[x] + a Sin[x] ell[x] == 0, ell[0] == a}, ell, {x, -1, 1}] Table[{a, f[a]}, {a, -1, 1, 0.1}] {{-1., -1.5836}, {-0.9, -1.3612}, {-0.8, -1.15559}, {-0.7, \ -0.965715}, {-0.6, -0.790565}, {-0.5, -0.629205}, {-0.4, -0.480748}, \ {-0.3, -0.344361}, {-0.2, -0.21926}, {-0.1, -0.104704}, {0., 0.}, {0.1, 0.0955071}, {0.2, 0.182432}, {0.3, 0.261353}, {0.4, 0.332815}, {0.5, 0.397327}, {0.6, 0.45537}, {0.7, 0.507396}, {0.8, 0.553828}, {0.9, 0.595063}, {1., 0.631475}} FindMinimum[{f@a, -1 < a < 1}, {a, 0}] // Timing {0.062261, {-1.5836, {a -> -1.}}} NMinimize[{f@a, -1 < a < 1}, {a}] // Timing {3.08797, {-1.5836, {a -> -1.}}} Some errors (in your code) resulted from using the same variable x for both NDSolve and FindMinimum. I used x for NDSolve, but a for FindMinimum. Others were caused by the fact that f[a] HAS no minimum without constraints on a. Plot[f@a, {a, -50, 50}] Note thet FindMinimum was much faster than NMinimize, probably because NMinimize uses more complicated methods for this problem. Speed can be increased as follows: g = FunctionInterpolation[f@a, {a, -1, 1}]; NMinimize[{g@a, -1 < a < 1}, {a}] // Timing {0.129609, {-1.5836, {a -> -1.}}} Bobby On Tue, 15 Jul 2008 05:15:49 -0500, <philip.ingrey at googlemail.com> wrote: > I'm trying to find the value of a coefficient that minimizes a > function. Below is a simplified version of the problem: > Using a Table to print out some results is fine: > > Table[{a, s = NDSolve[{El'[x] + a Sin[x] El[x] == 0, El[0] == a}, El, > {x, -1, 1}]; ((El[1] /. s) a)[[1]]}, {a, -1, 1, 0.1}] > > But a NMinimize or FindMinimum gives errors: > > NMinimize[s = NDSolve[{El'[x] + a Sin[x] El[x] == 0, El[0] == a}, El, > {x, -1, 1}]; (El[1] /. s) a, a] > > As it seams to try and evaluate the NDSolve then apply a value for a, > is there a way to solve this? > > Thanks in advance, > Phil I > > -- DrMajorBob at longhorns.com