# Documentation

## Continuous-Time Modeling in Stateflow

### What Is Continuous-Time Modeling?

Using continuous-time modeling you can simulate hybrid systems that use mode logic—that is, systems that respond to continuous and discrete mode changes. A simple example of this type of hybrid system is a bouncing ball. The ball moves continuously through the air until it hits the ground, at which point a mode change or discontinuity occurs. As a result, the ball changes direction and velocity due to a sudden loss of energy. A later exercise shows you how to model a bouncing ball in continuous time by using a Stateflow® chart (see Model a Bouncing Ball in Continuous Time).

When you configure Stateflow charts for continuous-time simulation, they interact with the Simulink® solver as follows:

• Maintain mode in minor time steps.

Stateflow charts do not update mode in minor time steps. The outputs computed in a minor time step are based on the state of the chart during the last major time step.

• Compute the state of the chart at each time step and expose the state derivative to the Simulink solver.

You can define local continuous variables to hold state information. Stateflow charts provide programmatic access to the derivatives of state variables. Continuous solvers in Simulink models use this data to compute the continuous states at the current time step in the chart, based on values from the previous time steps and the state derivatives. For more information on how solvers work, see Solvers (Simulink).

• Register zero crossings on state transitions.

Stateflow charts register a zero-crossings function with a Simulink model to help determine when a state transition occurs. When the Simulink solver detects a change of mode, it searches forward from the previous major time step to detect when the zero crossing or state transition occurred. For more information about how a Simulink model uses zero-crossing detection to simulate discontinuities in continuous states, see Zero-Crossing Detection (Simulink).

### When to Use Stateflow Charts for Continuous-Time Modeling

Use Stateflow charts for modeling hybrid systems with modal behavior, that is, systems that transition from one mode to another in response to physical events and conditions. In these systems, continuous time dynamics govern each mode. Be aware that you cannot use Moore charts for continuous time modeling.

In Stateflow charts, you can represent mode logic succinctly and intuitively as a series of states, transitions, and flow charts. You can also easily represent state information as continuous local variables with automatic access to time derivatives, as described in Purpose of Continuous-Time Variables.

If your continuous or hybrid system does not contain mode logic, consider using a Simulink model (see Model a Continuous System (Simulink)).

### Model Continuous-Time with Zero-Crossing Detection

You can run the following continuous-time models with zero-crossing detection.

ModelDescription

sf_abs

Rectifier takes a single (scalar) input and converts it to its absolute value. Shows how Stateflow charts register zero-crossing variables with Simulink models for accurate detection of mode changes.

sf_bounce

Shows how to model the dynamics of a bouncing ball by defining continuous-time state variables and their derivatives in a Stateflow chart.

Shows how to model elastic collisions between balls in Newton's Cradle, a device that conserves momentum and energy. Uses vector assignment to continuous-time state variables.

sf_clutch_enabled_subsystems

Implements the Simulink clutch example model purely in a Stateflow chart. Represents the modal nature of the clutch by using two states: Locked and Slipping.

sf_pool

Shows how to model continuous systems that have many discontinuous events, which rapidly (and unpredictably) change the dynamics.

### When to Disable Zero-Crossing Detection

Whether to disable zero-crossing detection on state transitions can be a trade-off between accuracy and performance. When detecting zero crossings, a Simulink model accurately simulates mode changes without unduly reducing step size. For systems that exhibit chattering, frequent fluctuations between two modes of continuous operation, zero-crossing detection can potentially impact simulation time. Chattering requires a Simulink model to check for zero crossings in rapid succession, which can slow simulation. In these situations, you can:

• Disable zero-crossing detection.

• Choose a different zero-crossing detection algorithm for your chart.

• Modify parameters that control the frequency of zero crossings in your Simulink model.