Derivatives and State Variables
Two special kinds of variables in the CSL language are "state variables" and "derivatives". A state variable is a variable whose time history (trajectory) is a solution to a differential equation. A derivative is, as its name implies, the time-derivative of a state. A derivative's value is determined by one or more equations in a CSL model. The corresponding state is calculated by numerically integrating the derivative, with a specified initial condition.
State variables and derivatives are declared as such by their use in INTEG statements. This statement declares that the output variable (on the left side of an equal sign) is numerically integrated from the derivative expression and initial condition provided as arguments to the INTEG operator.
The following example illustrates the implementation of a simple differential equation in CSL. Consider a simple exponential decay ODE: x' = -x, with x(0) = 1.0. To represent x', we create a variable called "xd". We will retain "x" as the name of the state (the solution of the differential equation). We then write one statement to compute the derivative xd, and another statement to indicate that x is the numerical integral of xd, with an initial condition of 1.0:
! Simple exponential differential equation
xd = -x
x = integ(xd, 1.0)
In the above code snippet, the variable "x" is a state variable because it occurs on the left side of a equation using the INTEG operator. The variable "xd" is a derivative because it is the first argument to the INTEG operator. The initial condition in this case is a literal value (1.0), but could also be a variable or a constant.
Note that states, derivatives and initial condition must be floating point values (e.g., as opposed to integers or logicals), but are not restricted to scalar values. Integration of array or matrix quantities uses the same notation as scalar quantities, but the respective variables must have been declared as matrices or arrays, and must all share the same dimensions. See the Language Reference section on the DIMENSION statement for details.
Note that, in some cases, it is necessary for the Magnolia translator to insert "dummy" variables into the model. An example of how this is done regards the INTEG statement.