EQUATION TYPES

Integration Method

METHOD name name = Euler, RK2, RK4, Auto, or Stiff

Integration Parameters (Default Settings)

STARTTIME = 0          Initial time
STOPTIME = 10           Final time
DT = 0.02                       Integration time step (Euler, RK2, and RK4 methods only)
DTMIN = 1.0e-6            Minimum DT (Auto and Stiff methods only)
DTMAX = 1.0                Maximum DT (Auto and Stiff methods only)
TOLERANCE = 0.01     Relative accuracy for Auto and Stiff integration methods
DTOUT = 0                    Output time interval (0 = store every step)
ROOTTOL = 0.001        Relative accuracy for built-in root finders

You can change the names of the above symbols and TIME by using the RENAME statement like this:

RENAME TIME = X
RENAME STARTTIME = Xo
RENAME STOPTIME = Xf

Initialization Equations

x(STARTTIME) = expression
INIT x = expression
INIT(x) = expression

Differential Equations

x' = expression
d/dt(x) = expression
FLOW x = expression
x(t) = x(t - dt) + (expression) * dt
x = x + dt * (expression)

You can define second-order (and higher) equations directly using the "prime" notation like this:

x'' = expression

Berkeley Madonna internally translates this into a system of two first-order equations with variables named x' and x.   You can refer to these variables in equations just like other variables you define.  You must provide initializers for each of these variables.  For example, here is a third-order system:

x''' = -2*x'' - 3*x' - x + 1
init x'' = 2
init x' = -1
init x = 0

Difference Equations

x(t + dt) = expression
NEXT x = expression

The value of x is replaced by the value of the expression on the right-hand side.  Therefore, to implement a typical finite difference equation where some value f(x) is added to the current value of x, you must write:

x(t+dt) = x + f(x)    or
NEXT x = x + f(x)

Limit Equations

LIMIT v >= c      Constrains variable v to be >= c
LIMIT v <= c      Constrains variable v to be <= c

Note that c does not need to be a constant (it can vary over time).

Root Finder Equations

An initial root finder runs once during the initialization step.   You supply an initial guess, the expression whose root (zero) you want to find, and lower and upper limits for the independent variable:

GUESS x = ...      Initial guess
ROOTI x = f(x)      Solve for f(x) = 0
LIMIT x >= ...      Lower limit for x
LIMIT y <= ...      Upper limit for x

A step root finder works like an initial root finder except that it runs not only during initialization but during each subsequent time step.  Step root finders are denoted with the ROOTS keyword instead of ROOTI.

Multidimensional problems are defined by creating multiple ROOTI or ROOTS statements whose right-hand sides depend on each other's independent variables.  For example, to find x and y such that both f(x,y)=0 and g(x,y)=0, you would write the following:

GUESS x = ...
GUESS y = ...
ROOTI x = f(x,y)
ROOTI y = g(x,y)
LIMIT x >= ...
LIMIT x <= ...
LIMIT y >= ...
LIMIT y <= ...

Discrete Equations

A variable can be defined as a conveyor, oven, or queue as follows:

x = CONVEYOR(input, transit_time, capacity)
x = OVEN(input, cook_time, fill_time, capacity)
x = QUEUE(input1, input2, ..., inputN)

The capacity argument for conveyors and ovens is optional; if omitted, the conveyor or oven has unlimited capacity.  See the "How Do I Use Conveyors/Ovens/Queues" models (Help menu) for more information.

Display Command

DISPLAY  a, b, ...     Display only symbols a, b, ... in lists

Array Equations

One-dimensional arrays (vectors) are defined as follows:

y[1] = expression      Define vector with 1 element (y[1])
y[1..3] = expression      Define vector with 3 elements (y[1], y[2], y[3])

Two-dimensional and three-dimensional arrays are similarly defined:

y[1,1] = expression      Define 2D array with 1 element (1x1)
y[1..2,1..2] = expression      Define 2D array with 4 elements (2x2)
y[1..3,1,1..4] = expression      Define 3D array with 12 elements (3x1x4)

In array equations, i, j, and k represent the index(es) of the element being assigned.  For example:

y[1..3] = k * f[i]

is equivalent to:

y[1] = k * f[1]
y[2] = k * f[2]
y[3] = k * f[3]

and:

y[1..2,1..3] = k * f[i] + g[j]

 is equivalent to:

y[1,1] = k * f[1] + g[1]
y[1,2] = k * f[1] + g[2]
y[1,3] = k * f[1] + g[3]
y[2,1] = k * f[2] + g[1]
y[2,2] = k * f[2] + g[2]
y[2,3] = k * f[2] + g[3]
 
Arrays of differential and difference equations are defined by placing the brackets immediately following the variable name.  For example, the following equations define a set of three differential equations:

INIT y[1] = 1
INIT y[2] = 10
INIT y[3] = 100
d/dt(y[1]) = -0.1 * y[1]
d/dt(y[2]) = -0.1 * y[2]
d/dt(y[3]) = -0.1 * y[3] 

These equations could be written more succinctly as follows:

INIT y[1..3] = 10^(i-1)
d/dt(y[1..3]) = -0.1 * y[i] 

Comments

Single-line comments can be added anywhere in your equations.   They begin with a semicolon and continue to the end of the line.   Comments which span multiple lines can be enclosed in curly brackets or composed of a series of single-line comments.  For example:

y = sin(time) ;This is a single-line comment
{This comment spans two lines and ends with
a matching curly bracket.}

;Here is a three line
;comment made out of
;single-line comments.

Curly-bracket comments can be nested.  This make it easy to "comment out" equations that already contain comments.   For example, the following won't compile because x and y are not defined:
 
a = 1

{x = cos(time) {Horizontal}
y = sin(time)  ;Vertical}
z = x^2 + y^2 

BUILT-IN FUNCTIONS

Basic Functions

ABS(x)      Absolute value of x
SQRT(x)     Square root of x
INT(x)     Largest integer <= x
ROUND(x)     Integer nearest to x
MOD(x, y)     Remainder of x / y (see: Creating Periodic Functions)
MIN(x1, x2, ...)     Minimum of x1, x2, ...
MAX(x1, x2, ...)     Maximum of x1, x2, ...
SUM(x1, x2, ...)     Sum of x1, x2, ...
MEAN(x1, x2, ...)     Average of x1, x2, ...
SWITCH(x, y)   Same as x >= y

Trigonometric Functions

PI      Value of pi
SIN(x)     Sine of x
COS(x)     Cosine of x
TAN(x)     Tangent of x
ARCSIN(x)      Inverse sine of x
ARCCOS(x)      Inverse cosine of
ARCTAN(x)     Inverse tangent of x
SINH(x)      Hyperbolic sine of x
COSH(x)      Hyperbolic cosine of x
TANH(x)      Hyperbolic tangent of x
ARCSINH(x)      Inverse hyperbolic sine of x
ARCCOSH(x)      Inverse hyperbolic cosine of x
ARCTANH(x)      Inverse hyperbolic tangent of x

Logarithmic Functions

LOGN(x)     Natural logarithm of x
LOG10(x)      Common logarithm of x
EXP(x)     Inverse natural logarithm of x

Other Transcendental Functions

ERF(x)     Error function of x
ERFC(x)     Complimentary error function of x
GAMMALN(x)      Natural logarithm of the gamma function of x

Discrete Functions

RANDOM(a, b)     Uniform random number between a and b
RANDOM(a, b, s)     Uniform random number with seed s
NORMAL(µ, sd)     Normal random number: mean µ and variance v
NORMAL(µ, sd, s)     Normal random number with seed s
BINOMIAL(p, n)      Binomial random number: n trials with probability p
BINOMIAL(p, n, s)      Binomial random number with seed s
POISSON(µ)      Poisson random number with mean µ
POISSON(µ, s)      Poisson random number with seed s
DELAY(x, d)     Value of x delayed by time d
DELAY(x, d, i)     Value of x delayed by time d with initial value i
MAXINFLOW(x)      Maximum allowable inflow into conveyor or oven x
OUTFLOW(x)      Outflow from conveyor or oven x
OSTATE(x)      State of oven x: 0=idle, 1=filling, 2=cooking, 3=emptying
QPOP(x, n, m)      Removes up to n items from queue x totaling m or less
QPOPS(x, n, m)      Like QPOP, but can remove part of an element to reach m

Miscellaneous Functions

STEPSIZE      Change in TIME from previous step to current step
INIT(x)     Value of expression x at STARTTIME
NETFLOW(r)    Net flow into reservoir r from previous time step
STEP(h, t)   Same as IF TIME >= t THEN h ELSE 0
PULSE(v, f, r)     Impulses of volume v, first time f, repeat interval r
SQUAREPULSE(t, d)      Pulse of height 1 starting at time t with duration d
GRAPH(x) (x1,y1) (x2,y2) (x3,y3) ...  Lookup x in list of points using linear interpolation

Array Functions

ARRAYSUM(x[*])     Sum of all elements in array x
ARRAYMEAN(x[*])     Average of all elements in array x
ARRAYSTDDEV(x[*])     Standard deviation of all elements in array x 

Dataset Functions

Any dataset in your model can be used as a piecewise-linear function.  Simply use the dataset as if it were a built-in function taking one or two arguments:

t = #temperature(x)
q = #charge(x,y)

This example assumes #temperature is a vector (one-dimensional) dataset and #charge is a matrix (two-dimensional) dataset.  Berkeley Madonna computes the result by performing linear interpolation on the points nearest the argument value(s).

OPERATORS

Arithmetic Operators

x + y     Addition
x – y     Subtraction
x * y     Multiplication
x / y     Division
x ^ y     Exponentiation
x ** y     Same as x ^ y
-x     Negation

Relational Operators

x = y     Equal to
x <> y     Not equal to
x < y     Less than
x <= y     Less than or equal to
x > y     Greater than
x >= y     Greater than or equal to

Logical Operators

x AND y     Logical AND
x OR y     Logical OR
NOT x     Logical inverse

Conditional Operator

IF x THEN y ELSE z

HINTS

Making Non-negative Reservoirs and Unidirectional Flows

Use LIMIT statements to make reservoirs non-negative and flows unidirectional.  For example:

r(t) = r(t - dt) + (i - o) * dt
LIMIT r >= 0     Makes reservoir r non-negative
LIMIT i >= 0     Makes inflow i uni-directional
LIMIT o >= 0     Makes outflow o uni-directional

Creating Periodic Functions

y = F(MOD(TIME, i))     F repeats over the time interval i

Example: Square wave with period = 1

y = IF MOD(TIME, 1) >= 0.5 THEN 1 ELSE 0