Re: A NDSolve within a FindMinimum/NMinimize

*To*: mathgroup at smc.vnet.net*Subject*: [mg90606] Re: A NDSolve within a FindMinimum/NMinimize*From*: dh <dh at metrohm.ch>*Date*: Tue, 15 Jul 2008 06:49:49 -0400 (EDT)*References*: <g5htfb$j7t$1@smc.vnet.net>

Hi Philipp, you have 2 bugs here. First, NMinimize first calls the function with a symbolic argument to see if it can do a symbolic simplification. You may fix this by wrapping a function with a numerical argument around your code. Second, NMinimize returns a list of results, even if there is only one result. To fix this, you simply take the first element of the list. Here is the corrected code: f[a_?NumericQ]:=(s=NDSolve[{El'[x]+a Sin[x] El[x]==0,El[0]==a},El,{x,-1,1}][[1]];(El[1]/.s) a) NMinimize[f[a],a] hope this helps, Daniel 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 > -- Daniel Huber Metrohm Ltd. Oberdorfstr. 68 CH-9100 Herisau Tel. +41 71 353 8585, Fax +41 71 353 8907 E-Mail:<mailto:dh at metrohm.com> Internet:<http://www.metrohm.com>