MathGroup Archive 2008

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

Search the Archive

Re: A NDSolve within a FindMinimum/NMinimize

  • To: mathgroup at smc.vnet.net
  • Subject: [mg90608] Re: A NDSolve within a FindMinimum/NMinimize
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 16 Jul 2008 06:27:38 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <g5htfb$j7t$1@smc.vnet.net>

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?

Contrary to Table[], NMinimize[] has none of the attributes of the Hold* 
family, so it does not prevent the evaluation of its arguments before is 
own evaluation.

     In[1]:= Attributes@{NMinimize, Table}

     Out[1]= {{Protected, ReadProtected}, {HoldAll, Protected}}

One way of fixing this is to write a function that is called only for 
numeric argument and contains your original compound expression, 
possibly with the use of construct such as Module[] to localize variables.

     In[2]:= f[a_?NumericQ] :=
      Module[{s},
       s = NDSolve[{El'[x] + a Sin[x] El[x] == 0, El[0] == a},
         El, {x, -1, 1}];
       First[(El[1] /. s) a]
      ]

     NMinimize[f[a], a]
     % // Chop

     Out[3]= {5.54633*10^-17, {a -> -7.44737*10^-9}}

     Out[4]= {0, {a -> -7.44737*10^-9}}

Regards,
-- Jean-Marc


  • Prev by Date: Solve[] doesn't
  • Next by Date: Re: WorldPlot, WorldRotation... with a twist?
  • Previous by thread: Re: A NDSolve within a FindMinimum/NMinimize
  • Next by thread: Re: A NDSolve within a FindMinimum/NMinimize