0
|
3590 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
GROUPS:

# Discrete values from NDSolve?

Posted 10 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 = NDSolveProcessEquations[ , t ]; NDSolveIterate[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 NDSolveProcessSolutions[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.
3 Replies
Sort By:
Posted 10 years ago
 Here is a solution with NDSolve components: state = First@NDSolveProcessEquations[{x''[s]+x[s]==0,x==0,x'==1},x,s]; data=Rest@Flatten@Reap@Do[ NDSolveIterate[state,0.1]; point=NDSolveSolutionDataComponent[state@"SolutionData"["Forward"], "DependentVariables"]; Sow[{t,point}]; state=First@NDSolveReinitialize[state,Thread[{x,x'}==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 10 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 10 years ago
 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, x'==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 NDSolveProcessEquations with SetOptions[]