Re: FindRoot with NDSolve inside of it doesn't work
- To: mathgroup at smc.vnet.net
- Subject: [mg111365] Re: FindRoot with NDSolve inside of it doesn't work
- From: Sam Takoy <sam.takoy at yahoo.com>
- Date: Thu, 29 Jul 2010 06:42:36 -0400 (EDT)
Thank you as always.
Perfect.
________________________________
From: Leonid Shifrin <lshifr at gmail.com>
To: Sam Takoy <sam.takoy at yahoo.com>; mathgroup at smc.vnet.net
Sent: Wed, July 28, 2010 2:52:56 PM
Subject: [mg111365] Re: [mg111343] FindRoot with NDSolve inside of it doesn't work
Sam,
The difference is that in your last example with exponents the r.h.s of the
function
does not necessarily involve numerical steps, while when you invoke NDSolve,
it does. So, for functions of the former type, you can get away with generic
patterns,
while for functions of the latter type you can not.
This is related to the order in which expressions are evaluated. Inside FindRoot
there is
f[x0, y0] == {2.7, 5.4}. When this is evaluated, first f[x0, y0] is evaluated.
If you have
generic patterns x_,y_ in the definition of <f>, then the definition for <f>
happily applies
at this moment, while x0 and y0 are still symbolic. This is wrong since then
NDSolve
can not do its job and returns back the set of equations. To postpone the
evaluation of
<f> until numeric values are supplied by FindRoot in place of x0, y0, we
restrict the pattern.
Then, evaluation of f[x0, y0] == {2.7, 5.4} just yields back f[x0, y0] == {2.7,
5.4}, since the
pattern is not matched by symbolic x0,y0, and then later x0 and y0 are bound to
(replaced by)
the numeric values generated by FindRoot, which results in correct execution.
Cases where one needs to use this trick are interesting since the interaction
between
built-in functions (FindRoot here) and user - defined ones (f here) mediated by
this
trick are rather non-trivial. In some sense, we go a little under the hood of
the built-in
commands here. However they work, we know for sure that they are bound to call
Mathematica
evaluator on <f> at some point since <f> was defined at top level. And that
means that
we have all the usual possibilities to alter that part of evaluation that the
evaluator normally
gives us. Now, since FindRoot holds all its arguments, one may ask why
does it not
bind equations to numerical arguments right away. My guess is that sometimes it
attempts
to do some symbolic preprocessing with the equations, a possibility which would
be ruled
out in the latter scenario.
FindRoot does not really care which type the equation is, symbolic or numeric or
mixed,
but the real problem was in the way you defined <f> - on some (symbolic say)
arguments it can
produce nonsense, and that means a flawed design of your function. If it makes
sense to
call it only on numeric arguments (like in your original case), this should be
reflected in its
definition. In all other cases, it should return unevaluated (this is a general
rule for functions
in Mathematica), and this is what the pattern-based definition semantics gives
you.
Hope this helps.
Regards,
Leonid
On Wed, Jul 28, 2010 at 9:17 PM, Sam Takoy <sam.takoy at yahoo.com> wrote:
Hi,
>
>
>Thanks for the response.
>
>
>I have determined that introducing ?NumericQ alone works and I am wondering
>why?
>
>
>How is the "f" that involves NDSolve fundamentally different from
>
>
>f[x0_,y0_]:={x0 Exp[1],y0 Exp[1]}
>
>
>?
>
>
>Thanks again,
>
>Sam
>
>
>
________________________________
From: Leonid Shifrin <lshifr at gmail.com>
>To: Sam Takoy <sam.takoy at yahoo.com>; mathgroup at smc.vnet.net
>Sent: Wed, July 28, 2010 6:03:47 AM
>Subject: Re: [mg111343] FindRoot with NDSolve inside of it doesn't work
>
>
>
>Sam,
>
>This modification will work:
>
>
>In[31]:=
>Clear[f];
>f[x0_?NumericQ, y0_?NumericQ] :=
> Module[{sol, x, y},
> (sol = NDSolve[{x'[t] == x[t], y'[t] == y[t], x[0] == x0,
> y[0] == y0}, {x, y}, {t, 0, 1}];
> {x[1], y[1]} /. sol[[1]])]
>
>
>In[33]:=
> (*f[x0_,y0_]:={x0 Exp[1],y0 Exp[1]};(*Works for this f*)*)
>FindRoot[f[x0, y0] == {2.7, 5.4}, {{x0, 1}, {y0, 2}}]
>
>
>
>
>Out[33]= {x0 -> 0.993274, y0 -> 1.98655}
>
>
>Apart from localizing your variables, the main thing here was to restrict x0 and
>y0
>as input parameters to <f>, to only numeric values, by appropriate patterns.
>
>
>Regards,
>Leonid
>
>
>
>On Wed, Jul 28, 2010 at 10:54 AM, Sam Takoy <sam.takoy at yahoo.com> wrote:
>
>Hi,
>>
>>I think it's completely self-explanatory what I'm trying to do in this
>>model example:
>>
>>f[x0_, y0_] := (
>> sol = NDSolve[{x'[t] == x[t], y'[t] == y[t], x[0] == x0,
>> y[0] == y0}, {x, y}, {t, 0, 1}];
>> {x[1], y[1]} /. sol[[1]]
>> )
>>
>>(*f[x0_, y0_]:={x0 Exp[1], y0 Exp[1]}; (* Works for this f *) *)
>>FindRoot[f[x0, y0] == {2.7, 5.4}, {{x0, 1}, {y0, 2}}]
>>
>>Can someone please help me fix this and explain why it's not currently
>>working?
>>
>>Many thanks in advance!
>>
>>Sam
>>
>>
>
>