The problem is $$ x''(t)=F(t) - kx'(t) \\ x'(0)=x(0)=0$$ with the additional constraints $$ 0 < x(t) WhenEvent[...].
Example:
First@First@NDSolve[{
x''[t] == 10 Sin[0.1 t] - 0.1 x'[t], x[0] == 0,
x'[0] == 0, WhenEvent[x[t] > 400, x[t] -> 399],
WhenEvent[x'[t] > 40, x'[t] -> 39],
WhenEvent[x[t] < 0, x[t] -> 1],
WhenEvent[x'[t] < -40, x'[t] -> -39]},
x, {t, 0, 200},
MaxSteps -> 50000];
Plot[{x[t] /. %, 10 x'[t] /. %}, {t, 0, 200}, PlotRange -> All,
PlotLegends -> {"x[t]", "x'[t]"}, ImageSize -> 700]

What is the proper way to solve this type of problem?
Answer
I guess that can be done. I don't know if you'll love this solution though.
{s, a} = NDSolveValue[{
x''[t] == alive[t] 10 Sin[0.1 t] - 0.1 x'[t],
x[0] == 0, x'[0] == 0,
alive[0] == 1,
speed[0] == 0,
WhenEvent[x[t] > 400, {
speed[t] -> x'[t], x'[t] -> 0, alive[t] -> 0}],
WhenEvent[Sin[.1 t] < 0, {
x'[t] -> -speed[t], alive[t] -> 1}],
WhenEvent[x[t] < 0, {
speed[t] -> x'[t], x'[t] -> 0, alive[t] -> 0}],
WhenEvent[Sin[.1 t] > 0, {
x'[t] -> -speed[t], alive[t] -> 1}]},
{x, alive},
{t, 0, 100},
DiscreteVariables -> {alive, speed}]
Roughly, I stored the first derivative and temporarily killed it together with force and then restored both at the appropriate moment.
Plot[{s[t], 300 a[t]},
{t, 0, 100},
PlotRange -> All,
PlotStyle -> Thick]

Comments
Post a Comment