| Contents | Index |
| On this page… |
|---|
The following topics give you step-by-step instructions for modeling a bouncing ball as a Stateflow chart in continuous time using the workflow described in Workflow for Creating Continuous-Time Charts.
The dynamics of a bouncing ball describes the ball as it falls, when it hits the ground, and when it bounces back up.
You can specify how the ball falls freely under gravity using the following second-order differential equation:
![]()
In this equation,
describes the
position of the ball as a function of time, and
, which is the acceleration due
to gravity.
A Stateflow chart requires that you specify system dynamics as first-order differential equations. You can describe the dynamics of the free-falling ball in terms of position p and velocity v using the following first-order differential equations:
| Equation | Description |
|---|---|
|
| Derivative of position is velocity |
|
| Derivative of velocity is acceleration |
The bounce occurs after the ball hits the ground at position p <= 0. At this point in time, you can model the bounce by updating position and velocity as follows:
Reset position to 0.
Reset velocity to the negative of its value just before the ball hit the ground.
Multiply the new velocity by a coefficient of distribution (-0.8) that reduces the speed just after the bounce.
The following steps take you through the workflow for modeling a bouncing ball in continuous time. To view the completed model, open the bouncing ball demo.
Create an empty Stateflow chart and open its properties dialog box.
If you need instructions, see Creating an Empty State Chart.
Set the update method to Continuous.
For this example, enable zero-crossing detection (the default) so that the Simulink model can determine exactly when the ball hits the ground at p <= 0. Otherwise, the Simulink model might not be able to simulate the physics accurately. For example, the ball might appear to descend below ground.
Define two continuous-time variables, p for position and v for velocity. For each variable, follow these steps:
In the Stateflow Editor, select Add > Data > Local.
Enter the name for the local data.
Set the update method to Continuous.
Leave all other properties at their default values and click OK.
Define two outputs, p_out and v_out for exposing continuous state to the Simulink model. For each variable, follow these steps:
In the Stateflow Editor, select Add > Data > Output to Simulink.
Enter the name for the output data.
Leave all other properties at their default values and click OK.
Your chart should contain the following data, as viewed in the Model Explorer:

For this example, you can use ode45 (Dormand-Prince), the default variable-step solver used by Simulink models with continuous states.
Add a state named Falling with a default transition. In the default transition, set initial position to 10 meters and initial velocity to 15 meters/second.

Add a during action to the Falling state that defines the derivatives of position and velocity, as follows.

The derivative of position is velocity and the derivative of velocity is acceleration due to gravity (-g).
In the during action, assign position to the output p_out and assign velocity to the output v_out, as follows.

Check semantics against the requirements defined in Design Considerations for Continuous-Time Modeling in Stateflow Charts.
This chart meets design requirements:
Assigns values to derivatives p_dot and v_dot in a during action
Writes to local variables p and v in a transition action
Initializes local variables on the default transition
Does not contain events, inner transitions, event-based temporal logic, or change detection operators
Attach each output to a scope.

Simulate the chart and view the outputs in the scope.

Note that the ball appears to fall below the ground (below position p = 0) because the chart does not yet include a check for the bounce.
Add a self-loop transition to state Falling.

Add a condition on the transition that indicates when the ball hits the ground.
The condition should check for position p <= 0 and velocity v < 0, as follows.

When the ball hits the ground, reset position and velocity in a condition action, as follows.

Simulate the chart again. This time, the scope shows the expected bounce pattern.

![]() | Defining Continuous-Time Variables | Design Considerations for Continuous-Time Modeling in Stateflow Charts | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |