imit_model / AnyLogic / UsersManual(AnyLogic)
.pdf
AnyLogic V User’s Manual
Root
myObj1 |
myObj2 |
Name: |
y |
|
|
|
|
|
|
|
|
|
Name: |
w |
||
MyClass1 |
|
|
|
MyClass2 |
|
|||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Name: |
x |
|
|
|
|
|
|
|
|
Name: |
v |
|||
Equation type: |
formula |
|
|
|
|
|
|
|
|
Equation type: |
formula |
|||
Equation: |
x = -y |
|
|
|
|
|
|
|
|
Equation: |
v = w - 2 |
|||
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|||||
|
|
Name: |
MyClass1 |
|
|
Name: |
MyClass2 |
|
|
|||||
|
|
Equations: |
x = - y |
|
|
Equations: |
v = w - 2 |
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Figure 43. Equations over variables
This leads to the following set of algebraic equations:
x = −y |
|
|
|
v = w −2 |
|
|
, |
v = y |
|
w = x
and we can easily solve it:
x = −y |
x = −y |
x = −x + 2 |
|||
|
−2 |
|
−2 |
|
−2 |
v = w |
y = x |
y = x |
|||
|
|
|
|
|
|
v = y |
|
v = y |
|
v = y |
|
|
|
|
|
|
|
w = x |
|
w = x |
|
w = x |
|
x =1
y = −1v = y .w = x
The values of the variables are set exactly to these values irrespective of their initial values.
© 1992-2004 XJ Technologies http://www.xjtek.com |
97 |
Chapter 5. Equations
5. Equations
You can define a set of differential equations, algebraic equations, and formulas to describe continuous changes of variables over time. You may associate equations (hereinafter we use the term equations for both equations and formulas) with:
•An active object;
•A particular state of a statechart.
At any given moment of time there exists the global active set of equations in a model. This set is constructed of all currently active equations of all objects and all statechart states. Equations associated with an active object are active during the whole object lifetime. Equations associated with a statechart state are active while the statechart is at that state. A statechart contributes to the global set not only the equations of the current simple state, but also of all its container states.
AnyLogic supports matrices and hyper-arrays as well as primitive types in equations, see Chapter 6, “Matrices and hyper-arrays”.
5.1 Equation types
An equation can be of three types:
•Differential equation (see section 5.1.1.1, “Differential equations”)
•Algebraic equation (see section 5.1.1.2, “Algebraic equations”)
•Formula (see section 5.1.1.3, “Formulas”)
5.1.1.1 Differential equations
Differential equations are defined in the form
d(x)/dt=F(x,y,t,…)
98 |
© 1992-2004 XJ Technologies http://www.xjtek.com |
AnyLogic V User’s Manual
where x is an output or state variable of type double or of matrix type, and F(x,y,t,…) is a well-formed arithmetic expression which may contain any variables of the active object and the special symbol t denoting time. The expression F(x,y,t,…) can contain any arithmetic operations and method calls, such as, e.g. sin(), cos(), sqrt(), etc., or calls to your own methods.
F(x,y,t,…) should not call those methods that indirectly use variables of the active object; the methods should take variables as parameters. The best way to ensure this is to use static methods only.
F(x,y,t,…) should never call those methods that change variables of the active object.
F(x,y,t,…) should not contain conditional operators e.g. b>0 ? c:d.
You can define any number of differential equations. It is an error if a variable occurs more than once in the left-hand side in the set of equations. An input variable is not allowed to appear in the left-hand side.
Example
In the example shown in Figure 44, the behavior of the variable x is defined by the differential equation d(x)/dt=t.
|
|
MyClass |
|
Name: |
x |
Name: |
MyClass |
Default value: |
1 |
Equations: |
d(x)/dt = t |
Figure 44. Differential equations (I)
If you solve this equation analytically, you get x(t)=t^2/2 + C, which, if you take into account the initial value x(0)=1, gives x(t)=t^2/2+1. This is a parabola. Figure 45 shows this parabola calculated and drawn by AnyLogic.
© 1992-2004 XJ Technologies http://www.xjtek.com |
99 |
Chapter 5. Equations
Figure 45. Parabola drawn by AnyLogic
Example
The differential equations presented in Figure 46 define a mathematical pendulum; x is the pendulum coordinate, y is its velocity. The solution calculated by AnyLogic is shown in Figure 47.
Name: |
x |
MyClass |
|
Default value: |
1 |
Name: MyClass |
|
Equations: |
|||
|
|
||
|
|
d(x)/dt = y |
|
Name: |
y |
d(y)/dt = -x |
|
|
|||
Default value: |
0 |
|
Figure 46. Differential equations (II)
100 |
© 1992-2004 XJ Technologies http://www.xjtek.com |
AnyLogic V User’s Manual
Figure 47. Coordinate and velocity of the mathematical pendulum
5.1.1.2 Algebraic equations
Algebraic equations are defined in the form
x=F(x,y,t,…)
where x is an output or state variable of type double or of a matrix type, and F(x,y,t,…) is a well-formed arithmetic expression. For the rules that apply to F(x,y,t,…) see section 5.1.1.1, “Differential equations”.
Also, you specify the set of unknown variables, assuring that the number of unknown variables equals the number of algebraic equations:
find(x, y, …)
You can define any number of algebraic equations. You cannot specify an input variable as an unknown one.
© 1992-2004 XJ Technologies http://www.xjtek.com |
101 |
Chapter 5. Equations
Example
Figure 48 shows an example of a set of two equations with two unknown variables.
Name: |
x |
MyClass |
|
Default value: |
0 |
||
Name: MyClass |
|||
|
|
||
|
|
Equations: |
|
|
|
0 = x + y |
|
Name: |
y |
0 = x - y – 2 |
|
find(x, y) |
|||
Default value: |
0 |
||
|
Figure 48. Algebraic equations
Obviously, the solution is: x = 1, y = -1.
5.1.1.3 Formulas
Formulas are defined in the form
x=F(y,t,…)
where x is output or state variable of type double or of vector/matrix type, and F(y,t,…) is a well-formed arithmetic expression. For the rules that apply to F(y,t,…) see section 5.1.1.1, “Differential equations”. The only additional restriction is that F(y,t,…) cannot not contain x because then it would be an algebraic equation, which must be defined using another form.
You can define any number of formulas. It is an error if a variable occurs more than one time in the left-hand side in the set of equations. An input variable cannot appear in the lefthand side.
A formula defines a direct dependency between variables. It is a special case of algebraic equation, but it does not need a time-consuming numerical solution. Therefore it is recommended to use formulas whenever possible to increase simulation performance.
102 |
© 1992-2004 XJ Technologies http://www.xjtek.com |
AnyLogic V User’s Manual
5.2 Defining an equation
You specify an equation as a string. For convenience you can specify equations in the Equations code section of an active object class or in the Equation section of a variable. Those two places are self-synchronized; that is, if you write or remove an equation in one place, the equation is added or removed in the other place automatically. In the Equations code section of an active object class, you can specify all kinds of equations. However, in variable’s properties, you can define only differential equations and formulas for this particular variable.
►To define an equation in active object class code
1.In the Project window, right-click the Code item of the active object class and choose Open Code from the popup menu, or
Double-click the Code item of the active object class.
The Code window of the active object is displayed, see Figure 49.
2.Type the equation in the Equations section.
Figure 49. Code window of the active object class
►To define a differential equation for a variable
1.Select the variable on the structure diagram.
© 1992-2004 XJ Technologies http://www.xjtek.com |
103 |
Chapter 5. Equations
2.In the Equation section of the Properties window, choose Integral or Stock from the Form drop-down list.
3.Type the right hand of the equation in the d(variable)/dt = edit box.
4.Type the variable initial value in the Initial value edit box.
►To define a formula for a variable
1.Select the variable on the structure diagram.
2.In the Equation section of the Properties window, choose Formula from the Form drop-down list.
3.Type the formula for a variable in the variable = edit box.
AnyLogic enables you to visualize the resulting dependencies between variables in your model with arrows (see Figure 50). An arrow pointing from variable A to variable B means that variable A is mentioned in the equation of variable B.
►To show/hide the dependencies between variables
1.Click the Show/Hide Variable Dependencies
toolbar button, or Choose Draw|Variable Dependencies from the main menu.
If dependencies are shown, the toolbar button looks blued.
104 |
© 1992-2004 XJ Technologies http://www.xjtek.com |
AnyLogic V User’s Manual
Figure 50. Dependencies displayed on the structure diagram
You may associate equations with a particular state of a statechart (composite as well as simple). You define equations for the statechart state into the property Equations of the state.
►To define equations for a statechart state
1.Click the state on the statechart diagram.
2.In the Properties window, type equations in the Equations section.
5.3 Functions
AnyLogic enables using custom functions in equations, namely:
•AnyLogic provides a set of predefined functions – the most frequently used mathematic functions.
•AnyLogic supports a special type of function – a lookup table. A lookup table is a continuous function defined in the table form. You may need it to define a complex non-linear relationship which cannot be described as a composition of standard functions, or to bring data defined as a table function to a continuous mode.
•AnyLogic enables you to define custom functions. This is frequently needed when there is a standard composition of functions used in multiple equations. In this case
© 1992-2004 XJ Technologies http://www.xjtek.com |
105 |
Chapter 5. Equations
you can simply define a function once and reuse it. You can define a mathematical function if you need to define a mathematical expression or an algorithmic function (a function written in Java), if you need to code some calculation algorithm.
This section describes using all these types of functions in equations.
5.3.1Predefined functions
AnyLogic provides a set of predefined functions – the most frequently used mathematic functions. You can use any composition of the predefined functions in a right-hand side of an equation and in initial values.
Table 2 lists the predefined functions.
Predefined functions |
Description |
|
|
|
|
sin, cos, tan |
Trigonometrical functions. |
|
|
|
|
asin, acos, atan, |
Inverse trigonometrical functions. |
|
atan2 |
||
|
||
|
|
|
pow, sqrt |
Power and square root functions. |
|
|
|
|
exp |
Exponent function. |
|
|
|
|
log |
Natural logarithm. |
|
|
|
|
abs |
Absolute value. |
|
|
|
|
min, max |
Minimal (maximal) value of the two arguments. |
|
|
|
|
round, rint, |
Real-to-integer conversions. |
|
floor, ceil |
||
|
||
|
|
|
random |
Random number uniformly distributed in the interval [0,1). |
|
|
|
|
delay |
Time shift. |
|
|
|
|
xidz, zidz |
Division functions. |
|
|
|
|
106 |
© 1992-2004 XJ Technologies http://www.xjtek.com |
