April 24, 2014

# Markov Chain Model

This is the first in a series of posts illustrating the use of MODL programming for ExtendSim model development. It uses an equation block to contain the MODL code. The model simulates the behaviour of a three state Markov chain. Inputs to the model are the state transition probability matrix and number of instances to simulate.

**Three State Markov Chain Model**

Before a brief look at the code it is worth making a few observations regarding the use of equation blocks:

- There are limitations to what can be coded in equation blocks. In particular, there are no static arrays or sub procedures. Workarounds are possible (using header files or global arrays for example) but may be cumbersome and there are memory limitations.
- This makes it difficult to generalise the model to arbitrary sized state spaces.
- In this case we would recommend the development of a custom value block. We will provide such a solution in a future post. While there are a few additional concepts that need to be understood we believe the extra flexibility to be well worth the effort.

**MODL Code for the equation block**

A few points worth noting about this code:

- The Initialise block at the top of the code is executed once the first time the equation is invoked. This is controlled by the Initialise static variable which is itself set to zero in the input variables listbox at the top of the equation dialog.
- Use of variables prefixed or suffixed by S0, S1 or S2. These static and local variables are to count the number of instances in each state. It is not difficult to see how use of arrays would simplify generalisation here.
- The StartCount static variable is used to determine the number of instances for looping purposes.
- Note the use of global array functions to access the ‘MarkovTransitionMatrix’ global array.

The model can be downloaded here: Markov3State.