MathGroup Archive 2007

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

Search the Archive

Re: how make function of solution by NDSolve depending on parameter?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75137] Re: how make function of solution by NDSolve depending on parameter?
  • From: Szabolcs <szhorvat at gmail.com>
  • Date: Wed, 18 Apr 2007 05:02:53 -0400 (EDT)
  • References: <f014kb$94o$1@smc.vnet.net>

On Apr 17, 2:33 am, Murray Eisenberg <murray at math.umass.edu> wrote:
> I have an initial-value problem that depends upon a parameter k.  I want
> to feed the result from NDSolve for it into a function of k and then
> operate upon that function, say to find a minimum with respect to k.
>
> As a toy example:
>
>    soln[k_]:=NDSolve[{y'[t] ==(k y[t]+Exp[-t])/(1+y[t]),
>                y[0]==1},y[t],{t,0,1}]
>
> I want to do something like this:
>
>    NMinimize[Evaluate[y[t] /. soln[k]] /. t -> 0.5, {k, 0.1, 1}]
>
> That generates errors about non-numerical values.  Yet I can get a
> result from, for example:
>
>   Table[Evaluate[y[t] /. soln[k]] /. t -> 0.5, {k, 0, 1}]
>
> So how can I create the function of k I want to feed into NMinimize?  I
> presume the issue is when NDSolve gets called, but I'm not sure how to
> resolve this issue.
>
> --
> Murray Eisenberg                     murray at math.umass.edu
> Mathematics & Statistics Dept.
> Lederle Graduate Research Tower      phone 413 549-1020 (H)
> University of Massachusetts                413 545-2859 (W)
> 710 North Pleasant Street            fax   413 545-1801
> Amherst, MA 01003-9305

Hi!

Unlike Table, NMinimize does not hold its argument until a numerical
value is substituted for k. So you have to make sure that the function
you pass to it is not evaluated for non-numerical arguments.

In[1]:=
soln[k_] := NDSolve[{y'[t] == (k y[t]+Exp[-t])/(1+y[t]), y[0]==1}, y,
{t,0,1}]

In[2]:=
fun[k_?NumericQ] := y[0.5] /. First[soln[k]]

In[3]:=
NMinimize[fun[k], {k, 0.1, 1}]

Out[3]=
{1.2078762005934287*^-13,
  {k -> -5.023394217556404*^12}}

In this particular example no minimum exists but the same approach
should be usable for your real problem. Also note that I changed y[t]
to y in NDSolve to avoid the double ReplaceAll.

Szabolcs



  • Prev by Date: Re: open parenthesis and reduce an expression
  • Next by Date: Re: ImplicitPlot errors
  • Previous by thread: Re: how make function of solution by NDSolve depending on parameter?
  • Next by thread: Re: how make function of solution by NDSolve depending on parameter?