# Modeling Newton's Cradle with Virtual Reality

This example shows how to model a popular toy called "Newton's cradle" which consists of a row of seven identical balls which are hung from a common height. At rest they are arranged such that they just touch each other. One or more balls from one end are then raised from their rest position and released.

An interesting consequence of elastic collisions between the balls is that the balls which are released seem to come to a stop and an equal number of balls from the other end get released (with almost the same energy as the incoming balls). The balls in the middle do not seem to move, although they are responsible for transferring momentum from one end to another.

This example uses a simple elastic collision model to describe the interactions between the balls. The Stateflow® chart uses local variables to depict the continuous states of the system, namely the position `p`

and the velocity `v`

. Note that both these local variables are defined to have **Update method** as `continuous`

, so you can refer to their derivatives as `p_dot`

and `v_dot`

respectively. Since the nominal dynamics of all the balls are identical, this example uses these vector assignments to represent the motion of all of the balls:

p_dot = v; v_dot = -g/l*sin(p);

Note that `p_dot`

and `v_dot`

are not chart local variables. They are automatically created because `p`

and `v`

are defined to be continuous.

The model uses a simple for-loop to detect collisions between balls. In a one-dimensional setting, the chart only models collisions between successive balls with a single for loop.

The response to a collision is also expressed simply. Each collision is treated as a perfectly elastic instantaneous collision. The position and velocity are exchanged for each of the balls involved in the collision.

Simulating this model creates a Simulink® 3D Animation™ which shows the motion of the balls. Double-click on any ball to start the simulation.