Re: how make function of solution by NDSolve depending on parameter?
- To: mathgroup at smc.vnet.net
- Subject: [mg75144] Re: how make function of solution by NDSolve depending on parameter?
- From: dh <dh at metrohm.ch>
- Date: Wed, 18 Apr 2007 05:06:28 -0400 (EDT)
- References: <f014kb$94o$1@smc.vnet.net>
Hi Murray, NMinimize does not have the attribute HoldFirst. Therefore, the argument is evaluated before beeing fed to NMinimize. But at this time k does not have a value. Therefore, put the first argument into a function that is evaluated only for numerical values. E.g.: soln[k_]:=NDSolve[{y'[t]==(k y[t]+Exp[-t])/(1+y[t]),y[0]==1}, y[t],{t,0,1}]; fun[k_/;NumberQ[k]]:=y[t]/.soln[k][[1,1]]/.t->0.5; NMinimize[fun[k],{k,0.1,1}] hope this helps, Daniel Murray Eisenberg 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. >