The code with one condition Mod[t, 2 π] == 0
data = Block[{d = 0.15, r = 0.3},
Reap[NDSolve[{x''[t] + d x'[t] - x[t] + x[t]^3 == r Cos[ t],
x[0] == 0, x'[0] == 0,
WhenEvent[Mod[t, 2 π] == 0(*&&t>50*),
Sow[{t, x[t], x'[t]}]]}, {}, {t, 0, 100},
MaxSteps -> ∞]]]
works well in v11.3, and gives the data as below
{{{}}, {{{6.28319, 0.895631,
0.418075}, {12.5664, -1.21673, -0.312119}, {18.8496, -0.405354,
0.587376}, {25.1327, -0.254392, -0.19556}, {31.4159, -0.40937,
0.151545}, {37.6991, -0.141298, 0.702613}, {43.9823, -1.09087,
1.0678}, {50.2655, -0.921924, -0.607913}, {56.5487, -0.594581,
0.48939}, {62.8319, 1.09998, -0.105309}, {69.115, 1.19792,
0.541834}, {75.3982, -1.09163, -0.417725}, {81.6814, -0.479742,
0.483879}, {87.9646, 0.846189,
0.400168}, {94.2478, -1.28112, -0.186879}}}}
However, when conditions Mod[t, 2 π] == 0&&t>50
are applied, the data for t>50
is not outputed.
Somebody can explain it? Any suggestions would be much appreciated!
event[t_] := Mod[t, 2 \[Pi]] == 0
: MMA v12 evaluates{{{}}, {}}
in this case. $\endgroup$WhenEvent
has the attributeHoldAll
, when this intermediate function is introduced,WhenEvent
only sees aevent[t]
, so, as mentioned in the Details and Options section ofWhenEvent
, it uses the strategy for pred to detect the event i.e. the event is detected only if the predicate pred becomesTrue
, which is almost impossible, while when one directly writeMod[t, 2 Pi] == 0
or useevent[t]//Evaluate
to make it explicit,WhenEvent
will see it and turn to the specialized strategy. $\endgroup$