NDSolve event handler Question [closed]

I'm trying to plot a ball falling, hitting the ground and coming back up.

without air friction, the equations for the system would be

 eqn = {y''[t] == -9.8, y[0] == 10, y'[0] == 0}


Now I want to get the function so that each time $y(t)=0$, the velocity, $y'(t)$ will swap signs... This will mimic when the ball hits the ground

I tried :

 f = NDSolve[eqn, s, {t, 0, 20},
Method -> {"EventLocator", "Event" -> s[t],
"EventAction" :> s'[t]=-s'[t]}]


but alas, I get

"Set::write: Tag RuleDelayed in EventAction:>(s^[Prime])[t] is Protected. >>"

and

"NDSolve::bdmtd: The value of the option Method -> {EventLocator,Event->s[t],-(s^[Prime])[t]} is not a known built-in method, a symbol that could be a user-defined method, or a list with a name followed by method options. >>"

Any thoughts on what I could try?

-

closed as too localized by Michael E2, m_goldberg, Simon Woods, Sjoerd C. de Vries, Yves KlettApr 1 '13 at 17:53

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Have you seen the "Bouncing Ball" demo in the docs? –  Ｊ. Ｍ. Oct 18 '12 at 0:22
I had the same problem. It saddens me to say, but everything is right, except the last line: s'[t]->-s'[t] That is, a -> instead of =. –  user6669 Mar 31 '13 at 7:40
The first error is roughly a syntax error, missing parentheses: "EventAction" :> (s'[t]=-s'[t]). However, I don't think you can change y'[t] in the middle of the integration this way. You can stop integration and restart it as in the bouncing ball example J.M. refers to. –  Michael E2 Mar 31 '13 at 12:39
with v9, this works NDSolve[{y''[t] == -9.81, y[0] == 5, y'[0] == 0, WhenEvent[y[t] == 0, y'[t] -> -0.95 y'[t]]}, y, {t, 0, 10}] (again from the docs) –  acl Mar 31 '13 at 14:04

Why don't you just include a force to model the compression of the ball? Let's also provide parameters for a "spring constant" $k$, to which the force is directly proportional, and the ball radius $r$:

g[y_, k_, r_] := -9.8 + Piecewise[{{20, y <= 0}, {(1 - (y/r))^2 r k/y^2, 0 < y <= r}}, 0];


(The definition for $y\le 0$ is there for completeness. It shouldn't need to be used, but occasionally Mathematica's numerical searches take it to values of $y$ less than $0$, so supplying a net upward restoring force at points "below ground" gives it a useful hint concerning the physical intention.)

Manipulate lets you quickly explore the effect. It tracks the center of the ball.

Manipulate[
eqn = {y''[t] == g[y[t], Exp[k], r], y[0] == 10, y'[0] == 0};
soln = NDSolve[eqn, y, {t, 0, 20}];
Plot[(y /. First@soln)[t], {t, 0, 20}, AxesOrigin->{0, 0}, AxesLabel->{"Time(s)", "Height(m)"}],
{{k, 8, "Log Spring"}, 0, 15}, {{r, 1, "Radius"}, 0.0001, 10}]


-