MathGroup Archive 2003

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

Search the Archive

Re: how do i access the inside of the interpoating functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg43542] Re: [mg43518] how do i access the inside of the interpoating functions
  • From: Selwyn Hollis <selwynh at earthlink.net>
  • Date: Fri, 19 Sep 2003 03:42:11 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Hi Sean,

If your Interpolating function is named interp and covers the interval 
[a,b], you can use FindMinimum like this:

FindMinimum[-interp[t],{t,.5(a+b),a,b}]

So the rub is how to extract a and b...  This does it with a rule:

FindMinimum[-interp[t],
    interp /. _[{{a_, b_}}, __] :> {t, 0.5*(a + b), a, b}]

Also, I recently wrote the following, which you and others might find 
useful. It defines a function named InterpolatingFunctionPlot.	
Needs["Graphics`Graphics`"];

InterpolatingFunctionPlot[f_List, (opts___)?OptionQ] :=
    DisplayTogether[(Plot[Evaluate[#[t]],
    Evaluate[#/. _[{{t1_,t2_}},__] :> {t,t1,t2}], opts] & ) /@ f] /;
     MatchQ[f, {InterpolatingFunction[__]..}];

InterpolatingFunctionPlot[f_InterpolatingFunction, (opts___)?OptionQ] :=
    InterpolatingFunctionPlot[{f}, opts] ;

InterpolatingFunctionPlot[f:{{_, _}..}, (opts___)?OptionQ] :=
    DisplayTogether[(ParametricPlot[
    Evaluate[#/. i_InterpolatingFunction -> i[t]],
    Evaluate[First[#/. _[{{t1_,t2_}}, __] :> {t,t1,t2}]], opts]&)/@ f]/;
     MatchQ[f, {{InterpolatingFunction[__]..}..}];

InterpolatingFunctionPlot[f:{{_, _, _}..}, (opts___)?OptionQ] :=
    DisplayTogether[(ParametricPlot3D[
    Evaluate[#/. i_InterpolatingFunction -> i[t]],
    Evaluate[First[#/. _[{{t1_,t2_}}, __] :> {t,t1,t2}]], opts]&)/@ f]/;
     MatchQ[f, {{InterpolatingFunction[__]..}..}];

(*Example*)

soln = {x, y, z} /.
First[NDSolve[{x'[t] == x[t]*Cos[z[t]], y'[t] == x[t] - y[t],
       z'[t] == x[t] - z[t], x[0] == 1, y[0] == 1, z[0] == 0},
      {x, y, z}, {t, 0, 10}]]

InterpolatingFunctionPlot[soln]

InterpolatingFunctionPlot[{soln}]


-----
Selwyn Hollis
http://www.math.armstrong.edu/faculty/hollis

On Thursday, September 18, 2003, at 05:40  AM, sean k wrote:

> good to see the group back up.
>
> What I would like to do is to look inside the interpolatingn functions
> generated by the NDSolve in Mathematica.
>
> so take y[t] -> InterpolatingFunction[{{0., 25.}}, "<>"][t] for
> instance.
>
> I need to figure out a few things about this interpolating function.
>
> 1. what is the Maximum y[t] value and the t value associated with that
> in the range of t given. (if there are more than on maximums, then the
> value of the first one that occurs in terms of time and the value of
> t. )
>
> 2. what is the half Maximal y[t] value and the t values associated
> with it?
>
> 3. are any of values less than 0?  if it has at least a single
> value that is negative, thenn I would like to throw out the
> integration( is this a proper way to control for artifacts/or
> targets?)
>
> For 1, 2 what I have thought initially is to use Table and use some
> kinda logic along with it. but it will be too slow since the intervals
> will be have to be sufficienly small.
>
> For 3, I'm kinda stumped. Similarly i can use table but its still
> going to be so slow...  There has to be another way to approachthis
> problem?
>
> So far only thing that I have found in the math group archives that
> does something similar to what I want is to use the FindRoot as below.
> It seems to work for the equations that saturate( second cells pasted
> below), but not for the type shown below. I's using lorenz eqn's here
> as an example. This really isn;t very good example since the maximum
> comes later in the time range, but my systems shows maximums before in
> the eralier time points.
>
>
> begin first cell----
>
> solution =
>   NDSolve[{Derivative[1][x][t] == -y[t] - z[t], x[0] == -0.04,
>       Derivative[1][y][t] == x[t] + 0.425*y[t], y[0] == -0.3,
>       Derivative[1][z][t] == 2 - (4 - x[t])*z[t], z[0] == 0.52},
> {x[t], y[t],
>       z[t]}, {t, 0, 25}, Method -> RungeKutta]
> (*ParametricPlot3D[Evaluate[{x[t], y[t], z[t]} /. solution], {t, 0,
> 25},
>       PlotPoints -> 1000, PlotRange -> All];*)
>
> (*pulling out interpolating functions*)
> interp =  solution[[1, 2, 2]]
>
> (*finding the maxvalues for y[t]*)
> maxYoft =  Max[Table[interp, {t, 0, 25, 1}]]
>
> (*finding the t value for max Y this doesn't work. is there any other
> ways to do this? instead of using findroot?  I don't quite understand
> how it's working, and it seems to cause lotta errors which means i
> would have to use Check[] on this too....*)
>
> tformaxY = FindRoot[interp == maxYoft, {t, 0}]
>
> (*half maximal y*)
> halfmaxY = ( maxYoft)/2
>
> (*tfor half maximal y*)
> {t, halfmaxY}
> tforhalfmaxY = FindRoot[interp == halfmaxY, {t, 0}]
>
> Plot[Evaluate[y[t] /. solution], {t, 0, 25}];
>
> end first cell---
>
>
> this one below works as expected and pick out at least the half
> maximal values and time associated with it and generates no errors.
>
>
> begin second cell ---
>
> In[1]:=
> NDSolve[{D[y[t], t] == 1 - y[t], y[0] == 0}, y[t], {t, 0, 20}];
> secondCellsol = y[t] /. %[[1]]
> maxy = Max[Table[secondCellsol, {t, 0, 20, 0.01}]]
> halfmaxy = maxy/2
> FindRoot[secondCellsol == halfmaxy, {t, 1}]
> Plot[secondCellsol, {t, 0, 10}, PlotRange -> {0, 2}]
>
> end second cell ---
>
>


  • Prev by Date: Re: Re: Re: v5 for Mac
  • Next by Date: contourplot3d again
  • Previous by thread: how do i access the inside of the interpoating functions
  • Next by thread: Introducing constraints in Nonlinear Fit "a la Levenberg Marquardt"