# Tutorial: Dynamic system model

Release: 4.6  •  5.0  •  5.1  •  5.2  •  5.3  •  5.4  •  6.0  •  6.1  •  6.2  •  6.3

This chapter shows you how to:

• Use the Dynamic() function and the system variable Time
• Customize the appearance of variable nodes
• Organize a model using modules
• Make duplicates of nodes and modules
• Create aliases of variable nodes
• View the results of two variables simultaneously

Before starting this chapter you should know how to perform the following actions which will no longer be described in detail:

Create a new model; Open an existing model; Save; Save As... (See Tutorial: Open a model to browse)

Create and define new variables; Enter attributes in Attribute or Object windows; Draw influence arrows between nodes. (See Tutorial: Create a model)

You should also be familiar with Array variables and Index variables. (See Tutorial: Arrays)

## Using the Time index

In this model you will set up a dynamic system to model a predator/prey relationship. The populations of foxes and hares will be set up as state variables: one-dimensional arrays indexed by Time.

Start by opening a new model and titling it Foxes and Hares Tutorial. Create a general variable titled Hare population.

Using either the Attribute window or the Object window, place the cursor in the definition field and choose Table from the Expression popup menu. The Indexes window will appear.

Notice that Time is listed as one of the indexes in the left hand window. You did not have to define this index before using it. Time is a system index in Analytica that is always available to you. Note that it is distinguished by the system variable icon (). Although it is a system variable, the values of Time can be edited just like any other index.

1. Create a new general variable titled Hare population.
2. Open the Attribute pane (or Object window), select Definition in the attribute popup window then select Table in the Expression popup menu.

Select the Time index and click the transfer button () to select it as an active index in the right-hand window. Click OK to exit the Indexes window.

3. Select Time in the indexes dialog box'.

1. Click the transfer button to move Time to the Selected indexes window.

Analytica will show an edit table for the Hare population variable. Notice that the default index values for Time are 0,1, and 2. You will need to have more than just three time points in your dynamic simulation.

Go to the Definition menu and select Edit time. Click the Expression popup menu and select Sequence. Analytica will prompt to make sure you want to change the Time index to a Sequence. Click OK.

1. Select Edit Time from the Definition menu.
2. Select Sequence from the Expression popup menu (just above the Definition field.

Press OK when asked to replace the current definition with a sequence.

Edit the sequence by keeping 0 for the start value and entering 10 for the end value. Click OK to exit the Object Finder dialog.

3. Select sequence to span from 0 to 10
3. Select sequence to span from 0 to 10

If the edit table for Hare population is not already open select the Hare population node and click on the Result button (). Notice that our time index now goes from 0 to 10. We will leave the values of the Hare population zero the moment.

## Setting up the Hare sub-model

Create three new general variables titled Initial hare population, Hare fertility, and Hare births. Draw influence arrows as follows:

• from Hare fertility to Hare births
• from Initial hare population to Hare population
• from Hare population to Hare births

Define Initial hare population as 500. Change its Identifier to Hare_zero.

Define Hare fertility as 60%. We will take this to mean that the number of newborn hares will be equal to 60% of the total population during the previous time period!

Define Hare births as Hare_population*Hare_fertility. This is the total number of births during a time period.

Create, define and draw influence arrows to 3 new variables as instructed above.
Create, define and draw influence arrows to 3 new variables as instructed above.
Create, define and draw influence arrows to 3 new variables as instructed above.

## Using the Dynamic() function

Now we consider how the hare population will develop over time. The hare population will be influenced by the number of hare births, which will be influenced by the hare population during the previous period. Ultimately the value of Hare population depends on the value of itself during the previous time period. We can address this situation using the Dynamic function.

Using either the Attribute window or the Object window, open the Definition field of the Hare population variable. Click on the Expression popup menu and select .

Delete the existing table expression and replace it with the following:

The Dynamic() function can be entered directly as an expression or accessed as a built-in function (by selecting Other... in the Expression popup menu). It is listed under the Special library heading.

Note that the Dynamic function automatically incorporates the Time system variable. It was not necessary to initially define our variable as a table indexed by Time, although this step was included in the first section of this chapter to illustrate the Time index.

Select the Hare population node and click the Result button ().

This is a simple exponential growth curve. It represents a rapidly multiplying population of immortal hares with no natural predators and an unlimited food supply. You will need to introduce a limiting factor to avoid this buried-in-bunnies scenario.

## Completing the Hare sub-model

Quickly before it’s too late, create four new general variables as follows:

Finally you need to change the definition of Hare population as follows:

Dynamic(hare_zero, self[time -1] + hare_births[time - 1]- hare_deaths[time - 1])

Now we have accounted for mortality and limited food supply by introducing a crowding factor. When the crowding factor is equal to one, the hare population has reached the carrying capacity for the habitat. At this point the death rate will be equal to the birth rate.

Select the Hare population node and click the Result button (), then switch to the graph result view.

Press the Graph icon to display the result graph

Introducing a crowding factor limits the population to the carrying capacity

## Using the color palette

In order to make an influence diagram easy to understand it is often useful to color code nodes according to function. The color palette is used for this purpose. You can access it in two ways:

• Right-click the node and select Show/hide color palette from the context menu
• Go to the Diagram menu and select Show/hide color palette

In the color palette you can select from standard colors or click Other... to select from the entire 24-bit RGB color space.

Tip

The node style and diagram style windows are also useful for customizing the appearance of your diagram. They can be accessed in the same manner as the color palette.

## Creating a module

Modules are an important feature of Analytica. They allow you to place a collection of nodes inside a single object, a module, which has the appearance of a single node. In this way, the top level of your diagram can be a simplified view showing influence arrows between modules. Each module can be opened as its own diagram window to show detailed interactions of the variables inside. Modules can even be placed within modules to create a nested hierarchy of detail.

Tip

If you have several diagram windows open at once you can click the Diagram window button () to return to the top level diagram at any time.

Creating a new module is just like creating a variable node. Drag the Module node from the node palette to the diagram window. A module looks similar to a node but it has a thick border around it and displays the title in bold print. Whereas double-clicking a variable node will open an Object window, double-clicking a module will open a new diagram window. Attributes for the module can be entered using the Attribute window at the bottom of the diagram. Your new module will have the following attributes:

• Title: Hare module
• Identifier: Hare_module
• Description: A dynamic sub-model of the hare population

To populate your new module, start by making a multiple selection of all the nodes except for the module itself.

Reminder: To make a multiple selection hold down the Control or Shift keys, or drag a rectangular frame around the nodes you want to select.

1. Create a new module by dragging the Module icon down to the diagram space.
2. Make a multiple selection of all variable nodes by dragging diagonally across the space. (Make sure that the module is not also selected).
2. Make a multiple selection of all variable nodes by dragging diagonally across the space. (Make sure that the module is not also selected).
3. Drag one of the selected nodes to the module and release).

Your top level diagram should now contain just the module by itself. Double-click the module to see the variables in a separate window.

4. Double-click on the module to open a new diagram window.

Tip

If a single diagram window takes up the entire workspace press the maximize/un-maximize toggle () near the upper right corner of the window.

## Duplicating a module

Now you will make a similar sub-model for the fox population. Since the basic structure will be similar to the hare sub-model, you can save time by duplicating it automatically.

Select the hare module in the top level diagram window. Select Duplicate nodes from the Edit menu. A duplicate module will appear.

Edit the Title and Identifier of the duplicate to read Fox module

Edit the Description attribute to read A dynamic sub-model of the Fox population

1. After selecting the Hare module, go to the edit menu and select Duplicate nodes.
2. Retitle the duplicate as Fox module.

Double-click the Fox module to open its diagram window. (Or click on the Module's Diagram window Hover Icon ).

The diagram appears to be identical to the original but you will notice some important differences in the Identifiers. Open the Attribute window and check the identifier for the Hare population node. Notice it has been changed to Hare_population1. Analytica has appended the 1 to ensure that the identity of the new variable is distinct from the original. Different variables are allowed to have identical titles but they must always have different identifiers

Identifiers in functional expressions are modified accordingly. This ensures that the duplicate variables belong to a self-contained group that does not interfere with the original variables.

Drag one of the selected nodes to the module and release.

## Completing the Fox sub-model

Edit the titles and identifiers in the fox module as follows: (These edits mostly involve substituting the word Fox for Hare.)

Change the definition of input variables as follows:

• Initial Fox population: 120
• Fox fertility rate: 35%
• Fox carrying capacity: 200

The fox population is now a self-contained sub-model. Select the Fox population variable and click on the Result button (). Or you can hover over the node and click on the result hover icon.

Due to a lower fertility rate, the fox population approaches it's carrying capacity more slowly than the hare population.

## Creating aliases

Although it is useful to organize a model with modular structure, accessing variables inside modules can be less convenient than having them all in the same window. To address this problem Analytica allows you make an alias of a variable and place it anywhere you wish. The alias provides local access to the variable but it is merely a representation, not a duplicate, of the original node.

In this case it will be convenient to access the Hare population and Fox population nodes on the parent diagram without having to open the modules.

Click the Diagram button () to open the top level diagram. Open the Fox module and arrange the windows so that both are visible.

Select the Fox population variable. Go to the Object window and select Make alias. The alias looks similar to the original node but the title will be shown in italics. Drag the Fox population alias to the parent diagram.

1. Arrange windows so that the Fox module diagram and it's parent diagram are both visible.
2. Select the Fox population variable and select make alias from the Object menu.
3. Drag the Fox population alias to the parent diagram window.
Note that alias nodes always show titles in italics.

Repeat the same procedure to create a Hare population alias on the top diagram.

4. Create an alias for the Hare population variable in the same manner.
Influence arrows are automatically drawn.
Influence arrows are automatically drawn.

## Drawing influence arrows across modules

Now you are ready to introduce predatory interaction! Create a new general variable titled Predations. This will represent the number of hares eaten by foxes in a given time period. The predation level is assumed to be proportional to the product of fox and hare populations.

Draw influence arrows from the Hare population alias to Predations, and from the Fox population alias to Predations. Enter the following expression to define the new variable: Hare_population*Fox_population / 600'

1. Create a new general variable titled predations.
2. Draw influence arrows from the Hare population and Fox population aliases to the Predations variable.
2. Draw influence arrows from the Hare population and Fox population aliases to the Predations variable.
3. Define Predations as Hare_population * Fox_population/600

Unfortunately for the hares, each predation will count as a death for their team. Open the Hare module and arrange windows so that the Hare module diagram and its parent diagram are both visible. Draw an influence arrow from Predations to Hare deaths.

4. Open the Hare module and arrange window so that the Hare module diagram and it's parent diagram are both visible.
5. Draw an influence arrow from the Predations variable to the Hare deaths variable.

Re-define Hare deaths as: Hare_population*Hare_ndr + Predations

6. Edit definition of Hare deaths to read Hare_population * Ndr + Predations.

If you look closely at the Hare population and Hare deaths nodes you will notice small arrowheads on the margins. These indicate influence relationships with other nodes outside the module.

Arrowheads indicate influences directed outside the module.
Arrowheads indicate influences directed outside the module.

Predations are a food source for fox population and have a positive effect on their numbers. This can be modeled by making the Fox carrying capacity proportional to the Predations variable.

Re-define Fox carrying capacity as: Predations*1.6

7. Re-define Fox carrying capacity directly by typing Predations * 1.6 into the Definition field.
Tip

Analytica will allow you to use a variable in an expression before drawing its influence arrow. The variable will not be listed in the Inputs popup menu so you must type the exact identity correctly. Influence arrows will automatically be drawn after you finish editing the expression.

## Viewing multiple results simultaneously

Your model is now complete. Click the Diagram button () to open the top diagram. As you already know, you can view the results of the fox and hare populations separately by selecting either one of the population aliases and clicking the Results button (). But there is an easy way to combine both variables on the same graph. Select both alias nodes simultaneously (holding down the Control or Shift keys while selecting). Now when you click the Results button a dialog box will appear asking if you want to add a node to your model with this comparison. Click Yes and Analytica adds a new node to the diagram with the comparison, and display its result window. A comparison node is essentially a list variable with the variables to be compared as elements of the list.

The default title in this case will be Compare1.

1. Select the Hare population and Fox population aliases simultaneously.
2. Show the result by clicking the result button in the tools palette or the result hover icon (
3. Press Yes to confirm you want to add a node to the module with the comparison.

A new comparison node is generated and its result window shown.

The result window for Compare1 will contain information from both of the selected nodes. Switch to the Graph result, and choose Time as the horizontal axis in the Result window of Compare1. You can now see the result of the predatory interaction.

1. Click the Graph result icon.
2. Set time as the horizontal axis
The graph shows results for Fox and Hare populations simultaneously.

## Foxes and Hares Act III

How does the Fox/Hare drama end? You can extend the simulation by simply adding more values to the Time index.

Go to the Definition menu and select Edit time. Extend the sequence to go from 0 to 100, and click the green check button to accept the new definition.

1. Select Edit time from the definition menu.
2. Edit the definition to set 100 as the end step.
3. Click the green check to accept the definition.

Select the Compare1 node and click the Results button () again.

4. Show the result of the Compare1 node again
The populations eventually settle down to a new equilibrium

Now you can experiment by changing input values to see how they impact the result.

If you would like to see a more complete Foxes and Hares example, open Foxes and Hares.ana in the Tutorial Models folder.

## Summary

In this chapter you have:

• Used the Dynamic() function to model a feedback loop in a dynamic system.
• Edited the system variable Time which is used as an index in the Dynamic() function
• Customized the appearance of nodes using the Color palette
• Grouped nodes into Modules
• Used the Duplicate command to create a new module with similar structure to the original