2.5.4 Events of a differential equation
Sometimes, when we are implementing the evolution of our simulation through the
solution of a system of differential equations, we want the computer to detect that
a given condition which depends on the variables of the system has taken place,
allowing us then to make some corrections to adjust the simulation.
For instance, suppose that the falling body that we are simulating using equations
(2.4) is an elastic ball that we have thrown against the floor. The numerical
solution of these equations doesn’t take into account, by itself, the fact that the
computed solution will eventually take the ball to a position below ground, that
is, y(t) < 0. As the method for numerical solution advances at constants step of
time, it is very likely that the exact moment of the collision of the ball with the
floor doesn’t coincide with any of the solution steps of the algorithm, taking the ball
to a, let’s put it this way, ‘illegal state’. Instead of this, we would have preferred
that the computer had detected the problem and had momentaneously stopped at
the precise instant of the collision, applying then the code necessary to simulate the
rebounding of the ball against the floor, and continuing the simulation from these
new initial conditions. This is the archetypical example of what we call an event.
More precisely, we define an event as the change of sign of a real-valued function
of the state variables (the variables that we differentiate) and of the independent
variable of an ODE. (Events caused only by the independent variable are traditionally
called time events, while those caused by the other variables are called state
events). To simplify the discussion that follows and, since actually all variables depend
on the independent variable, we will denote by h(t) the function that changes

an event is specified by providing: (a) the function h
which depends on the state, (b) the desired tolerance, and (c) the action to invoke
at the event instant.

Creating events for an ODE
Notice that the editor for differential equations includes a button labeled “Events”
and a field that indicates that, by default, there are no events defined for this
equation. Clicking the button “Events” will bring in an independent window with
an editor with a behavior similar to that found in other parts of the model, and
which we can use to create as many pages of events for our differential equation as
we want.
There exist, however, some differences. Observe in Figure 2.16 that a page for
the edition of events appears divided into two sections (besides the text field for

In the upper section of this page we need to indicate how to detect the event. For
this, we need to write the code that computes the function h(t) from the values of

the state and the independent variable of our ODE. This code must end, necessarily,
returning a value of type double. To help us remember this, the editor writes by
default the next code (which, by the way, causes no event at all):
return 1.0;

In the “Tolerance” field on the upper right corner of this section we need to indicate
the value for the tolerance that we want for this event (this is the  in the discussion
above).
The lower section is used to tell the computer what to do when it detects (and
then precisely locates) an event. Recall that this action must solve, or at least
simplify, the situation that triggered the event. In the upper-right corner of this
second section we find a checkbox labeled “Stop at event”, currently activated, that
tells the computer whether it should return from the solution of the ODE at the
instant of the event or not. Notice that, if checked, this causes the real increment
of the independent variable to be smaller that the one originally desired. But still,
checking this option may be useful if you want to appreciate the exact moment of
the event.

We can use our example of the falling ball to construct a sample event. For this,
edit the code of the upper section of the page so that it reads:
return y;
This indicates that the event will take place when the ball reaches the level of the
ground, y = 0. The default value for the tolerance is adequate. As action for the
event, write the code:
vy = -vy;

which simulates a totally elastic rebounding at the instant the event takes place.
Leave the box “Stop at event” checked, so that the system visualizes the instant of
the rebounding.