[Date Index]
[Thread Index]
[Author Index]
Re: NMinimize/FindMinimum and function involving NDSolve
*To*: mathgroup at smc.vnet.net
*Subject*: [mg46256] Re: [mg46219] NMinimize/FindMinimum and function involving NDSolve
*From*: Daniel Lichtblau <danl at wolfram.com>
*Date*: Thu, 12 Feb 2004 22:46:16 -0500 (EST)
*References*: <200402121215.HAA11908@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
Kezhao Zhang wrote:
> Hello,
>
> I tried to find the initial condition for a second order ODE by
> numerically solving the equation and using NMinize or FindMininum. The
> objective to be minimized is defined as a function of c1 and c2, the
> initial condition. The problem is that NMimize and FindMinum complain
> that NDSolve::"ndinnt": "Initial condition x is not a number or a
> rectangular array of numbers". I wonder if NMinimize and FindMinimum
> can take a function involving complex rpocedurals like NDSolve. If
> not, is there a workaround? My code is listed below. Thanks for your
> help.
>
> In[]:=objective[c1_, c2_] := Module[{x, t, s, xx}, xx = Linspace[0,
> 2., 40];
> s = NDSolve[{x''[t] - x'[t] + x[t] == 0, x[0] == c1, x'[0] == c2},
> x, {t,
> 0, 2}];
> Total[(First[x /. s][#] - Exp[#/2] Cos[Sqrt[3]/2#])^2 & /@ xx]]
>
> In[]:=FindMinimum[objective[x, y], {{x, 1}, {y, 1}}]
> or
> In[]:=NMinimize[objective[x, y], {x, y}]
>
> gives error message "NDSolve::ndinnt: Initial condition x is not a
> number or a rectangular array of numbers."
>
> K. Zhang
>
This is a common question since the emergence of version 5. The short
answer is to insist that objective take numerical values. For example,
you could do as below.
objective[c1_Real, c2_Real] := Module[
{x, t, s, xx},
s = NDSolve[{x''[t]-x'[t]+x[t] == 0, x[0]==c1, x'[0]==c2}, x, {t,0,2}];
Apply[Plus, ((First[x/.s][#] - Exp[#/2]*Cos[Sqrt[3]/2#])^2)& /@
Table[j, {j,0.,2.,.025}]]
]
We can now use objective[] in FindMinimum or NMinimize.
In[6]:= InputForm[FindMinimum[objective[x,y], {x,1,2}, {y,1,2}]]
Out[6]//InputForm=
{8.808049059330324*^-15, {x -> 0.9999999799891415, y -> 0.5000000206170404}}
In[7]:= InputForm[NMinimize[objective[x, y], {x, y}]]
Out[7]//InputForm=
{2.259003660110298*^-14, {x -> 0.9999999655576155, y -> 0.5000000133413322}}
Daniel Lichtblau
Wolfram Research
Prev by Date:
**Re: how to explain this weird effect? Integrate**
Next by Date:
**RE: MultipleListPlot**
Previous by thread:
**NMinimize/FindMinimum and function involving NDSolve**
Next by thread:
**Sorting text-file data into list lists**
| |