simlogo1

 

 

 

SimQuest 6.4 help

 

 

Typing equations

 

An equation model is entered as a series of equations. There are some restrictions regarding what you can and cannot type in an equation.  The SimQuest Equation Model Editor has its own debugger, which will let you know if you made a mistake after you have pressed Accept. Make sure however that you stick to the basic rules below.

 

Simple equations

A typical simple equation model takes the form:

 

force = mass * gravity

torque = force * length

 

In this model the output variable force is calculated by multiplying the input variables mass and gravity. In the second equation, force is used as input variable to calculate torque. SimQuest uses the value of force from the first equation in the second.

 

Note: The order in which you enter equations does not matter; SimQuest determines the order of computation itself.

Note: The case of the names is important; Velocity is different from velocity.

 

Differential equations

A typical differential equation model takes the form:

 

acceleration = force/mass

velocity = acceleration 

dposition/dt = velocity

 

The meaning of this model is that at any moment during the simulation the first two equations hold, that is acceleration is computed from force, velocity is computed from acceleration. The third equation means that the change of the position is computed from the velocity. If at t=0 the position is 10, and the velocity is 2, at t = 1, the position will be 12, assuming the velocity does not change in this period. In mathematical terms, the equation dposition/dt is a differential equation and SimQuest knows how to handle these.

 

Discrete state equations

Apart from simple equations and differential equations, SimQuest also knows discrete state equations. These equations compute the new value of a variable out of the current state. These can be useful to handle discrete events, such as throwing a dice. For instance:

 

dice = random(1,6,true)

new(total) = total + dice

new(counter) = counter + 1

average = total/counter

 

Every time this model is computed, a random value is computed, which is added to an existing total. Also, a counter is incremented, so that the average can be computed. The equations starting with new(…) = are the discrete state equations. Discrete state equations can NOT be combined in one model with differential equations, (d../dt = …).

 

Variable types

As will be clear, variables play a central role in SimQuest equation models. We have four types:

 

Output variables

Appear on the left side of a simple equation:
outputVariable = 3 * inputVariable.

State variables

Appear as dependent variable in a differential equation:
dstateVariable = 2 * x

Discrete state variables

Appear in discrete state equations:
new(discreteState)= discreteState + 1

Input variables

All other variables that appear on the right side of equations.

 

In order for a model to be able to run, all state, discrete state and input variables need to get an initial value. After your model has compiled, you can set the initial values in the model editor.

 

Equation syntax

The general syntax of equation models is as follows:

 

<variable name> = <expression> 

 

OR

 

<differential expression> = <expression> (where <differential expression> is d<variable name>/dt)

 

OR

 

<discrete state expression> = <expression> (where <discrete state expression> is new(<variable name>))

 

Variable names may be any sequence of letters and numbers, starting with a letter.

Expressions are written down as simple mathematics expressions like:

 

a + b

3

4 * a

a ^ 3 (i.e. a to the power of 3, a*a*a)

sin a

5 * z + 8 * cos a

3 *(c + d)

a string (use single quotes for strings).

true

false (Boolean values)

a & b (logical and)

pi (= 3.1415926…)

 

Note: there is no need for parentheses as part of functions that take one argument, such as sin and cos, however they are not forbidden: cos(a) is as good as cos a.

 

Also, these functions have a low priority in computation order: cos a + 1 is computed as (cos a) + 1, not as cos(a+1). If you need the latter, parentheses should be used. Functions that can take more than one argument, such as random(), always get parentheses, even if the number of arguments is zero!

 

Conditional expressions are also possible:

 

b = if a > 0 then 5 else 5 endif

 

OR:

b = 5 * if a > 0 then 1 else 1 endif

 

An if-then-else-endif construct is part of an expression, it is not a statement. It is not allowed to write:

 

if x < 0 then a = 1 else a = -1 endif (WRONG!)

 

This does not match the pattern for an equation.

 

Equations may extend over multiple lines. In most cases, especially with if-then-else-endif constructions, you can format the equations as you like such as:

 

b = if a> 0

then

<some long expression>

else

<some long alternative expression>

endif

 

In some cases you must tell the compiler explicitly that the next line is also part of the same equation. You can do this by typing an underscore (_) at the end of the line.

 

Inserting comments

Comments can be inserted in the model. There are two ways for doing this. Short comments can be added at the end of the line by putting a semicolon (;) followed by the comment. The comment ends at the end of the line. Longer comments can be marked by double quotes:

 

a = b + c ; short comment

b = squared d

a longer comment 

Spreading over multiple lines

d = 5 * z _

+ 3 ; equation explicitly extended over two lines.

 

Operators and functions

Above a few operators and functions (sin, cos) were introduced in the examples above. The complete list of functions and operators available in SimQuest is:

 

Name

Description

 

 

abs x

the absolute value of x. abs 3 = 3; abs 3 = 3

arcCos x

the inverse cosine of x. The result lies between 0 and pi

arcSin x

the inverse sine of x. The result lies between pi/2 and pi/2

arcTan x

the inverse tangent of x. The result lies between pi/2 and pi/2

cos x

the cosine of x

exp x

e to the power of x

fractionPart x

the fractional part of x. fractionPart 1.345 = 0.345

ln x

the natural logarithm of x.

log x

the 10-base logarithm of x.

x max y

the maximum of x and y

x min y

the minimum of x and y

rounded x

x rounded to the nearest integer. rounded 3.2 = 3, rounded 3.6 = 4

random()

returns a random value between 0 and 1. Note: () are needed !

random(min, max)

returns a random value between min and max

random(min, max, true)

returns a random integer value between min and max

sin x

the sine of x

sqrt x

the square root of x

squared x

x multiplied by itself

tan x

the tangent of x

truncated x

x rounded to its integer part: truncated 3.6 = 3

x & y

x and y are Booleans. True when x and y are true

x | y

x and y are Booleans. True when either x or y is true

x + y

x plus y, or, when x and y are strings, the two strings concatenated.

x y

x minus y

x / y

x divided by y

x * y

x multiplied by y

x ^ ``y

x to the power of y

- x

x negated

x // y

integer division. 7//3 = 2

x < y

x is smaller than y

x <= y

x is smaller than or equal to y

x = y

x is equal to y

x >= y

x is greater than or equal to y

x > y

x is greater than y

x ~= y

x is not equal to y

 

Vectors

Apart from working with numbers, SimQuest can also calculate with a lot of numbers at a time. Consider the following example:

 

dx/dt = v

dv/dt = a

A = F/m

F = - unity x * lengthSquared x * Q

 

At first this looks like a simple equation model, apart from the last line. But we can interpret x as a vector. A vector is a variable that consists of a number of numbers. For instance x may be equal to [3, 4, 6] (which may be entered in the model editor as initial value).. So x represents a three-dimensional vector. In this case dx/dt means that the change of each of the vector components is computed. In fact, almost every operation with vectors, applies to the individual components of the vectors. Some examples of vector operations are:

 

x = [0,2,5] assign a vector value to a variable

[2,4,6] * [1,2,3] yields: [2,8,18]

4 * [1,2,3] yields: [4,8,12]

sin [1,2] yields: [sin 1, sin 2]

 

and so on. Of course there are a few specific vector operations:

 

min v

the minimum value of a vector (min [4,3,8] = 3)

max v

the maximum value of a vector (max [4,3,8] = 8)

average v

the average value of a vector (average [4,3,8] = 5)

length v

the length of a vector. length [1,1] = 1.41 (= sqrt (1 squared + 1 squared)

lengthSquared v

the square value of the length. Included for efficiency reasons. lengthSquared x computes fasted than (length v) squared

unity v

the unit vector, i.e. a vector with length equal to 1, in the direction of v. unity [1,1] = [0.71, 0.71]

v distance w

the distance between two points x distance y is equal to length (v w). Note that v and w need to have the same size, otherwise an error occurs

v distanceSquared w

similar to lengthSquared

v add elementOrVector

adds one or more elements at the end of a vector. [1, 2, 3] add 4 results in [1,2,3,4], [1,2,3] add [4,5] results in [1,2,3,4,5]

constantVector (n, x)

returns a vector of the form [x,x,x ] of size n

randomVector(n, min, max)

returns a vector of n random values between min and max

stepVector(start, stop, step)

returns a vector with values [start, start+ step, start + 2 * step, , stop]. stepVector(0,10, 2) is equal to [0,2,4,6,8,10]. If step is omitted a step size of 1 is assumed

substitute(vector, index, newValue)

substitutes one or more elements in a vector by new values. v = substitute([0,1,2,3,4,5], 3, 10), results in [0, 1,10, 3,4,5] (the third element is replaced). Multiple elements can be replaced at once by using a vector for the index and new values: v = substitute([0,1,2,3,4,5], [3,5], [10, 20]), results in [0, 1,10, 3,20,5]

interpolate(v, x)

returns an integer index of the value in the vector v, closest to x. interpolate([0,2,5,8,10,11,12], 9) returns 4. If x is lower than the first element, 1 is returned. If x is greater than the last element, n+1 is returned where n is the size of the vector. This function is useful when defining image animations

size v

the number of elements of the vector

 

Strings

Sometimes it is handy to display a string instead of a numeric value. Therefore SimQuest equations can also handle strings, and convert strings to numbers and vice versa:

 

x = a string

assign a string to a variable (use single quotes)

y = toString(z)

convert a number to a string

k = toValue(y)

convert a string to a number

result = The value of x =  + toString(x)

concatenate strings

 

Error handling

In some cases equations may result in errors. E.g., y = 1/x, will fail when x = 0. Of course this can be solved with an if-construction:

 

y = if x ~= 0 then toString(1/x) else undefined endif

 

but if there are multiple points for error in an equation (e.g. y = 1/((x-3) * (x-a) *(x-b))), then there are three cases to be checked. SimQuest offers an easy way to trap all errors, using the try function:

 

y = try(toString(1/((x-3) * (x-a) *(x-b))), undefined)

 

The try function tries to compute and return the expression entered as its first argument. If an error occurs during this computation, the second argument is returned. The user should take care that the second argument will not result in an error as well.

 

 

 

Related topics:

 

 

 

Copyright University of Twente 2011