MathGroup Archive 2010

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

Search the Archive

Re: FindRoot with parameterized interpolated function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110758] Re: FindRoot with parameterized interpolated function
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Mon, 5 Jul 2010 06:01:40 -0400 (EDT)

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

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

{t->2.35619}

f[a_?NumericQ, t_?NumericQ] :=
 (x /. solnn[a])[t] -
  (y /. solnn[a])[t]

Plot3D[f[a, t], {a, 0, 2}, {t, 0, Pi}]

With[{a = 2 RandomReal[]},
 {a, t /.
   FindRoot[f[a, t] == 0,
     {t, Pi/2}][[1]]}]

{1.01545,2.3344}

f @@ %

0.

FindRoot[{f[a, t] == 0, a - 1 == 0},
 {{a, 1}, {t, Pi/2}}]

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

f[a, t] /. %

-3.33067*10^-16


Bob Hanlon

---- Ulvi Yurtsever <a at b.c> 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?



  • Prev by Date: Re: FindRoot with parameterized interpolated function from NDSolve
  • Next by Date: Re: Display/manipulating numeric values inside a DynamicModule[]
  • Previous by thread: Re: The side-effects of mixing TraditionalForm inside expressions.
  • Next by thread: a 4d algebraic geometry problem