From: Ulvi Yurtsever on
I have an interpolated function obtained
as the solution to a system of ODEs via NDSOlve.
The system and the solution depend on a number
of parameters. I then want to plug this function into
FindRoot to find the numerical solution of a system
of equations which depend on both the dependent variable
of the ODEs and the parameters. Mathematica barfs at
the use of parameters as in the following example:

First, what works as expected:


In[135]:= solnn =.

In[142]:=
solnn[a_] :=
NDSolve[{x'[t] == a *y[t], y'[t] == -x[t], x[0] == 1, y[0] == 0}, {x,
y}, {t, 0, Pi}]

In[144]:= FindRoot[(x /. solnn[1][[1]])[t] - (y /. solnn[1][[1]])[
t] == 0, {t, 2}]

Out[144]= {t -> 2.35619}


however:


FindRoot[{(x /. solnn[a][[1]])[t] - (y /. solnn[a][[1]])[t] == 0,
a - 1 == 0}, {{a, 0}, {t, 2}}]


produces, instead of the expected

{a->1., t->2.355619},

lots of error messages to the effect that NDSolve has encountered non-
numerical initial values etc

Is there any other way to use FindRoot for the purpose I am trying to
use it?

From: danl on
> I have an interpolated function obtained
> as the solution to a system of ODEs via NDSOlve.
> The system and the solution depend on a number
> of parameters. I then want to plug this function into
> FindRoot to find the numerical solution of a system
> of equations which depend on both the dependent variable
> of the ODEs and the parameters. Mathematica barfs at
> the use of parameters as in the following example:
>
> First, what works as expected:
>
>
> In[135]:= solnn =.
>
> In[142]:=
> solnn[a_] :=
> NDSolve[{x'[t] == a *y[t], y'[t] == -x[t], x[0] == 1, y[0] == 0}, {x,
> y}, {t, 0, Pi}]
>
> In[144]:= FindRoot[(x /. solnn[1][[1]])[t] - (y /. solnn[1][[1]])[
> t] == 0, {t, 2}]
>
> Out[144]= {t -> 2.35619}
>
>
> however:
>
>
> FindRoot[{(x /. solnn[a][[1]])[t] - (y /. solnn[a][[1]])[t] == 0,
> a - 1 == 0}, {{a, 0}, {t, 2}}]
>
>
> produces, instead of the expected
>
> {a->1., t->2.355619},
>
> lots of error messages to the effect that NDSolve has encountered non-
> numerical initial values etc
>
> Is there any other way to use FindRoot for the purpose I am trying to
> use it?

It is best done by restricting some functions to numeric arguments. That
way FindRoot will not try to evaluate symbolic things for which it does
not have numeric parameter values.

solnn[a_?NumericQ] := {x, y} /.
First[NDSolve[{x'[t] == a*y[t], y'[t] == -x[t], x[0] == 1,
y[0] == 0}, {x, y}, {t, 0, Pi}]]

eqn[a_?NumericQ, t_?NumericQ] :=
With[{sol = solnn[a]}, sol[[1]][t] - sol[[2]][t]]

In[85]:= FindRoot[{eqn[a, tt] == 0, a - 1 == 0}, {{a, 0}, {tt, 2}}]

Out[85]= {a -> 1., tt -> 2.35619}

Daniel Lichtblau
Wolfram Research




From: Patrick Scheibe on
Hi,

try to localize everything:

solnn[a_?NumericQ, xx_?NumericQ] :=
Module[{x, y, t},
Subtract @@
Through[({x, y} /.
First(a)NDSolve[{x'[t] == a*y[t], y'[t] == -x[t], x[0] == 1,
y[0] == 0}, {x, y}, {t, 0, Pi}])[xx]]
]

FindRoot[solnn[1, t], {t, 2}]
FindRoot[{solnn[a, t] == 0, a - 1 == 0}, {{a, 0}, {t, 2}}]

and the world is beautiful again.

Cheers
Patrick

PS: Read the error-messages carefully and you understand the bad things
that happen with your t when it's not localized.

PPS: People who use a(a)b.c as mail-address won't get an answer next time.

On Sun, 2010-07-04 at 06:09 -0400, Ulvi Yurtsever wrote:
> I have an interpolated function obtained
> as the solution to a system of ODEs via NDSOlve.
> The system and the solution depend on a number
> of parameters. I then want to plug this function into
> FindRoot to find the numerical solution of a system
> of equations which depend on both the dependent variable
> of the ODEs and the parameters. Mathematica barfs at
> the use of parameters as in the following example:
>
> First, what works as expected:
>
>
> In[135]:= solnn =.
>
> In[142]:=
> solnn[a_] :=
> NDSolve[{x'[t] == a *y[t], y'[t] == -x[t], x[0] == 1, y[0] == 0}, {x,
> y}, {t, 0, Pi}]
>
> In[144]:= FindRoot[(x /. solnn[1][[1]])[t] - (y /. solnn[1][[1]])[
> t] == 0, {t, 2}]
>
> Out[144]= {t -> 2.35619}
>
>
> however:
>
>
> FindRoot[{(x /. solnn[a][[1]])[t] - (y /. solnn[a][[1]])[t] == 0,
> a - 1 == 0}, {{a, 0}, {t, 2}}]
>
>
> produces, instead of the expected
>
> {a->1., t->2.355619},
>
> lots of error messages to the effect that NDSolve has encountered non-
> numerical initial values etc
>
> Is there any other way to use FindRoot for the purpose I am trying to
> use it?
>


From: Peter Pein on
Am Sun, 4 Jul 2010 10:09:48 +0000 (UTC)
schrieb Ulvi Yurtsever <a(a)b.c>:

> I have an interpolated function obtained
> as the solution to a system of ODEs via NDSOlve.
> The system and the solution depend on a number
> of parameters. I then want to plug this function into
> FindRoot to find the numerical solution of a system
> of equations which depend on both the dependent variable
> of the ODEs and the parameters. Mathematica barfs at
> the use of parameters as in the following example:
>
> First, what works as expected:
>
>
> In[135]:= solnn =.
>
> In[142]:=
> solnn[a_] :=
> NDSolve[{x'[t] == a *y[t], y'[t] == -x[t], x[0] == 1, y[0] == 0}, {x,
> y}, {t, 0, Pi}]
>
> In[144]:= FindRoot[(x /. solnn[1][[1]])[t] - (y /. solnn[1][[1]])[
> t] == 0, {t, 2}]
>
> Out[144]= {t -> 2.35619}
>
>
> however:
>
>
> FindRoot[{(x /. solnn[a][[1]])[t] - (y /. solnn[a][[1]])[t] == 0,
> a - 1 == 0}, {{a, 0}, {t, 2}}]
>
>
> produces, instead of the expected
>
> {a->1., t->2.355619},
>
> lots of error messages to the effect that NDSolve has encountered non-
> numerical initial values etc
>
> Is there any other way to use FindRoot for the purpose I am trying
> to use it?
>

Define an auxilliary function which evaluates iff a _and_ t have got
numeric values (I changed solnn a bit for better handling):

In[1]:= Clear[solnn,x,y];
solnn[a_?NumericQ] :=
Block[{t},
First[
NDSolve[{x'[t] == a*y[t], y'[t] == -x[t], x[0]==1, y[0] == 0},
{x, y}, {t, 0, Pi}]
]]

In[3]:= FindRoot[x[t] - y[t] == 0 /. solnn[1], {t ,2}]
Out[3]= {t->2.35619}

In[4]:= target[a_?NumericQ, t_?NumericQ] := x[t] - y[t] /. solnn[a]
In[5]:= FindRoot[{target[a, t] == 0, a - t/3 == 0},
{{a, 0}, {t, 2, 0, Pi}}]
Out[5]= {a->0.860285,t->2.58085}

Peter


From: Ulvi Yurtsever on

My thanks to all those who replied. I've also found the page

http://reference.wolfram.com/mathematica/tutorial/UnconstrainedOptimization
SymbolicEvaluation.html

where a clear explanation of the main issue is given.