
« The Model workpanel  Sitemap  Model elements » VariablesAll magnitudes in an EJS model are called variables, regardless of whether they change over the course of the execution or remain constant. Variables can be modified either by the internal dynamics of the simulation or by user interaction with the simulation view. Additionally, changing a variable can sometimes produce a change in other variables. This situation is referred to by saying that there are fixed relations among these variables.
Variables are created in tables (or pages) of variables. To organize the variables in groups, perhaps according to their role in the model, you can create more than one table. Each variable has a Comment field at the bottom of the page to add a short description about the variable that can be useful for future readers. The table itself has also a Page comment field to describe the whole table. Tables of variables are processed from left to right. A context menu for reordering the tables is opened by rightclicking on the top tab.
A model with several pages of variables. The Comment field displays the comment for the variable currently selected.
To create a new variable in a table of variables of EJS, first assign it a name, then specify its type, and, in the case of vectors or matrices, its dimension. Finally, you can give the variable an initial value by typing a constant or a simple expression in the Initial value cell.
Variables without an initial value are given a default value (0 for numerical variables). These variables can be given an initial value later on, in the Initialization panel or by a page of fixed relations.
Every time a new variable is created a new row will appear in the same page to continue adding new variables. If you want to insert a variable between two existing ones or delete some of them use the popup menu that appears by rightclicking on the table.
Naming variablesIn naming our variables and the other elements of our simulation, such as panels, methods written for the model and view elements there are some rules and also some conventions.
Conventions:
The type of a variableDepending on how you will use them on the computer you need to specify for each of our variables what type it is. Although other types exist in Java, designed to optimize memory usage, EJS works with the following types:
A classic example of using variables of the type Object is a set of color variables. This can be used to make a certain element of the simulation change color. It could be something like:
Later the variable MyColor can be given another value with an expression like this:
The dimension of a variable
In Java indices of an array of elements start with the number zero. So in the case of a vector called posX with 10 elements the first element is referred to as posX [0] and the 10th element as posX [9]. Analogously, with matrices the element in the first row and first column is the array element [0] [0].
If an initial value is given to a matrix or vector variable the elements are all initialized with that same value. It’s also possible to let the initial value be dependent on the index position.
If n is an integer initialized with the value 5 this vector will be the vector (0,2,4,6,8)
« The Model workpanel  Sitemap  Model elements » 