WhenEvent
✖
WhenEvent
Details 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.
- WhenEvent expressions can be used in NDSolve, NDSolveValue, ParametricNDSolve, ParametricNDSolveValue, DSolve, and DSolveValue.
- An event corresponds to a particular time
crossed during the integration of eqns in NDSolve.
- In cases where the event expression is given explicitly in terms of a function f of the solution such that the event becomes True at the roots of f, then WhenEvent uses a numerical root-finding procedure to determine an interval
, where
is the time where the function has a root. Examples of explicitly given events are:
-
f0 the function f of the solution variables crosses zero f>0 f crosses zero from below f<0 f crosses zero from above Mod[t, ]0
sample at regular intervals in the time variable
- With an explicit event expression ev, the event can be made conditional on a predicate pred using the form ev && pred. In this case, the event is only triggered if pred is True at the root of the event function.
- When the event is given in a form that cannot be converted directly to one of the explicit forms above, then the event expression is considered as a predicate pred that should either be True or False. In this case, the interval
is found using bisection.
- The interpretation of explicit events with conditions and events considered as predicates is slightly different. It is best to use explicit events when possible since they are more likely to be detected and finding the root is much more efficient. »
- WhenEvent[{event1,…},action] is treated as {WhenEvent[event1,action],…}.
- 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 {-1, 0, 1} or {-1, 1} 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 "DetectionMethod" 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 "Sign" detection method has the least overhead, but may miss events in rapidly varying functions. The "Interpolation" detection method is more robust and may detect multiple events per step.
- Possible settings for "LocationMethod" 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 "IntegrateEvent" are:
-
Automatic automatically determined False do not integrate the event function True integrate the event function - The settings for "Priority" can be any integer or Infinity. In the case of simultaneous events, the actions will be evaluated in sorted priority order.


Examples
open allclose allBasic Examples (1)Summary of the most common use cases
Scope (16)Survey of the scope of standard use cases
Events (6)
Print the value of t whenever x[t] crosses 1/2:

https://wolfram.com/xid/0b8empjuoy-km6jb

https://wolfram.com/xid/0b8empjuoy-2dvm4

Print the value of t whenever x[t] becomes greater than 1/2:

https://wolfram.com/xid/0b8empjuoy-fj909q
Print the value of t whenever x[t] becomes less than 1/2:

https://wolfram.com/xid/0b8empjuoy-d3h9wg
Sample the solution at regular time intervals :

https://wolfram.com/xid/0b8empjuoy-hzw170

Specify several events in a list to trigger an event when any of them occur:

https://wolfram.com/xid/0b8empjuoy-cn2rya

https://wolfram.com/xid/0b8empjuoy-bgmtrx

Use an event with the condition
to detect crossings of the positive
axis:

https://wolfram.com/xid/0b8empjuoy-gqcrg

https://wolfram.com/xid/0b8empjuoy-ddt2be

In general, the event can be any expression that can be tested for becoming True:

https://wolfram.com/xid/0b8empjuoy-cyb0uf

Trigger an event to stop integration when a button is clicked:

https://wolfram.com/xid/0b8empjuoy-hdhl75

If the Stop button is clicked soon enough, the integration will be terminated before :

https://wolfram.com/xid/0b8empjuoy-fxfasf


Actions (9)
Specify several actions in a list to be executed in consecutive order:

https://wolfram.com/xid/0b8empjuoy-bfif49

https://wolfram.com/xid/0b8empjuoy-910bl

Use "StopIntegration" to end the solution at an event:

https://wolfram.com/xid/0b8empjuoy-9vbiy

https://wolfram.com/xid/0b8empjuoy-tid2i

Use "RemoveEvent" to remove an event after the first occurrence:

https://wolfram.com/xid/0b8empjuoy-rcgz3

https://wolfram.com/xid/0b8empjuoy-ddob0q

Remove an event after the third occurrence:

https://wolfram.com/xid/0b8empjuoy-poxvnl

https://wolfram.com/xid/0b8empjuoy-kjiu5j

Use "RestartIntegration" to force restarting integration after an event:

https://wolfram.com/xid/0b8empjuoy-ecrdor

https://wolfram.com/xid/0b8empjuoy-k9rsxk

https://wolfram.com/xid/0b8empjuoy-cybdfn

A better way to handle this change is to use a discrete state variable a[t]:

https://wolfram.com/xid/0b8empjuoy-c3ojpm

https://wolfram.com/xid/0b8empjuoy-cldfg3

Rules can be used to modify DependentVariables or DiscreteVariables:

https://wolfram.com/xid/0b8empjuoy-isn7zw

https://wolfram.com/xid/0b8empjuoy-flyfhi

Use "CrossDiscontinuity" to manage a discontinuity crossing:

https://wolfram.com/xid/0b8empjuoy-f2sji

https://wolfram.com/xid/0b8empjuoy-0294a

https://wolfram.com/xid/0b8empjuoy-kiqv2a

Use "CrossSlidingDiscontinuity" to indicate that Filippov continuation can be used:

https://wolfram.com/xid/0b8empjuoy-fkw1w5

https://wolfram.com/xid/0b8empjuoy-drcs39

https://wolfram.com/xid/0b8empjuoy-63vst

Use DiscreteVariables and "DiscontinuitySignature" to efficiently cross a discontinuity:

https://wolfram.com/xid/0b8empjuoy-b8lg5d

https://wolfram.com/xid/0b8empjuoy-dq6ke8

In the Filippov sliding mode case, the "DiscontinuitySignature" will be 0:

https://wolfram.com/xid/0b8empjuoy-bw5x2k

https://wolfram.com/xid/0b8empjuoy-vydjp

Solvers (1)
WhenEvent can be used in NDSolve:

https://wolfram.com/xid/0b8empjuoy-hfizm


https://wolfram.com/xid/0b8empjuoy-kgph5


https://wolfram.com/xid/0b8empjuoy-hkikal


https://wolfram.com/xid/0b8empjuoy-b13m2s


https://wolfram.com/xid/0b8empjuoy-ff1ijm


https://wolfram.com/xid/0b8empjuoy-ffrq9m


https://wolfram.com/xid/0b8empjuoy-g4qbb5


https://wolfram.com/xid/0b8empjuoy-pekilt


https://wolfram.com/xid/0b8empjuoy-jqghfu


https://wolfram.com/xid/0b8empjuoy-c51mcu


https://wolfram.com/xid/0b8empjuoy-cr1ny


https://wolfram.com/xid/0b8empjuoy-xaxkn

Generalizations & Extensions (2)Generalized and extended use cases
Options (11)Common values & functionality for each option
"DetectionMethod" (3)
The default event detection method is "Sign", which checks for sign changes between steps:

https://wolfram.com/xid/0b8empjuoy-f9zqm8


https://wolfram.com/xid/0b8empjuoy-qdr9i

https://wolfram.com/xid/0b8empjuoy-muwii4

"Sign" can miss events if there is an even number of crossings between steps:

https://wolfram.com/xid/0b8empjuoy-toow4

"DerivativeSign" halves the step if the sign is the same, but the derivative sign changes:

https://wolfram.com/xid/0b8empjuoy-nfjq3y

For rapidly varying event functions, use the "Interpolation" method to locate the events:

https://wolfram.com/xid/0b8empjuoy-po3pqd


https://wolfram.com/xid/0b8empjuoy-dhzeqe

"LocationMethod" (3)
The default location method "Brent" accurately locates an event:

https://wolfram.com/xid/0b8empjuoy-pv3r3
"LinearInterpolation" method will locate the event reasonably well:

https://wolfram.com/xid/0b8empjuoy-bgowh1
If high accuracy of the event location is not essential, use "StepBegin" or "StepEnd":

https://wolfram.com/xid/0b8empjuoy-cr0hbj
"IntegrateEvent" (2)
When an event function is varying more rapidly than the solution, crossings may be missed:

https://wolfram.com/xid/0b8empjuoy-99784
Plot the event function between and
, with the time steps and crossings found:

https://wolfram.com/xid/0b8empjuoy-glb16c

With "IntegrateEvent"->True, the steps taken are much smaller:

https://wolfram.com/xid/0b8empjuoy-cu7urw

Define a function for comparing event-detection performance:

https://wolfram.com/xid/0b8empjuoy-bsk9er
With "DetectionMethod"->"Interpolation", the event is integrated by default for robustness:

https://wolfram.com/xid/0b8empjuoy-e9svg6


https://wolfram.com/xid/0b8empjuoy-kpgcll

"Priority" (3)
Set priority for events when they happen at the same time, in this case for every integer :

https://wolfram.com/xid/0b8empjuoy-omca4f
Automatic priority will occur after any finite priorities, but before priority Infinity:

https://wolfram.com/xid/0b8empjuoy-hgfa7b
Priorities are ordered in the canonical order used by Sort:

https://wolfram.com/xid/0b8empjuoy-gwgneb

Applications (21)Sample problems that can be solved with this function
Bouncing Balls (3)
Model a ball bouncing down steps:

https://wolfram.com/xid/0b8empjuoy-xmzx76

https://wolfram.com/xid/0b8empjuoy-o6fmnd

Plot the ball's kinetic, potential, and total energy:

https://wolfram.com/xid/0b8empjuoy-s2cy40

https://wolfram.com/xid/0b8empjuoy-hgbqvb

Model 20 bouncing balls dropped simultaneously from different heights:

https://wolfram.com/xid/0b8empjuoy-p87g4f

In a square box, model a ball that changes direction upon impact with the side walls:

https://wolfram.com/xid/0b8empjuoy-y5ekj

https://wolfram.com/xid/0b8empjuoy-jl0x5c

With an irrational initial velocity vector, there are no longer periodic solutions:

https://wolfram.com/xid/0b8empjuoy-96a8i

https://wolfram.com/xid/0b8empjuoy-g408je

Optimization (1)
Solving a differential equation with event detection can be used to find all local minimums and maximums for a function in a limited range. Consider the function
:

https://wolfram.com/xid/0b8empjuoy-dqv2dn

https://wolfram.com/xid/0b8empjuoy-buycoi
Solve the differential equation where the derivate of the function is 0 and store the points:

https://wolfram.com/xid/0b8empjuoy-kqq9fc
Plot the function and the minimum and maximum values:

https://wolfram.com/xid/0b8empjuoy-by343w

Crossing Detection (3)
Mark the points of arc length 1 for a parametric curve:

https://wolfram.com/xid/0b8empjuoy-iuhlqi

https://wolfram.com/xid/0b8empjuoy-vu2vv

Mark the points where a parametric solution enters or leaves an annular region:

https://wolfram.com/xid/0b8empjuoy-ttizp0

https://wolfram.com/xid/0b8empjuoy-gj8ktv

https://wolfram.com/xid/0b8empjuoy-5gift

Mark only the points where a parametric solution leaves a region:

https://wolfram.com/xid/0b8empjuoy-kcfml1

https://wolfram.com/xid/0b8empjuoy-8lhdu0

https://wolfram.com/xid/0b8empjuoy-ltkf7u

Mark only the points where a parametric solution enters a region:

https://wolfram.com/xid/0b8empjuoy-64ioen

https://wolfram.com/xid/0b8empjuoy-2s4v5t

https://wolfram.com/xid/0b8empjuoy-i4m7fp

Mark the points where the solution enters or leaves a region:

https://wolfram.com/xid/0b8empjuoy-6721f1

https://wolfram.com/xid/0b8empjuoy-qtsx9i

https://wolfram.com/xid/0b8empjuoy-jqv9uq

Hybrid Dynamical Systems (3)
Each time a linear oscillator solution crosses the negative axis, reflect it across the
axis:

https://wolfram.com/xid/0b8empjuoy-9bdu3

https://wolfram.com/xid/0b8empjuoy-y7ete
The solution of this reset oscillator exhibits chaotic behavior:

https://wolfram.com/xid/0b8empjuoy-uxod7

Plot the solution on the negative axis with a histogram of the reflection points:

https://wolfram.com/xid/0b8empjuoy-yidr71

Model a one-degree-of-freedom impact oscillator with sinusoidal forcing:

https://wolfram.com/xid/0b8empjuoy-hlphlf

https://wolfram.com/xid/0b8empjuoy-gdtcmp

Model a damped oscillator that gets a kick at regular time intervals:

https://wolfram.com/xid/0b8empjuoy-t44s44

https://wolfram.com/xid/0b8empjuoy-5rh2e4


https://wolfram.com/xid/0b8empjuoy-kq790h

The trajectory eventually settles into a consistent orbit:

https://wolfram.com/xid/0b8empjuoy-3eoai

Use the same oscillator, but give a random kick at regular time intervals:

https://wolfram.com/xid/0b8empjuoy-d3lwxu

https://wolfram.com/xid/0b8empjuoy-e9hmy7


https://wolfram.com/xid/0b8empjuoy-ftzloq


https://wolfram.com/xid/0b8empjuoy-jd6f8h

Friction Models (1)
Model a block on a moving conveyor belt anchored to a wall by a spring using different models for the friction force , including viscous, Coulomb, Stribeck, and static. Compare positions and velocities for the different models:


https://wolfram.com/xid/0b8empjuoy-tdrdct
Newton's equation for the block:

https://wolfram.com/xid/0b8empjuoy-sccosb
Viscous friction is proportional to the relative velocity :

https://wolfram.com/xid/0b8empjuoy-2frp1i

https://wolfram.com/xid/0b8empjuoy-yphp8b
The block stabilizes just above the spring's natural length of 1:

https://wolfram.com/xid/0b8empjuoy-m4ydrd

Coulomb friction is proportional to the sign of relative velocity :

https://wolfram.com/xid/0b8empjuoy-zvkzis

https://wolfram.com/xid/0b8empjuoy-eczy5
The block moves with the belt until the spring force is strong enough:

https://wolfram.com/xid/0b8empjuoy-inwr8c

Stribeck friction is a refined Coulomb friction :

https://wolfram.com/xid/0b8empjuoy-n4lr3j

https://wolfram.com/xid/0b8empjuoy-mnol8z
The variation at low velocities is slightly reduced:

https://wolfram.com/xid/0b8empjuoy-5vs7j7

Static friction holds the block in place until the spring force exceeds some value μ depending on roughness of surfaces. Use the discrete variable stuck set to 1 when the block is stuck and 0 otherwise:

https://wolfram.com/xid/0b8empjuoy-ep1kjy

https://wolfram.com/xid/0b8empjuoy-h2ftrq
Check whether the spring force is smaller than μ, and if the block is not moving relative to the belt:

https://wolfram.com/xid/0b8empjuoy-ti0wxz

https://wolfram.com/xid/0b8empjuoy-jsovqv
The block repeatedly sticks to the belt, then slips away due to the spring force:

https://wolfram.com/xid/0b8empjuoy-2o43hw

Comparing the different models:

https://wolfram.com/xid/0b8empjuoy-bzvmsh

Power Electronics (4)
Model an AC-to-DC full wave rectifier with four diodes and a capacitor for smoothing the output:

The input voltage vi and rectified voltage vr:

https://wolfram.com/xid/0b8empjuoy-bzm6w6

https://wolfram.com/xid/0b8empjuoy-bt5eqq

https://wolfram.com/xid/0b8empjuoy-i0xcg1

When vr increases, vo[t]=vr[t]; it charges the capacitor and supplies current to the load. When vr starts to decrease (vr'[t]<0), the capacitor discharges through the load and the output voltage follows vo'[t]=vo[t]/c r :

https://wolfram.com/xid/0b8empjuoy-5kfb11

https://wolfram.com/xid/0b8empjuoy-nesvjk

https://wolfram.com/xid/0b8empjuoy-kyig46

https://wolfram.com/xid/0b8empjuoy-mc42ti
Plot the smoothened capacitor output voltage:

https://wolfram.com/xid/0b8empjuoy-q6nl38

Model a DC-to-DC buck converter from input voltage level vi to desired output voltage level vd using a pulse-width modulated feedback control q[t]:

Use Kirchhoff's laws to get a model for the circuit above:

https://wolfram.com/xid/0b8empjuoy-m2awe
The control signal q[t] will switch the transistor on for a fraction vd/vi of each period τ:

https://wolfram.com/xid/0b8empjuoy-fkdchv
Buck from a higher voltage vi=24 to a lower voltage vd=16:

https://wolfram.com/xid/0b8empjuoy-cuxirb

https://wolfram.com/xid/0b8empjuoy-clreq9

https://wolfram.com/xid/0b8empjuoy-cmdcbl

Model a DC-to-DC boost converter from input voltage level vi to desired output voltage level vd using a pulse-width modulated feedback control q[t]:

Use Kirchhoff's laws to get a model for the circuit above:

https://wolfram.com/xid/0b8empjuoy-yjcwl
The control signal q[t] will switch the transistor on for a fraction vi/vd of each period τ:

https://wolfram.com/xid/0b8empjuoy-cbw3pa
Boost from a lower voltage vi=24 to a higher voltage vd=36:

https://wolfram.com/xid/0b8empjuoy-evrwaq

https://wolfram.com/xid/0b8empjuoy-fojjwr

https://wolfram.com/xid/0b8empjuoy-cocgov

Model a DC-to-DC buck-boost converter from input voltage level vi to desired output voltage level vo using a pulse-width modulated feedback control q[t]:

Use Kirchhoff's laws to get a model for the circuit above:

https://wolfram.com/xid/0b8empjuoy-gstkbo
The control signal q[t] will switch the transistor on for a fraction vd/(vi+vd) of each period:

https://wolfram.com/xid/0b8empjuoy-gubt24
Boost from a lower voltage vi=24 to a higher voltage vd=36:

https://wolfram.com/xid/0b8empjuoy-h76lvs

https://wolfram.com/xid/0b8empjuoy-bavmru

https://wolfram.com/xid/0b8empjuoy-bcnhp7

Buck from a higher voltage vi=24 to a lower voltage vd=16:

https://wolfram.com/xid/0b8empjuoy-hyeewh

https://wolfram.com/xid/0b8empjuoy-bgv78b

https://wolfram.com/xid/0b8empjuoy-bmb5vm

Digital Control (5)
Simulate the system stabilized with a discrete-time controller
:

https://wolfram.com/xid/0b8empjuoy-4pl02

https://wolfram.com/xid/0b8empjuoy-g35spr

https://wolfram.com/xid/0b8empjuoy-cvmz3f

Control a double integrator using a dead-beat discrete-time controller:

https://wolfram.com/xid/0b8empjuoy-lvb9sj
Use a dead-beat digital feedback controller :

https://wolfram.com/xid/0b8empjuoy-qfff3

https://wolfram.com/xid/0b8empjuoy-bt5k5v

https://wolfram.com/xid/0b8empjuoy-ci2tu

Model the position of a moving body with 1 kg mass:

https://wolfram.com/xid/0b8empjuoy-df4mkw
Use a sampled proportional-derivative (PD) controller to keep the position constant:

https://wolfram.com/xid/0b8empjuoy-c0b0i7

https://wolfram.com/xid/0b8empjuoy-xqvyw

https://wolfram.com/xid/0b8empjuoy-bmhzat

Design a discrete-time controller for the plant with state equations ,
and output
and simulate the closed-loop system:

https://wolfram.com/xid/0b8empjuoy-eqim

https://wolfram.com/xid/0b8empjuoy-bapg0n

Design a continuous-time controller by using a state estimator and state feedback:

https://wolfram.com/xid/0b8empjuoy-osdl3x

Get a discrete-time controller by sampling with sample time :

https://wolfram.com/xid/0b8empjuoy-b1ytnd

https://wolfram.com/xid/0b8empjuoy-fnecrw

Use the discrete-time controller:

https://wolfram.com/xid/0b8empjuoy-4z435

https://wolfram.com/xid/0b8empjuoy-kcaeo
Simulate the closed-loop system with the controller and plant together:

https://wolfram.com/xid/0b8empjuoy-e66ljo

https://wolfram.com/xid/0b8empjuoy-rfce8n

https://wolfram.com/xid/0b8empjuoy-mqukuq

Model a pendulum at angle θ anchored to a moving cart at position x undergoing a horizontal force f:

https://wolfram.com/xid/0b8empjuoy-4pb8el
Design a controller to stabilize the pendulum about the unstable vertical fixed point:

https://wolfram.com/xid/0b8empjuoy-20zs6x


https://wolfram.com/xid/0b8empjuoy-t0i7is

Use WhenEvent to apply the feedback at regular sample times:

https://wolfram.com/xid/0b8empjuoy-k96max

https://wolfram.com/xid/0b8empjuoy-6zjyvw
The pendulum's angle is quickly stabilized in the inverted position ():

https://wolfram.com/xid/0b8empjuoy-j5qmzq

PDE Models (1)
Model thermostat-controlled heat generation in a room with three insulated walls and a glass front subject to the outside temperature:

https://wolfram.com/xid/0b8empjuoy-g7sivm
A heater load is ramped up or down at an event. The factor of 20 specifies the slope of the transition:

https://wolfram.com/xid/0b8empjuoy-iownow
Visualize the heater at an arbitrary event time of 10:

https://wolfram.com/xid/0b8empjuoy-ige1zc

Note that there is an overlap in the up and down phases. This prevents them from continuously jumping between the two states, much like a real controller would.
The PDE models heat diffusion through air while generating heat inside a circle and losing heat through the glass window:

https://wolfram.com/xid/0b8empjuoy-p95yfn
If the thermostat at position measures a temperature below/above a trigger, and if the discrete variable
changed, the heater is switched on/off:

https://wolfram.com/xid/0b8empjuoy-v92bfp
Monitor the time integration of the PDE with initial condition equal to the outside temperature:

https://wolfram.com/xid/0b8empjuoy-b2x83o

Visualize the temperature measured at the thermostat, the outside temperature, and the triggers for the heater. A blue background is shown where the heater is on:

https://wolfram.com/xid/0b8empjuoy-3wr7yn

Inspect the time steps taken during the integration and note how NDSolveValue adjusted the time step size as needed during events:

https://wolfram.com/xid/0b8empjuoy-e9s51t

Properties & Relations (2)Properties of the function, and connections to other functions
NDSolve will try to automatically set up a WhenEvent event to handle a discontinuity in :

https://wolfram.com/xid/0b8empjuoy-bpfnhg

https://wolfram.com/xid/0b8empjuoy-kgbnh0

After automatic discontinuity processing, the following equivalent problem is solved:

https://wolfram.com/xid/0b8empjuoy-cekplw
If is a black-box function, NDSolve cannot automatically process the discontinuity:

https://wolfram.com/xid/0b8empjuoy-c5ash5

https://wolfram.com/xid/0b8empjuoy-86vb5

In this case, proper discontinuity handling can be achieved by adding a WhenEvent:

https://wolfram.com/xid/0b8empjuoy-bajmz

https://wolfram.com/xid/0b8empjuoy-fmhgse

Alternatively, the discontinuity can be crossed using "CrossSlidingDiscontinuity":

https://wolfram.com/xid/0b8empjuoy-fgdk3i

https://wolfram.com/xid/0b8empjuoy-44ngz

Possible Issues (6)Common pitfalls and unexpected behavior
Event Interpretation (1)
Events given explicitly with conditions have a slightly different interpretation than a single predicate:

https://wolfram.com/xid/0b8empjuoy-s2rno
WhenEvent cannot find an explicit root function, so the event occurs when pred becomes True:

https://wolfram.com/xid/0b8empjuoy-eoea1m

When an explicit function () is given, the event is considered as the root
but is conditional on the predicate (
) that is False at
, so it is never triggered:

https://wolfram.com/xid/0b8empjuoy-fedx7x
Show the solution trajectory and the event location with the background colored green where the condition is True and red where it is False:

https://wolfram.com/xid/0b8empjuoy-bf6dti

When an explicit function () is given, the event is considered as the root
:

https://wolfram.com/xid/0b8empjuoy-c66tfo
Show the solution trajectory and the event location with the background colored green where the condition is True and red where it is False:

https://wolfram.com/xid/0b8empjuoy-lr252b

Event Detection (2)
With the default "DetectionMethod", some events may be missed:

https://wolfram.com/xid/0b8empjuoy-ce7mb6

Try "DerivativeSign" or "Interpolation" for better event detection:

https://wolfram.com/xid/0b8empjuoy-f9531h


https://wolfram.com/xid/0b8empjuoy-jre8ps

Events corresponding to even-order roots may be missed:

https://wolfram.com/xid/0b8empjuoy-h9676s

Instead, rewrite with odd-order roots:

https://wolfram.com/xid/0b8empjuoy-cqgpqt

Arbitrarily Close Events (1)
Some systems admit an infinite number of events in a finite span of time:

https://wolfram.com/xid/0b8empjuoy-gyu6lv
Eventually, an event is not detected:

https://wolfram.com/xid/0b8empjuoy-cnk38


https://wolfram.com/xid/0b8empjuoy-isctct

Tightening the tolerances for locating events improves detection:

https://wolfram.com/xid/0b8empjuoy-njs4di

https://wolfram.com/xid/0b8empjuoy-gsztyb

https://wolfram.com/xid/0b8empjuoy-lwmody

Events will not be detected if they are closer than this:

https://wolfram.com/xid/0b8empjuoy-jkfizp

With detection by interpolation, event detection can be further improved:

https://wolfram.com/xid/0b8empjuoy-x0xqsk

https://wolfram.com/xid/0b8empjuoy-xah4tu
The gap between the last two events is very close to this limit:

https://wolfram.com/xid/0b8empjuoy-dqep3s

Stop the integration automatically before events are missed:

https://wolfram.com/xid/0b8empjuoy-7v2454

https://wolfram.com/xid/0b8empjuoy-gt5tuy

Events are detected up to the point at which integration is stopped:

https://wolfram.com/xid/0b8empjuoy-w0xvz6

Increasing the working precision allows detection of events on a finer time scale:

https://wolfram.com/xid/0b8empjuoy-uv8p4w

https://wolfram.com/xid/0b8empjuoy-qwik8s

Event Actions (2)
The highest derivative cannot be reset in an ODE:

https://wolfram.com/xid/0b8empjuoy-jrbokz



https://wolfram.com/xid/0b8empjuoy-fnw0ew

When solving as a DAE, the highest derivative can be reset:

https://wolfram.com/xid/0b8empjuoy-m8sobp

The corresponding value of is found by reinitializing so that the residual
is 0:

https://wolfram.com/xid/0b8empjuoy-i771jj

With sequential event actions, the variables are modified in turn:

https://wolfram.com/xid/0b8empjuoy-p2ojzn

https://wolfram.com/xid/0b8empjuoy-zq238w

To swap the variable values, use simultaneous events:

https://wolfram.com/xid/0b8empjuoy-fx9hlo

https://wolfram.com/xid/0b8empjuoy-3hw47c

Interactive Examples (2)Examples with interactive outputs
Compare a ball in a box with rational and irrational initial values:

https://wolfram.com/xid/0b8empjuoy-bsla3c

https://wolfram.com/xid/0b8empjuoy-n5kj

https://wolfram.com/xid/0b8empjuoy-hmhx76

Compare a ball in a box with rational and irrational initial values:

https://wolfram.com/xid/0b8empjuoy-diqwts

https://wolfram.com/xid/0b8empjuoy-h6qba

https://wolfram.com/xid/0b8empjuoy-epxl1v

Wolfram Research (2012), WhenEvent, Wolfram Language function, https://reference.wolfram.com/language/ref/WhenEvent.html (updated 2014).
Text
Wolfram Research (2012), WhenEvent, Wolfram Language function, https://reference.wolfram.com/language/ref/WhenEvent.html (updated 2014).
Wolfram Research (2012), WhenEvent, Wolfram Language function, https://reference.wolfram.com/language/ref/WhenEvent.html (updated 2014).
CMS
Wolfram Language. 2012. "WhenEvent." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/WhenEvent.html.
Wolfram Language. 2012. "WhenEvent." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/WhenEvent.html.
APA
Wolfram Language. (2012). WhenEvent. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/WhenEvent.html
Wolfram Language. (2012). WhenEvent. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/WhenEvent.html
BibTeX
@misc{reference.wolfram_2025_whenevent, author="Wolfram Research", title="{WhenEvent}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/WhenEvent.html}", note=[Accessed: 30-April-2025
]}
BibLaTeX
@online{reference.wolfram_2025_whenevent, organization={Wolfram Research}, title={WhenEvent}, year={2014}, url={https://reference.wolfram.com/language/ref/WhenEvent.html}, note=[Accessed: 30-April-2025
]}