Message Boards Message Boards

0
|
4280 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Discrete values from NDSolve?

Posted 11 years ago

NDSolve typically returns an interpolation function, which you can then build a table of values from. However, if I only need a pre-determined set of discrete time steps, can I just directly obtain these from NDSolve without building the interpolating function?

Presumably if i do

state = NDSolve`ProcessEquations[ <stuff>, t ];
NDSolve`Iterate[state, 1 ] (* iterates from t=0 out to t=1 *)

then i already have a list of discrete values stored somewhere in "state", and I don't need to do

NDSolve`ProcessSolutions[state]

Any ideas on how to do this? I just want a list of values, say at t={0,0.2,.0,4,0.6,0.8,1}, for example.

POSTED BY: brad ramshaw
3 Replies

Here is a solution with NDSolve components:

state = First@NDSolve`ProcessEquations[{x''[s]+x[s]==0,x[0]==0,x'[0]==1},x,s];
data=Rest@Flatten@Reap@Do[
    NDSolve`Iterate[state,0.1];
    point=NDSolve`SolutionDataComponent[state@"SolutionData"["Forward"], "DependentVariables"];
    Sow[{t,point}];
    state=First@NDSolve`Reinitialize[state,Thread[{x[0],x'[0]}==point]];,
    {t,0.1,1.,0.1}
]
Show[Plot[Sin[s],{s,0,1},Frame->True,Axes->False],ListPlot[Partition[data,3][[;;,1;;2]],PlotStyle->{Red,PointSize[Large]}]]
POSTED BY: Ivan Morozov
Posted 11 years ago

Ok, thanks a lot. It turns out this is actually slower (about a factor of ten) than just Threading the interpolation function NDSolve gives you over a list of times. But I learned a lot from your example.

POSTED BY: brad ramshaw

Hi,

Try to use "EventLocator" method for NDSolve (or WhenEvent[] option). Here is an example with "EventLocator" (for some reason values at 0. are not catched):

data = Flatten[
    Reap[
       NDSolve[
         {
         x''[s]+x[s]==0,
         x[0]==0,
         x'[0]==1
         },
         {},
         {s,0.,1.},
         Method->{
          "EventLocator",
          "Event"-> Thread[s-Table[x,{x,0.,1.,0.2}]],
          "EventAction" :> Sow[{s,x[s],x'[s]}],
          "Direction" -> 1
         }
       ]
    ]
]
Show[
    Plot[Sin[s],{s,0,1},Frame->True,Axes->False],
    ListPlot[Partition[data,3][[;;,1;;2]],PlotStyle->{Red,PointSize[Large]}]
]

Not sure if it is possible with NDSolve components, you can try to set EventLocator method for NDSolve`ProcessEquations with SetOptions[]

POSTED BY: Ivan Morozov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract