is an option for NDSolve and other functions that specifies variables that only change at discrete times in a temporal integration.


  • DiscreteVariables->{v1, v2,} specifies that v1, v2, should be treated as variables that only change at discrete times.
  • The value of a discrete variable v can be changed through WhenEvent[event,v->val] or WhenEvent[event,v->"DiscontinuitySignature"].
  • The discrete variable solution v can be returned by using NDSolve[eqn,{v,},].
  • DiscreteVariables->{vspec1,vspec2,} can be used to specify the ranges for discrete variables.
  • Possible forms for vspeci are:
  • vv has range Reals or Complexes
    Element[v,Reals]v has range Reals
    Element[v,Complexes]v has range Complexes
    Element[v,Integers]v has range Integers
    Element[v,{n1,}]v has discrete range {n1,}
    {v,vmin,vmax}v has range v_(min)<=v<=v_(max)
    vspeciactioniperform actioni when vspeci is no longer satisfied
  • Derivatives with respect to time of discrete variables are zero almost everywhere and should not appear in the equations.
  • For partial differential equations, discrete variables can depend only on the temporal independent variable in a method of lines semi-discretization.


open allclose all

Basic Examples  (2)

Solve for with , a discrete variable that changes at regular intervals in time:

Solve for with , a discrete variable that changes whenever crosses 0:

Scope  (7)

Increment a discrete variable at regular time intervals:

Increment a discrete variable when the solution crosses 0:

Modify multiple discrete variables simultaneously when the solution crosses 0:

This time, enact the change in before modifying :

Stop the integration when a discrete variable goes out of the discrete range {1,2,3}:

Stop when the discrete variable goes out of the continuous range :

Print a message when out of range, but continue integrating the equation:

The initial condition is also out of range:

Discrete variables can take on non-numerical values:

Allow sliding mode solutions by using the action "DiscontinuitySignature":

Plot the vector field and solution:

The value of the discrete variable is 0 when the solution is in sliding mode:

Set the discontinuity state variable when reaches a sliding discontinuity curve :

Applications  (5)

Switch between two right sides of a differential equation using a discrete variable:

Set up a differential equation that switches between multiple right sides:

Simulate a ball bouncing down steps:

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

Simulate the system stabilized with a discrete-time controller :

Change the wave speed at in a wave equation:

Properties & Relations  (1)

NDSolve automatically handles discontinuous functions like Sign using discrete variables:

Use "DiscontinuitySignature" with a discrete variable to emulate the Sign function:

Possible Issues  (3)

When a discrete variable goes out of range, a message is displayed and the integration halts:

Derivatives of discrete variables cannot appear in the equations passed to NDSolve:

Discrete variables with "DiscontinuitySignature" action must have range {-1,0,1}:

If the range is {-1,1}, the sliding mode solution will not be found:

Specify the range as Element[a,{-1,0,1}] for sliding mode solutions:

Introduced in 2012