BUILT-IN MATHEMATICA SYMBOL

# WhenEvent

WhenEvent[event, action]
specifies an action that occurs when the event triggers it for equations in NDSolve.

## Details and OptionsDetails and Options

• In NDSolve[eqns, ...] the WhenEvent expressions are included in eqns and can be considered a part of the specification for the solution, especially for piecewise or hybrid systems.
• At a time when the event becomes True, the action is evaluated with the numerical solution values at . Examples of events and how is determined include:
•  pred the predicate pred becomes True f=0 the function f of the solution variables crosses zero f>0 f crosses zero from below f<0 f crosses zero from above f=0&&pred f crosses zero and pred is True Mod[t,]=0 sample at regular intervals in the time variable
• WhenEvent typically finds a function f of the solution variables such that the roots of f occur where the event becomes True, and a bracketing interval within the accuracy and precision tolerances of the numerical computation is found using a numerical root-finding procedure.
• In the case that a root function cannot be determined, the bracketing interval is found using bisection.
• WhenEvent[{event1, ...}, action] is treated as .
• With WhenEvent[event, {action1, action2, ...}], the actions are evaluated sequentially.
• The value from the evaluation of action is ignored by WhenEvent unless it has a special value that is used to affect NDSolve. The following special values are recognized:
•  "StopIntegration" stop integrating at , return the solution "RestartIntegration" restart the integration at "CrossDiscontinuity" integrate to , extrapolate, and restart at "CrossSlidingDiscontinuity" integrate to , extrapolate and check sliding mode conditions, and restart at "RemoveEvent" remove the event y[t]->val change the state variable y to val d[t]->"DiscontinuitySignature" change the discontinuity signature variable d
• With WhenEvent[f==0, d->"DiscontinuitySignature"], the discontinuity is the surface f==0, and d must be a discrete variable that takes on values or and is effectively set to Sign[f] for subsequent to the event.
• WhenEvent has attribute HoldAll, so by default the event and action are only evaluated with variables assigned approximated values.
• WhenEvent can take the following options:
•  "DetectionMethod" Automatic the method to use for detecting an event "LocationMethod" Automatic the method to use for locating an event within a time step "IntegrateEvent" Automatic whether to integrate the event function "Priority" Automatic priority for actions of simultaneous events
• Possible settings for are:
•  Automatic automatically determine detection method "Sign" use a sign change "DerivativeSign" use sign change and time derivative "Interpolation" use interpolation of dense solution output
• The detection method has the least overhead, but may miss events in rapidly varying functions. The detection method is more robust and may detect multiple events per step.
• Possible settings for are:
•  Automatic automatically determine location method "StepBegin" locate the event at the beginning of a step "StepEnd" locate the event at the end of a step "LinearInterpolation" linearly interpolate within a step "Brent" use Brent's root location within a step
• Possible settings for are:
•  Automatic automatically determined False do not integrate the event function True integrate the event function
• The settings for can be any integer or Infinity. In the case of simultaneous events, the actions will be evaluated in sorted priority order.

## ExamplesExamplesopen allclose all

### Basic Examples (1)Basic Examples (1)

Simulate a bouncing ball that retains 95% of its velocity in each bounce:

 Out[2]=