       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==a},El,{x,-1,1}][];(El/.s) a)

NMinimize[f[a],a]

hope this helps, Daniel

> 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 == a}, El,

> {x, -1, 1}]; ((El /. s) a)[]}, {a, -1, 1, 0.1}]

>

> But a NMinimize or FindMinimum gives errors:

>

> NMinimize[s = NDSolve[{El'[x] + a Sin[x] El[x] == 0, El == a}, El,

> {x, -1, 1}]; (El  /. 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?

>

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

```

• Prev by Date: Re: Wacky Font Substitution in .eps Export
• Next by Date: NIntegrate Confusion
• Previous by thread: A NDSolve within a FindMinimum/NMinimize
• Next by thread: Re: A NDSolve within a FindMinimum/NMinimize