Contents

Downloads
1 Bouncing Ball Example
2 Thermostat Example
3 Water Tanks Example
4 Stable and Unstable Switched Systems

1 Bouncing Ball Example

Top

The modeling and simulation of the bouncing ball example can be found in [1]. One can refer to step-by-step instructions for modeling a bouncing ball as a Stateflow chart [2].

The dynamics of a bouncing ball can be defined in terms of two continuous time variables, namely the position and the velocity of the ball, as shown in Fig. 1.


PIC

Figure 1: Simulink diagram for the bouncing ball example


In order to model this in Stateflow, you first need to create a chart whose "Update Method" is "continuous" and which has "Enable zero-crossing detection" option checked. Once this is done, you can create local variables whose "Update Method" is "continuous". For this model, we create two local variables p and v whose update method is "continuous". When you create a "continuous" local variable, you can also define its time-derivative by using the notation varname|_dot| where varname is the name of the continuous variable. Thus the variable p_dot refers to the time derivative of the position variable p. You automatically get access to this variable once you declare p to have "continuous" update.

Using this convention, the bouncing ball can be modeled using the following Stateflow chart, as shown in Fig. 2.


PIC

Figure 2: Stateflow chart for the bouncing ball example


For most of the time, the ball is just freely falling under the influence of gravity. The dynamics in this "mode" are specified in the "during" section of the "Falling" state. The sudden resets in position and velocity which occurs when the ball bounces are modeled using a self-transition with a condition action which performs the state reset.

When this model is simulated, we get the familiar bouncing trajectory of the ball, as shown in Fig. 3.


PIC

Figure 3: Simulation trajectories for the bouncing ball example


2 Thermostat Example

Top

The simulation is built using Simulink and Stateflow in MATLAB. Fig. 4 shows the Simulink diagram for this example. The left block (named “Thermostat”) is a Stateflow block, which simulates the hybrid dynamics of the thermostat. It has two outputs, i.e. x_output and q. x_output represents the temperature and q represents the switching mode (0 as “OFF” and 1 as “ON”). The right block is a scope block, used to visualize the trajectory of temperature and switching modes.


PIC

Figure 4: Simulink diagram for the thermostat example


You can explore the inside of “Thermostat” by double-clicking the Stateflow block. Fig. 5 shows how to model the thermostat example using the Stateflow chart. The chart is similar to the graphical representation of its hybrid automaton model. The two rectangular blocks represent two discrete modes, “OFF” and “ON”. In each mode, the temperature evolves according to a specified differential equation.


PIC

Figure 5: Stateflow chart for the thermostat example


The initial mode (“OFF”) and temperature value (60) are specified by the arrow above the “OFF” block. The conditions of transition between “ON” and “OFF” are specified by the arrows between two modes. The condition [x<=ml(’rand+69;’)] guarantees that the furnace will be turned on if the temperature is below a random number between 69 and 70. Similarly, the condition [x>=ml(’rand+70;’)] guarantees that the furnace will be turned off if the temperature is above a random number between 70 and 71. Randomness is introduced because we know the hybrid automaton model for this example is non-deterministic. The trajectories of temperature and discrete modes should be different each time the model runs.

When this model is simulated, we can see the trajectories of temperature and switching mode from the scope block. Fig. 6 shows the trajectories when the model runs once. Fig. 7 shows the different trajectories when the model runs another time.


PIC

Figure 6: Simulation trajectories when the model runs once



PIC

Figure 7: Simulation trajectories when the model runs another time


3 Water Tanks Example

Top

Fig. 8 shows the Simulink diagram for this example. It consists of three blocks, which are the Stateflow, XY graph plot and scope blocks. The “two tanks” Stateflow block simulates the evolution of water levels of the two tanks. It has three outputs, x1_output, x2_output and q, representing the water level of tank 1, water level of tank 2 and switching mode respectively. The scope and XY graph blocks are used to visualize the trajectory of water levels and switching mode.


PIC

Figure 8: Simulink diagram for the water tanks example


The inside of “two tanks” State block is shown in Fig. 9. The chart is similar to the graphical representation of its hybrid automaton model. The two rectangular blocks represent two discrete modes, “the hose is switched to tank 1” and “the hose is switched to tank 2”. In each mode, the water levels evolve according to the leaking and inflow rates, specified by the differential equations.


PIC

Figure 9: Stateflow chart for the water tanks example


The initial water levels are specified by the arrow above the “q1” block. The water levels for both ranks are 15 at the beginning. The conditions of transition between “q1” and “q2” are specified by the arrows between two modes. The condition [x2<=5] means that the hose will be switched to tank 2 instantaneously whenever the water level of tank 2 falls down to 5. Similarly, the condition [x1<=5] guarantees that the hose will be switched to tank 1 instantaneously whenever the water level of tank 1 falls down to 5.

From Fig. 10 and 11, it can be seen that the water levels are always above 5, which verifies that the goal of keeping the water levels of the tanks above certain level is achieved. However, a drawback of this modeling is the occurrence of Zeno phenomenon. The switching between the two tanks becomes faster and faster, and the state trajectories will converge to the point (5,5), which cannot happen in practice. This is because there are infinite number of switches within a finite time interval. This phenomenon can be captured by the model and an error message is given, as in Fig. 12.


PIC

Figure 10: XY plot of the trajectory of water levels



PIC

Figure 11: The trajectory of water levels and switching mode along time



PIC

Figure 12: The error message due to Zeno behavior


4 Stable and Unstable Switched Systems

Top

Fig. 13 shows the Simulink diagram for a switched system with two stable LTI subsystems

where A1 = , A2 = . Notice that both A1 and A2are are exponentially stable since all eigenvalues of A1 and A2 have negative real part. In addition, they have a common equilibrium point, which is the origin. The inside of “two stable systems” State block shows the dynamics of the two subsystems and the switching law. However, this simple hybrid system is unstable, since a divergent trajectory can be generated for an initial state even very close to the equilibrium point. See Fig. 14 for illustrations.


PIC

Figure 13: Simulink diagram for a switched system with two stable systems example



PIC

Figure 14: XY plot of the switched system with stable LTI subsystems


Fig. 15 shows the Simulink diagram for a switched system with two unstable LTI subsystems

where A1 = ,A2 = . Notice that both A1 and A2 are unstable since both have positive real part eigenvalues. However, the overall hybrid system is exponentially stable. This is illustrated in Fig. 16 as the state trajectory approaches the origin as time goes to infinity.


PIC

Figure 15: Simulink diagram for a switched system with two unstable systems example



PIC

Figure 16: XY plot of the switched system with unstable LTI subsystems


References

[1]   http://www.mathworks.com/help/stateflow/examples/modeling-a-bouncing-ball.html.

[2]   http://www.mathworks.com/help/stateflow/ug/modeling-a-bouncing-ball-in-continuous-time.html.