How come this doesn't work as I intended?
s[{x0_, y0_}, a_, v_] := NDSolveValue[{
x'[t] == vx[t], WhenEvent[x[t] > 0, vx[t] -> -vx[t]],
y'[t] == vy[t] - t, WhenEvent[y[t] < 0, vy[t] -> -vy[t]],
x[0] == x0, vx[0] == v Cos@a,
y[0] == y0, vy[0] == v Sin@a},
{x[t], y[t]}, {t, 0, 10},
DiscreteVariables -> {vx, vy}]
Simulating falling and bouncing of a 2D mass point.
With[{r0 = {-5, 7}},
ParametricPlot[Evaluate@s[r0, -.9, 2],
{t, 0, 10},
PlotRange -> {{-10, 10}, {-10, 10}},
Epilog -> {Point@r0}]]
I can't understand why it doesn't bounce on the x-axis first, and what does happen there anyway? Event x[t]>0
works fine.
Following help I started with lowering the order of equations (and so doubling the number of variables). Here I wrote the equations in a more familiar 2nd order; everything works as expected. I put the function below in a tiny loop which bisected the right initial velocity v0
that returns the ball in its starting position.
Could this be done within NDSolve
? I looked Wolfram tutorial for Boundary Value Problems but haven't been able to figure it out.
sstop[{x0_, y0_}, a_, v0_] := NDSolveValue[{
x''[t] == 0, WhenEvent[x[t] > 0, x'[t] -> -x'[t]],
y''[t] == -1, WhenEvent[y[t] < 0, y'[t] -> -y'[t]],
WhenEvent[x[t] < x0, Sow[{t, y[t]}]; "StopIntegration"],
x[0] == x0, x'[0] == v0 Cos[a],
y[0] == y0, y'[0] == v0 Sin[a]},
{x[t], y[t]}, {t, 0, 20}] // Reap
Answer
Here you have a bouncing ball simulation using WithEvents[]:
s[{x0_, y0_}, a_, v_] := NDSolveValue[
{
y''[t] == -1,
x'[t] == vx[t],
y[0] == y0,
x[0] == x0,
y'[0] == v Sin@a,
vx[0] == v Cos@a,
vy[0] == v Sin@a,
WhenEvent[y[t] < 0, y'[t] -> - y'[t]],
WhenEvent[x[t] > 0, vx[t] -> -vx[t]]},
{x, y}, {t, 0, 8},
DiscreteVariables -> {vx, vy}] ;
With[{r0 = {-1, 1}},
ParametricPlot[Through[s[r0, 1, 1][u]], {u, 0, 5}, PlotRange -> All,
Epilog -> {Point@r0}]]
Comments
Post a Comment